How to make my pygame tron game draw the players?
Question:
My tron game that I am trying to make won’t run the actual characters. I’ve created a class for both player one and player two, and in the while loop I have put a the draw command, but it still is not running the game. Any tips or solutions? Sorry for the lack of comments in the code.
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()
Answers:
You have to update the display with either pygame.display.update()
or pygame.display.flip()
:
done = False
while not done:
# [...]
screen.fill(black)
p1.draw()
p1.move()
p2.draw()
p2.move()
pygame.display.update()
My tron game that I am trying to make won’t run the actual characters. I’ve created a class for both player one and player two, and in the while loop I have put a the draw command, but it still is not running the game. Any tips or solutions? Sorry for the lack of comments in the code.
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()
You have to update the display with either pygame.display.update()
or pygame.display.flip()
:
done = False
while not done:
# [...]
screen.fill(black)
p1.draw()
p1.move()
p2.draw()
p2.move()
pygame.display.update()