I’ve been creating a flappy bird style game. I’m new to programming, and I can’t get the bird to pipe collision to function. It seems as if the bbox on everything is massive. Any help?
Forum Archive
Collision Help
I’ve looked at the code and I can’t figure out a way to get it to work. I know the code could use a lot of work but can you run it and see if you could get the pipe collision to work? There’s a code at the bottom with a note.
from scene import *
import sound
import random
import math
A = Action
class MyScene (Scene):
def setup(self):
self.game_running = False
self.y = 600
self.background_color = '#4a1919'
self.pipes = []
self.score = 0
self.score_label = LabelNode(text='score: 0')
self.score_label.anchor_point = (0, 0)
self.score_label.position = (20, 760)
self.score_label.font = ('Chalkboard SE', 30)
self.frame_counter = 0
self.player = SpriteNode('Untitled 2.PNG')
self.player.position = (100, self.y)
self.player.scale = 1.5
self.ground = Node(parent=self)
x = 0
while x <= self.size.w + 64:
tile = SpriteNode('plf:Ground_GrassHalf_mid', position=(x, 0))
x += 64
self.ground.add_child(tile)
self.loading_screen = SpriteNode('Screen.PNG')
self.loading_screen.position = (550, 450)
self.loading_screen.scale = 1.5
self.add_child(self.loading_screen)
self.play_button = SpriteNode('Untitled 3.PNG')
self.play_button.position = (550, 130)
self.add_child(self.play_button)
def update(self):
for touch in self.touches.values():
if touch.location in self.play_button.bbox:
self.play_button.scale = 1.3
self.game_running = True
if self.game_running == True:
self.add_child(self.score_label)
self.add_child(self.player)
self.background_color = '#49c9dc'
self.loading_screen.remove_from_parent()
self.play_button.remove_from_parent()
if self.player.position.y == 400:
self.player.position = (self.player.position.x, self.player.position.y - 1)
for touch in self.touches.values():
self.player.position = (self.player.position.x, self.player.position.y + 25)
self.player.rotation = 101
else:
self.player.postion = (self.player.position.x, self.player.position.y - 10)
if self.player.position.y > 400:
self.player.position = (self.player.position.x, self.player.position.y - 9)
elif self.player.position.y < 400:
self.player.position = (self.player.position.x, self.player.position.y - 10)
self.player.rotation = 100
elif self.player.position.y < 300:
self.player.position = (self.player.position.x, self.player.position.y - 11)
elif self.player.position.y < 200:
self.player.position = (self.player.position.x, self.player.position.y - 12)
elif self.player.position.y < 100:
self.player.position = (self.player.position.x, self.player.position.y - 13)
self.frame_counter += 1
if self.frame_counter >= 220:
self.frame_counter = 0
new_pipe = SpriteNode('Untitled.PNG')
new_pipe.scale = 2.5
new_pipe.position = (1300,random.randint(250, 600))
self.add_child(new_pipe)
self.pipes.append(new_pipe)
for new_pipe in (self.pipes):
new_pipe.position = (new_pipe.position.x - 2, new_pipe.position.y)
if new_pipe.position.x < -150:
new_pipe.remove_from_parent()
self.pipes.remove(new_pipe)
for new_pipe in (self.pipes):
if new_pipe.position.x == 130:
sound.play_effect('arcade:Coin_5')
self.score += 1
self.score_label.text = "score: " + str(self.score)
this is the part that keep messing up
if new_pipe.bbox.intersects(self.player.bbox) and new_pipe.position.x <= 130:
exit()
if name == 'main':
run(MyScene(), show_fps=False)
@FLAME130
Yes sir just give me a bit to look over everything
Thank you
One problem I see:
if self.game_running == True:
self.add_child(self.score_label)
self.add_child(self.player)
This code gets called every 1/60th of a second. You shouldn't be adding player as a child each frame -- it should already be added once.
@FLAME130 what isn't working? Is it exiting immediately?
Where do you define exit()?
Have you tried printing the bbox's?
Also, maybe first try with ShapeNodes for player and pipes -- sometimes if your image is not prepared right, the bbox might be larger than you'd expect, for instance if the icon has a large clear border
Oh, okay thank you. I didn’t want it to exit I just set it to that so I can see the change immediately. Do you have any examples of shape nodes?
@FLAME130 said:
Oh, okay thank you. I didn’t want it to exit I just set it to that so I can see the change immediately. Do you have any examples of shape nodes?
from scene import *
import sound
import random
import math
def Circle(pos, color, p):
n = ShapeNode(parent=p)
n.fill_color = 'yellow'
n.stroke_color = 'black'
n.line_width = 5.0
n.position = p.size/2
n.path = ui.Path.oval(0, 0, 100, 100)
return n
class MyScene (Scene):
def setup(self):
self.animated = Circle(self.size/2, 'yellow', self)
self.animated.run_action(
Action.repeat(
Action.sequence(
Action.move_by(-100, 0, 1),
Action.move_by(100, 0, 1),
Action.move_by(100, 0, 1),
Action.move_by(-100, 0, 1),
Action.move_by(0, -100, 1),
Action.move_by(0, 100, 1),
Action.move_by(0, 100, 1),
Action.move_by(0, -100, 1)), -1))
if __name__ == '__main__':
run(MyScene(), show_fps=False)