I can’t get my player character to load in on the screen :”)
I’m trying to make a crossy road esque game, but currently the player character doesn’t show up on the screen :/
I’ve tried moving pygame.display.flip() around, but that doesn’t seem to work either :’)
Does anyone know what might be wrong with my code?
I’m pretty new to using pygame, so I’m still figuring out the basics
Here’s what I have now:
` import pygame
import random
pygame.init()
screen = pygame.display.set_mode((400,640)) #(width, height)
pygame.display.set_caption('Chemin de traverse: édition olympique') #gives pygame window a name
clock = pygame.time.Clock()
game_active = True
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
player_walk_1 = pygame.image.load('venv/graphics/player1.png').convert_alpha()
player_walk_2 = pygame.image.load('venv/graphics/player2.png').convert_alpha()
self.player_walk = [player_walk_1, player_walk_2]
self.player_index = 0
self.image = self.player_walk[self.player_index]
self.rect = self.image.get_rect(midbottom = (200,630))
def player_input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.rect.y -= 40
def animation_state(self):
self.player_index += 0.5
if self.player_index >= len(self.player_walk): self.player_index = 0
self.image = self.player_walk[int(self.player_index)]
def update(self):
self.player_input()
self.animation_state()
def terrain_generator(x,y):
terrain = random.choice(['grass', 'water', 'road', 'road'])
if terrain == 'grass':
grass = pygame.draw.rect(screen,'#B5ED5D',pygame.Rect((x,y,400,40)))
if terrain == 'water':
water = pygame.draw.rect(screen,'#85D5F8',pygame.Rect((x,y,400,40)))
if terrain == 'road':
road = pygame.draw.rect(screen,'#535763',pygame.Rect((x,y,400,40)))
return(terrain)
#def obstacle_generator(terrain):
#if terrain == 'grass':
# draw trees
#if terrain == 'water':
# draw lilypads + moving logs
#if terrain == 'road':
# draw vehicles
# groups
player = pygame.sprite.GroupSingle()
player.add(Player())
x = 0
y = 640
for y in [640,600,560,520,480]:
pygame.draw.rect(screen, '#B5ED5D', pygame.Rect((x, y, 400, 40)))
pygame.display.flip()
while True and y > -40000:
y -= 40
terrain = terrain_generator(x,y)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit() # closes code entirely
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
exit() # closes code entire
if game_active:
player.draw(screen)
player.update()
pygame.display.flip()
pygame.display.update() # updates anything drawn in while loop onto display surface
clock.tick(60) # tells while loop to not run faster than 60 loops/second `
user25391828 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.