Forum Archive

[SOLVED!] Help with touch

Pythonistapro777

Is there a way that instaed of pressing and holding to fly, I can tap and it will fly and tap again to fall.

Thanks in advance!

Here's the code:


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.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)
        for touch in self.touches.values():
    #ellipse(touch.location.x - 50, touch.location.y - 50, 100, 100)
            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):
        pass

    def touch_moved(self, touch):
        pass

    def touch_ended(self, touch):
        pass

run(MyScene())
ccc

Check out BashedCrab's games for tons of good ideas... https://gist.github.com/BashedCrab esp. JumpyOctopus http://omz-forums.appspot.com/pythonista/post/5806864235233280

Pythonistapro777

I've looked at his and some other people's but I can't find one that has a code where when you click you can flip gravity (in the game). I need this because I'm making a gravity guy game on pythonista.

Webmaster4o

Try this:

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())
Pythonistapro777

Thanks a lot!