我试图重新创建一个游戏,但我所需要的只是创建一个游戏结束功能,当玩家碰撞或无法跳过岩石时发生。我试图进行检查碰撞但玩家正在经过。当玩家与摇滚相撞时,我应该做什么或改变什么才能在屏幕上显示游戏?这是我的示例代码:
#--- Player Definition
class Player(pygame.sprite.Sprite):
#- Constructor
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.playing = False
self.image = pygame.image.load("bcquestchar.png")
self.rect = self.image.get_rect()
self.color = BLUE
self.rect.x = 50
self.rect.y = 210
self.goalY= 450
self.gameover = False
#- Allows Player to Jump
def jump(self):
self.goalY -= 25
#- Imports & Displays player_image
def draw(self, screen):
screen.blit(player_image, [self.rect.x, self.rect.y])
#--- Rock definition
class Rock(pygame.sprite.Sprite):
#- Constructor
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("bcrocks.png")
self.rect = self.image.get_rect()
self.rect.x = random.randrange (0, 200)
self.rect.y = 335
#- Moves Rock
def move(self):
self.rect.x -= 3
#- Imports & Displays rock_image
def draw(self, screen):
screen.blit(self.image, [self.rect.x + 700, self.rect.y])
# Loop until user closes pygame
done = False
# How fast the screen updates
clock = pygame.time.Clock()
# Creates player
rock = Rock()
player = Player()
obstacles = pygame.sprite.Group()
obstacles.add(rock)
if pygame.sprite.spritecollide(player, obstacles, False):
player.gameover = True
#----- Main Program Loop -----#
while not done:
#movesound.play()
#- Calls in draw_background function
draw_background (screen, 0, 0)
#- Tracks Events
speed = 1
#--- Main Event Loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = False
#- User is playing when M1 is clicked
elif event.type == pygame.MOUSEBUTTONDOWN:
player.playing = True
#- User jumps when playing and has not lost
if(player.gameover == False and player.playing == True):
player.jump()
#- Is the player playing?
if(player.playing == True):
# Jump up when the up arrow is pressed
if event.type == pygame.MOUSEBUTTONDOWN:
currently_jumping = True
going_up = True
#hopsound.play()
if currently_jumping == True:
if player.rect.y < 188:
going_up = False
elif going_up == False and player.rect.y > 215:
currently_jumping = False
if going_up:
player.rect.y -= 2
elif going_up == False:
player.rect.y += 2
elif currently_jumping == False:
if(player.rect.y > 600 or player.rect.y < 0):
player.gameover = True
if(player.rect.y < 210):#player.goalY):
player_moving_up = False
elif(player.rect.y > 210):#player.goalY):
player_moving_up = True
if player_moving_up == True or player_moving_up == True:
player.rect.y += 1
player.rect.y -= 1
player.goalY += 3
if(timer3 > 120):
timer3 = 0
if(player.playing == True and player.gameover == False):
score += 30
#--- Draw and Move Rocks
for rock in obstacles:
rock.draw(screen)
if(player.gameover == False):
rock.move()
rock.draw(screen)
#rock.checkCollision(player.x, player.y)
if pygame.sprite.spritecollide(player, obstacles, False):
obstacles.remove(rock)
#----- Gameover
if(player.gameover == True):
player.goalY = 600
font = pygame.font.SysFont('Ariel', 50, True, False)
text = font.render("Game Over", True, WHITE)
screen.blit(text, [150, 100])
代码编辑
答案 0 :(得分:0)
将Rock()
设为pygame.sprite.Sprite
课程,如果可能,请加入pygame.sprite.Group()
。在这种情况下,您可以使用if pygame.sprite.spritecollide()
检查检测:
rock = Rock()
player = Player()
obstacles = pygame.sprite.Group()
obsatcles.add(rock)
if pygame.sprite.spritecollide(player, obstacles, False):
pass #Do something
使用pygame.sprite.Group()
的好处是你可以使用它来帮助检测与玩家的碰撞和任何其他障碍。假设您在obstacles
中有三个项目,它们都是rock
的:
for rock in obstacles:
if pygame.sprite.spritecollide(player, obstacles, False):
obstacles.remove(rock) #Removes the obstacle
将组放在精灵类之外和while循环之前。
对于摇滚乐队,请在你的init函数中使用它:
self.rect = self.image.get_rect()
所以它会是:
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("filename.png")
self.rect = pygame.image.get_rect()
然后将此添加到您的while循环中:
for rock in obstacles:
rock.move()
rock.draw()
你的最后一堂课(不包括最后两个职能)将是:
class Rock(pygames.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("bcrocks.png")
self.rect = self.image.get_rect()
self.rect.x = random.randrange (0, 200)
self.rect.y = 335