Forum Archive

[SOLVED!] Help with random (game) (short code)

Pythonistapro777

I'm in the finishing stages of making my gravity guy game. However I need help with the obstacles. When the game is run you will see 3 blocks on the side. Those are the obstacles, I need help with making them scroll from right to left (so the player can dodge them), I also need help with making a random obstacle to be picks after one has scrolled. Please let me know if I'm not explicit enough, because I have no idea what to do.

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 = 400
        self.scr_w = 218
        self.flr = 101 #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
        self.ex = self.scr_h - 32
        self.ey = self.flr
        self.etimer = 0.0
        self.box_h = 32
        self.box_2 = 0
        self.m = 32
        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
        fill(1,1,1)
        rect(self.x-40,self.y,32,32)

    #obstacles
        self.obstacle1=rect(self.ex,self.ey,32,self.box_h)
        self.obstacle2=rect(self.ex,self.ey+86,32,self.box_2--32)
        self.obstacle3=rect(self.ex,self.ey+43,32,self.box_2--32)

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


  # 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

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

run(MyScene(), LANDSCAPE)

Thanks in advance!

ccc

You should go back to the Magic8Ball source https://github.com/Pythonistapro777/Magic8Ball/blob/master/Magic8Ball.py and grab the Particle code and make the GravityBlock and the three obstacles be Particles. Each will then have their own position, size, color, direction, etc. This will allow you to simplify the scene.Scene which today has too many confusing responsibilities and too short variable names. Make longer variable names (max_speed instead of ms) so you can think more deeply about you code because you no longer need to translate initials into ideas.

Pythonistapro777

I will give it a try, but first. Is it possible to create a rectangle in setup and then call it in draw(). Kind of like with buttons.

Pythonistapro777

@ccc @Webmaster4o @omz @Phuket2