illage2
illage2

Reputation: 5

Sprite which is supposed to move with keyboard input does not show up

import pygame
from pygame.locals import *
import sys

pygame.init()

# Creating a Window
pygame.display.set_caption("Hello there")
screen = pygame.display.set_mode((640, 480))

player = pygame.image.load("player.png")
clock = pygame.time.Clock()

# X AND Y VALUES FOR PLAYER
player_x = 32
player_y = 32

#MOVING AROUND
#moving_right = False
#moving_left = False
#moving_up = False
#moving_down = False

#GAME LOOP
moving_right = False
moving_left = False
moving_up = False
moving_down = False
clock.tick(60)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_LEFT:
                moving_left = True
            elif event.key == K_RIGHT:
                moving_right = True
            elif event.key == K_UP:
                moving_up = True
            elif event.key == K_DOWN:
                moving_down = True

    elif event.type == KEYUP:
        if event.key == K_LEFT:
            moving_left = False
        elif event.key == K_RIGHT:
            moving_right = False
        elif event.key == K_UP:
            moving_up = False
        elif event.key == K_DOWN:
            moving_down = False

screen.blit(player, (player_x, player_y))

pygame.display.update()

  #UPDATING PLAYER
player_speed = 15
if moving_up:
    player_y -= player_speed
elif moving_down:
    player_y += player_speed
if moving_left:
    player_x -= player_speed
elif moving_right:
    player_x += player_speed


#CLEAR THE SCREEN OFF SO THERE'S NO TRAIL WHEN THE PLAYER MOVES
screen.fill((0, 0, 0))

I just can't seem to see where I've gone wrong. There's no errors when I run the game, the sptite just doesn't show up on screen. I want to be able to move the sprite while the key is being held down.

Upvotes: 0

Views: 45

Answers (1)

rwflash
rwflash

Reputation: 188

Your code is fine, you just have indenting problems. The While True: loop should look like this:

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_LEFT:
                moving_left = True
            elif event.key == K_RIGHT:
                moving_right = True
            elif event.key == K_UP:
                moving_up = True
            elif event.key == K_DOWN:
                moving_down = True

        elif event.type == KEYUP:
            if event.key == K_LEFT:
                moving_left = False
            elif event.key == K_RIGHT:
                moving_right = False
            elif event.key == K_UP:
                moving_up = False
            elif event.key == K_DOWN:
                moving_down = False

    screen.blit(player, (player_x, player_y))

    pygame.display.update()

      #UPDATING PLAYER
    player_speed = 15
    if moving_up:
        player_y -= player_speed
    elif moving_down:
        player_y += player_speed
    if moving_left:
        player_x -= player_speed
    elif moving_right:
        player_x += player_speed


    #CLEAR THE SCREEN OFF SO THERE'S NO TRAIL WHEN THE PLAYER MOVES
    screen.fill((0, 0, 0))

The idea here is that you need to blit and update the screen in your While loop for every event that occurs.

Upvotes: 1

Related Questions