Forum Archive

Help with touch (game)

Pythonistapro777

So, I found this game on the forum and it gave me an idea to make a gravity guy game. @Webmaster4o helped my modify the already made game so instead of pressing and holding, you could just tap to flip gravity.

Here's the game:

from scene import *
import random

class MyScene (Scene):
    def setup(self):
  # This will be called before the first frame is drawn.

        self.x = 100
        self.y = 0
        self.scr_h = 400
        self.scr_w = 470
        self.flr = 32 #floor
        self.g = 0.5 #gravity
        self.acel = 0

        self.grav = True

        self.lw = self.scr_h

        self.ms = 6 #max speed
        self.n = 6
  #wall
        self.ex = self.scr_h - 32
        self.ey = self.flr
        self.etimer = 0.0
        self.box_h = 32
        self.box_2 = 32
        self.rand = 1
        self.col = 0
        self.m = 32

  #reset
        self.pts = 0
        self.rtimer = 0

  #collisions
        self.dead = False
        pass

    def draw(self):
  # This will be called for every frame (typically 60 times per second).
        background(255, 255, 255)
        stroke(255,0,0)

  #floor
        if self.col < self.n:
            line(0,self.flr,self.lw,self.flr)
            stroke(200,100,50)
            stroke_weight(1)
        else:
            self.flr = 0
            self.ey = 0
            self.m = 64
        if self.y == 0:
            self.dead = True

        if self.col == self.n - 1:
            text('Lets fly!!', 'Arial', 12, self.scr_h / 2, (self.scr_w / 2) + 16, 5)
            self.lw = self.ex

  #text
        tint(0,0,0,1)
        if self.dead == True:
            text('You are DEAD', 'Arial', 12, 48, self.y+ 16, 5)

        if self.dead == False:
            self.pts += 1

        text('Points: ' + str(self.pts), 'Arial', 12, self.x-48, self.y + 32, 5)

  #character
        rect(self.x,self.y,32,32)

  #character badguy
        rect(self.ex,self.ey,32,self.box_h)
        rect(self.ex,self.ey+self.scr_w,32,-self.box_2-32)

  # Touch input:
        fill(1,0,0)
        if not self.grav:
            self.acel += 1

    #reset when dead
            if self.rtimer > 30:
                self.ex = self.scr_h - 32
                self.ey = self.flr
                self.rtimer = 0.0
                self.box_h = 32
                self.box_2 = 32
                self.rand = 1
                self.pts = 0
                self.etimer = 0
                self.dead = False
                self.flr = 32
                self.ey = self.flr
                self.col = 0
                self.m = 32
                self.lw = self.scr_h

        if self.dead == True:
            self.rtimer += 1
            text('Press to reset!', 'Arial', 12, self.scr_h / 2, self.scr_w / 2, 5)

  # Physics
        if self.dead == False:
            self.y += self.acel

        if self.y < self.flr:
            self.y = self.flr
            self.acel = 0
        else:
            self.acel -= self.g

        if self.y > self.scr_w - 32:
            self.y = self.scr_w - 32
            self.acel = 0

        if self.acel > self.ms:
            self.acel = self.ms
        if self.acel < -self.ms:
            self.acel = -self.ms

  #badguy
        if self.dead == False:
            self.etimer += 0.1
            self.ex -= self.etimer
            if self.ex < 0:
                self.ex = self.scr_h
                self.rand = random.randrange(1,8)
                self.box_h = 32 * self.rand
                self.box_2 = 32 * (10 - self.rand - 4)
                self.dead = False
                self.etimer = 0
                self.col += 1

  #Collisions
        if self.ex > 100 - 32 and self.ex < 100 + 32:
            if self.y < self.box_h + self.flr:
                self.dead = True

            if self.y > self.scr_w - self.box_2 - self.m:
                self.dead = True

            if self.dead == False:
                self.pts += 50

        if self.dead == True:
            self.etimer = 0


    def touch_began(self, touch):
        self.grav = not self.grav

    def touch_moved(self, touch):
        pass

    def touch_ended(self, touch):
        pass

run(MyScene())

So, I'm making a gravity guy clone (gravity block), however when I added the game gravity an rise from the other game. It doesn't seem to work. Could someone please help with that? It's either something small that slipped past my mind or it's something massive that right in front of me, but I just can't seem to figure it out.

Thanks in advance!!

Here's the code to gravity block:

from scene import *
from random import random

class MyScene (Scene):
    def setup(self):
        self.x=70
        self.y=224
        self.g = 0.5 #gravity
        self.acel = 0
        self.grav = True


    def draw(self):
        background(0, 0.5, 1.0)
        fill(1, 1, 1)
        self.player=rect(self.x,self.y,32,32)
        fill(0,0,0)
        self.roof=rect(self.x-70,self.y+75,700,32)
        self.floor=rect(self.x-70,self.y-75,700,32)
        if not self.grav:
            self.acel += 1


    def touch_began(self, touch):
        self.grav = not self.grav

    def touch_moved(self, touch):
        pass

    def touch_ended(self, touch):
        pass

run(MyScene())
JonB

this is probably better handled in a github repo. it is not particularly convenient to edit code in forum posts.

ccc

Agreed. Lately there are too many long code blocks on the forum which makes it a chore to remain up to date. It would be better to put long code blocks in GitHub repos where we can send pull requests rather than in the forum where the code remains uneditable.

ccc2

See https://github.com/Pythonistapro777 for code updates.

Webmaster4o

Another tip is to only post relevant bits of code. It might do you good for helpful question posting to ask questions on StackOverflow. They're really strict about asking clearly there. It really taught me how to ask and answer questions productively.
How do I ask a good question?

Phuket2

@ccc and @jonb, can understand your feelings about the long code posts. But a lot of us just don't understand how to get the while github integration working with what we need to. Not entirely our fault. Only so much can be discussed here. So remains a bit of a black art. I find it so frustrating, but I accept it for what it is. Of course I would like to ask a ton of questions and have a simple way so all my code going both ways. But my hands are tied.