Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Help needed with collision detect (game) (short code) [SOLVED!]
-
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)
-
In case anyone's wondering, this isn't it. I'm gonna make the still speed up after every 700 points. Also, at 7000 points another obstacle will appear!
-
@Pythonistapro777 I would like to help you here, as I previously had this problem. Where is the obstacle supposed to be coming from to hit the player? Any random point?
-
Why don't you look at the code from your original script, before you had gravity working?
-
@Webmaster4o I tried that but because I'm using a method and mines not as complicated as theirs I can't.
-
Yes @techteej , if the obstacle hits the player from any point.
-
@Pythonistapro777 I'm sorry, just looking at your code first glance is a little confusing.
Most variables should have a full name: for example
'self.ms'
should be'self.max-speed'
or something similar (the-
could be changed to whatever/or have no space at all). Even that YOU know'self.ms'
is max speed - others looking at your code do not. I realize you commented on this, but only having one time comments could get repetitive (looking back and forth). I made the same mistake when I started out.Luckily, Pythonista offers a find and replace option so it can be an easy fix.
-
I created stop_and_go.py using your code as a starting point. It is a different game (go as much as you can but stop before other blocks crash into you) but it shows how you can create several blocks that each have their own frame, velocity, and color as well as how to do collision detection. Nothing as advanced as BashedCrab's games but easy to understand.
-
@ccc I spent all morning trying to extract it from your game, but I just kept getting an error so I have up. Then I say hit tests on the forum but those were way to complicated for me and for people that might want to use me code. Could you please write me a collision test with 2 squares so I can work with that?
-
Super simple collision demo...
Scene has two rects:
- scene.rect() with a lowercase r is a method that draws a rectangle on the screen and returns None
- scene.Rect() with an uppercase R is an object with x, y, w, h
If intersect() is not working for you, make sure that both objects are of type(scene.Rect) with a Capitol R.
# coding: utf-8 import random, scene, sound def random_color(): return scene.Color(random.random(), random.random(), random.random()) class MyScene(scene.Scene): def __init__(self): scene.run(self, scene.LANDSCAPE) def setup(self): self.square1 = scene.Rect(50, 50, 50, 50) self.square2 = scene.Rect(99, 99, 50, 50) def draw(self): scene.background(0, 0.5, 1) scene.fill(*random_color()) scene.rect(*self.square1) scene.fill(*random_color()) scene.rect(*self.square2) if self.square1.intersects(self.square2): sound.play_effect('arcade:Explosion_1') MyScene()