好吧,所以我需要在两个支柱之间对撞鸟玩家进行碰撞。抱歉,如果代码很复杂,我是编程新手。
在代码中,有两个支柱向左移动并重复。 我做了xpos2,ypos2,xpos,ypos,因为同一时刻有2个支柱。 它们之间的距离为670,因为支柱的图片很大, 因此我将它们绘制得很高或很低-您看不到所有支柱。 我需要在鸟的每个支柱(顶部或底部)之间进行碰撞。 我尝试了colliderect和另一个pygame函数,但没有成功。
谢谢。
代码
import pygame
import random
size = [900, 504]
wait = pygame.time.Clock()
red = (255, 0, 0)
pygame.init()
class game:
def __init__(self):
self.screen = pygame.display.set_mode((size[0], size[1]))
pygame.display.set_caption("Flappy Bird @yuv")
self.background = pygame.image.load(r"d:\Users\Student\Desktop\background.png").convert()
self.top_pillar = pygame.image.load(r"d:\Users\Student\Desktop\top.png").convert()
self.bottom_pillar = pygame.image.load(r"d:\Users\Student\Desktop\bottom.png").convert()
self.done = False
self.xpos = 400 # XPOS OF THE PILLARS
self.xpos2 = 800 #
self.ypos_top = random.randint(-400, -200) # ypos of the pillar - top
self.ypos_bottom = self.ypos_top + 670 # ypos of the pillar - bottom
self.ypos2_top = random.randint(-400, -200) # ypos of the pillar - bottom
self.ypos2_bottom = self.ypos2_top + 670 # ypos of the pillar - bottom
self.score = 0
def pillars(self):
for i in range(5):
self.xpos -= 1
self.xpos2 -= 1
if self.xpos <= 0:
self.xpos = 800
self.ypos_top = random.randint(-400, -200) # ypos of the pillar - top
self.ypos_bottom = self.ypos_top + 670 # ypos of the pillar - bottom
if self.xpos2 <= 0:
self.xpos2 = 800
self.ypos2_top = random.randint(-400, -200) # ypos of the pillar - bottom
self.ypos2_bottom = self.ypos2_top + 670 # ypos of the pillar - bottom
# def collide(self):
## how the hell i do it ! ? ?! ?! ?! ?!
def scoregame(self):
if bird().x > self.xpos or bird().x > self.xpos2:
self.score += 1
myfont = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = myfont.render('score : ' + str(int(self.score / 39)), True, red)
self.screen.blit(textsurface, (0, 0))
def gameover(self):
font = pygame.font.SysFont(None, 55)
text = font.render("Game Over!", False, red)
self.screen.blit(text, [100, 250])
self.done = True
class bird:
def __init__(self):
self.x = 200
self.y = 350
self.bird = pygame.image.load(r"d:\Users\Student\Desktop\player.png").convert_alpha()
def move_bird(self):
wait.tick(20)
for i in range(3):
self.y += 2
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
for i in range(50):
self.y -= 1
def check_obstacle(self):
if self.y > 478 or self.y < -10: # check if the player touch the top or the bottom of the screen
print('3')
Game.gameover()
Game = game()
birdfunc = bird()
while Game.done == False:
Game.screen.blit(Game.background, (0, 0))
Game.screen.blit(birdfunc.bird, (birdfunc.x, birdfunc.y))
birdfunc.move_bird()
birdfunc.check_obstacle()
Game.screen.blit(Game.top_pillar, (
Game.xpos, Game.ypos_bottom)) # ypos -500 is the start of the top between -400 to -200 XXXXX gap- 200
Game.screen.blit(Game.bottom_pillar, (Game.xpos, Game.ypos_top)) # ypos 500 is the start of the bottom
Game.screen.blit(Game.top_pillar, (Game.xpos2, Game.ypos2_bottom)) # ypos -500 is the start of the top between -400 to -200 XXXXX gap- 200
Game.screen.blit(Game.bottom_pillar, (Game.xpos2, Game.ypos2_top)) # ypos 500 is the start of the bottom
Game.scoregame()
Game.pillars()
pygame.display.flip()
while Game.done == True:
Game.screen.blit(Game.background, (0, 0))
Game.gameover()
答案 0 :(得分:0)
您已经非常接近了……我看到您已经编写了与屏幕顶部和底部冲突的代码。您也不需要库就可以在对象之间进行简单的冲突处理,在这种情况下,您可以编写自己的算法。
def check_obstacle(self):
if self.y > 478 or self.y < -10: # check if the player touch the top or the bottom of the screen
print('3')
Game.gameover()
您如何修改此设置以使小鸟与柱子碰撞?
这是我最初的想法:可以将柱的边缘简化为self.y
的不同值,因此,如果小鸟经过柱,则鸟不在柱之间的有效y值范围内(您是否有pillar.x
?),则应调用Game.gameover()
。在纸上绘制图片并在坐标上加上标签可能会帮助您计算数学。