Almost done with the game, I just need help with when self.player makes contacts with self.obstacle, self.dead = True. I tried intersects() but it didn't work.
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 = 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)
self.player=rect(self.x-40,self.y,32,32)
# Touch input:
if not self.grav:
self.acel += 1
self.obstacle=rect(self.ex,self.ey,32,self.box_h)
if self.dead == False:
self.etimer += 0.1
self.ex -= self.etimer
if self.ex < -100:
self.ex = self.scr_h
self.rand = random.randrange(1,6)
if self.rand==1 or self.rand==3:
self.ex = self.scr_h - 32
self.ex=self.ex+80
if self.rand==2 or self.rand==4:
self.ey = self.flr
self.box_2 = 0
self.ey=self.ey+86
self.box_2=self.box_2--32
if self.rand==5 or self.rand==6:
self.ex = self.scr_h - 32
self.ey = self.flr
self.box_2 = 0
self.ex=self.ex+80
self.ey=self.ey+43
self.box_2=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
def touch_began(self, touch):
self.grav = not self.grav
run(MyScene(), LANDSCAPE)