Python 如何让我的Pytron游戏吸引玩家?
我正在制作的tron游戏不会运行实际的角色。我已经为玩家一和玩家二创建了一个类,在while循环中我在draw命令中添加了一个类,但是它仍然没有运行游戏。有什么建议或解决方案吗?很抱歉代码中没有注释Python 如何让我的Pytron游戏吸引玩家?,python,pygame,Python,Pygame,我正在制作的tron游戏不会运行实际的角色。我已经为玩家一和玩家二创建了一个类,在while循环中我在draw命令中添加了一个类,但是它仍然没有运行游戏。有什么建议或解决方案吗?很抱歉代码中没有注释 import pygame pygame.init() black = 0,0,0 #Create black for background P1_Color = 0,0,225 #Create blue for player 1 P2_Color = 255,0,0 #Create red fo
import pygame
pygame.init()
black = 0,0,0 #Create black for background
P1_Color = 0,0,225 #Create blue for player 1
P2_Color = 255,0,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, self.y - 1, 2, 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, 0, (2, 0), P1_Color, (500,0)) # creates p1
p2 = Player(50, 50, (-2, 0), 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()
您必须使用或更新显示:
done=False
虽然没有这样做:
# [...]
屏幕填充(黑色)
p1.绘图()
p1.移动()
p2.绘图()
p2.move()
pygame.display.update()