如何解决如何让我的 pygame tron 游戏吸引玩家?
我尝试制作的 tron 游戏无法运行实际角色。我为玩家一和玩家二创建了一个类,并且在 while 循环中我放置了一个 draw 命令,但它仍然没有运行游戏。任何提示或解决方案?很抱歉代码中缺少注释。
import pygame
pygame.init()
black = 0,0 #Create black for background
P1_Color = 0,225 #Create blue for player 1
P2_Color = 255,0 #Create red for player 2
class Player:
def __init__(self,x,y,direction,color,position):
self.x = x #X coord
self.y = y #Y coord
self.position = (x,y)
self.direction = direction #direction
self.color = color #color
self.rect = pygame.Rect(100,self.y - 1,2,2) #player rect object
def draw(self):
self.rect = pygame.Rect(100,2) # redefines rect
pygame.draw.rect(screen,self.color,self.rect,0) # draws player onto screen
def move(self):
self.x += self.direction[0] * 2
self.y += self.direction[1] * 2
screen = pygame.display.set_mode((1000,750)) # creates window
pygame.display.set_caption("Tron") # sets window title
clock = pygame.time.Clock()
p1 = Player(50,(2,0),P1_Color,(500,0)) # creates p1
p2 = Player(50,50,(-2,P2_Color,(0,500)) #create p2
done = False
while not done:
for event in pygame.event.get(): # gets all event in last tick
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
# === Player 1 === #
if event.key == pygame.K_w:
p1.direction = (0,-2)
elif event.key == pygame.K_s:
p1.direction = (0,2)
elif event.key == pygame.K_a:
p1.direction = (-2,0)
elif event.key == pygame.K_d:
p1.direction = (2,0)
# === Player 2 === #
if event.key == pygame.K_UP:
p2.direction = (0,-2)
elif event.key == pygame.K_DOWN:
p2.direction = (0,2)
elif event.key == pygame.K_LEFT:
p2.direction = (-2,0)
elif event.key == pygame.K_RIGHT:
p2.direction = (2,0)
screen.fill(black)
p1.draw()
p1.move()
p2.draw()
p2.move()
pygame.quit()
解决方法
您必须使用 pygame.display.update()
或 pygame.display.flip()
更新显示:
done = False
while not done:
# [...]
screen.fill(black)
p1.draw()
p1.move()
p2.draw()
p2.move()
pygame.display.update()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。