Forum Archive

[SOLVED!] Gravity guy clone (GRAVITY BLOCK)

Pythonistapro777

I'm finished with the game. Literally have just one more thing to do. I've finished the gravity block game, collision detect and speed increase works great. I need help with making it so at 5000 points another random obstacle appears. If it can't be done, please let me know, if it can please give me some pointers on how.

Thanks in advance!

Here's the code:

#coding: utf-8
from scene import *
import random

class MyScene (Scene):
    def setup(self):
        self.x = 100
        self.y = 0
        self.scr_h = 510
        self.scr_w = 218
        self.flr = 101 #floor
        self.g = 0.5 #gravity
        self.etimer = 0.0
        self.acel = 0
        self.grav = True
        self.lw = self.scr_h
        self.ms = 6 #max speed
        self.n = 6
        self.ex = self.scr_h - 32
        self.ey = self.flr
        self.box_h = 32
        self.box_2 = 0
        self.button = Button(Rect(self.size.w/2-250, self.size.h/2--58, 500, 35))
        self.button.background = Color(0,0.05,0.2)
        self.button.stroke = Color(0,0.05,0.2)
        self.button1 = Button(Rect(self.size.w/2-250, self.size.h/2-92, 500, 35))
        self.button1.background = Color(0,0.05,0.2)
        self.button1.stroke = Color(0,0.05,0.2)

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

  #collisions
        self.dead = False
        pass


    def draw(self):
        background(0, 0.5, 1)
        stroke(255,0,0)
        self.button.draw()
        self.button1.draw()

  #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

        tint(1,1,1)
        text('Points: {}'.format(self.pts), x=self.size.w/3, y=self.size.h/3.5*3, font_size=40)
        text('You can click the ❎ at the\ntop right of the screen to exit.', x=self.size.w/2, y=self.size.h/11, font_size=20)

  #character
        self.player=Rect(self.x-40,self.y,32,32)
        fill(1,1,1)
        rect(*self.player)

  # Touch input:
        if not self.grav:
            self.acel += 1

        self.obstacle=Rect(self.ex,self.ey,32,self.box_h)
        fill(1,1,1)
        rect(*self.obstacle)

        if self.pts < 500:
            self.spd =  0.1

        if self.pts > 500 and self.pts < 1000:
            self.spd = 0.15

        if self.pts > 1000 and self.pts < 1500:
            self.spd = 0.2

        if self.pts > 1500 and self.pts < 2000:
            self.spd =  0.25

        if self.pts > 2000 and self.pts < 2500:
            self.spd =  0.3

        if self.pts > 2500 and self.pts < 3000:
            self.spd =  0.35

        if self.pts > 3000 and self.pts < 3500:
            self.spd =  0.4

        if self.pts > 3500 and self.pts < 4000:
            self.spd =  0.45

        if self.pts > 4000 and self.pts < 4500:
            self.spd =  0.5

        if self.pts > 4500 and self.pts < 5000:
            self.spd =  0.55

        if self.pts > 5500 and self.pts < 6000:
            self.spd =  0.1

        if self.pts > 6000 and self.pts < 6500:
            self.spd =  0.15

        if self.pts > 6500 and self.pts < 6000:
            self.spd =  0.2

        if self.pts > 6000 and self.pts < 6500:
            self.spd =  0.25

        if self.pts > 6500 and self.pts < 7000:
            self.spd =  0.3

        if self.pts > 7000 and self.pts < 7500:
            self.spd =  0.35

        if self.pts > 7500 and self.pts < 8000:
            self.spd =  0.4

        if self.pts > 8000 and self.pts < 8500:
            self.spd =  0.45

        if self.pts > 8500 and self.pts < 9000:
            self.spd =  0.5

        if self.pts > 9000 and self.pts < 9500:
            self.spd =  0.55

        if self.pts > 9500 and self.pts < 10000:
            self.spd =  0.6

        if self.dead == False:
            self.etimer += self.spd
            self.ex -= self.etimer
            if self.ex < 0:
                self.ex = self.scr_h
                self.rand = random.randint(1,3)
                if self.rand==1:
                    self.scr_h = 510
                    self.scr_w = 218
                    self.ex = self.scr_h - 32
                    self.ey = self.flr
                    self.box_h = 32
                    self.box_2 = 0
                if self.rand==2:
                    self.scr_h = 510
                    self.scr_w = 218
                    self.ex = self.scr_h - 32
                    self.ey = self.flr
                    self.box_h = 32
                    self.box_2 = 0
                    self.ey=self.ey+86
                    self.box_2--32
                if self.rand==3:
                    self.scr_h = 510
                    self.scr_w = 218
                    self.ex = self.scr_h - 32
                    self.ey = self.flr
                    self.box_h = 32
                    self.box_2 = 0
                    self.ey=self.ey+43
                    self.box_2--32

                self.dead = False
                self.etimer = 0

  # 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


        if self.player.intersects(self.obstacle):
            self.dead=True

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

run(MyScene(), LANDSCAPE)
Webmaster4o

It doesn't quite work on an iPad size screen I don't think

Pythonistapro777

Damn it. I did it for my iphone. I'll have to work on that later. 😪

Webmaster4o

:) it's ok, you designed it for iPhone. Just that I won't be able to test it too easily. It shouldn't be too hard to fix.

ccc

Nice improvements with the rects!

A few points...
* Aovid asking if dead == True: or if dead == False: instead ask if dead: or if not dead:
* You can get closer to what you want if you change if self.pts > 500 and self.pts < 1000: to elif 500 <= self.pts < 1000: because the current code does nothing if self.pts exactly equals 500 or exactly equals 1000, etc.
* I would move that speed calculation logic to a separate function called get_speed_for_points(points) that returns the appropriate speed value.
* On line 25, you write self.size.h/2--58. In Python (as in English) a double negative is a positive so this is the same thing as self.size.h/2 + 58.
* Double negatives are also on lines 156 and 165.
* A more portable way to set the screen width and screen height would be self.scr_w = self.bounds.w ; self.scr_h = self.bounds.h.
* Change self.ms to self.max_speed ;-)