我创建了一个类似蛇的游戏,用户在其中移动精灵,精灵留下轨迹。如果用户遇到了他创建的足迹,我希望游戏结束,而玩家则输了。
随着精灵移动,它将在黑色背景上产生白色痕迹。如果我可以检测到用户控制的精灵会击中白色区域之一(其“尾巴”),则可以创建一个if
语句,导致游戏失败。
我使用[Surface.get_at()] [1]函数获取精灵将移动到的空间的颜色,并检查该颜色是否与背景颜色相同。如果它与背景颜色不同,则意味着它是精灵的尾巴,游戏以失败告终。
代码如下:
if screen.get_at(player.peek_next()) != BACKGROUND:
done=True
但是,一旦我运行游戏,我就可以直线行驶约两秒钟,然后再执行“ if语句”,然后游戏结束。我不知道为什么它认为“下一个”位置是!=BACKROUND
。我试图降低时钟速度,以便每秒记录较少的帧,但这并不能阻止它关闭,只会延迟它。
代码如下:
import pygame
import os
import time
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BACKGROUND = (100, 30, 100)
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([20, 20])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.change_x = 0
self.change_y = 0
def peek_next(self):
return (self.rect.x+self.change_x, self.rect.y+self.change_y)
def changespeed(self, x, y):
self.change_x += x
self.change_y += y
def update(self):
self.rect.x += self.change_x
self.rect.y += self.change_y
pygame.init()
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption('The Etch-a-Sketch Game')
screen.fill(BACKGROUND)
myfont = pygame.font.SysFont('Times', 20)
textsurface = myfont.render('This is the Etch-a-Sketch Game', False, (255, 255, 255))
screen.blit(textsurface,(0,0))
myfont = pygame.font.SysFont('Times', 15)
textsurface = myfont.render('Feel free to draw, but if you cross your own path, you will die.', False, (255, 255, 255))
screen.blit(textsurface,(0,20))
player = Player(400, 300)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, -3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, 3)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(-3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, 3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, -3)
if screen.get_at(player.peek_next()) != BACKGROUND:
done=True
player.update()
all_sprites_list.draw(screen)
pygame.display.flip()
clock.tick(250)
pygame.quit ()
答案 0 :(得分:2)
有2个问题。
首先,在计算碰撞测试的位置时,必须考虑播放器的大小(白色矩形)。测试矩形旁边的“下一个”:
def peek_next(self):
if self.change_x > 0:
return (self.rect.right+1, self.rect.centery)
elif self.change_x < 0:
return (self.rect.left-1, self.rect.centery)
elif self.change_y > 0:
return (self.rect.centerx, self.rect.bottom+1)
elif self.change_y < 0:
return (self.rect.centerx, self.rect.top-1)
return (self.rect.x, self.rect.y)
如果位置没有变化,则进行测试是没有意义的,因为测试位置就是当前位置,这将始终导致“碰撞”。如果满足player.change_x != 0 or player.change_y != 0
,则仅允许进行碰撞测试。
进一步的测试必须在事件循环之外进行,注意是否按住了键,“玩家”仍然在移动,并且您还必须检查碰撞情况:
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, -3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, 3)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(-3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, 3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, -3)
# <----
if (player.change_x != 0 or player.change_y != 0) and screen.get_at(player.peek_next()) != BACKGROUND:
done=True