0

I'm currently trying to build a game through vscode using pygame lib. My code to move character around with keyboard arrow keys won't apply to my module.

  • My module won't close even though I click exit button or esc. Any thoughts why its not working?
import pygame
import os

pygame.init() 


screen_width = 480 
screen_height = 640 
screen = pygame.display.set_mode((screen_width,screen_height))


pygame.display.set_caption("June's Game")


background = pygame.image.load("D:/Python/pygame_basic/background.png")


character = pygame.image.load("D:/Python/pygame_basic/character.png")
character_size = character.get_rect().size 
character_width = character_size[0] 
character_height = character_size[1] 
character_x_position = (screen_width / 2) - (character_width / 2) 
character_y_position = screen_height - character_height


to_x = 0
to_y = 0


running = True #게임이 진행중인가

while running:
    for event in pygame.event.get(): 
        if event == pygame.QUIT: 
            running = False
        if event == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                to_x -= 5
            elif event.key == pygame.K_RIGHT:
                to_x += 5
            elif event.key == pygame.K_UP:
                to_y -= 5
            elif event.key == pygame.K_DOWN:
                to_y += 5

        if event == pygame.KEYUP: 
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                to_x = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                to_y = 0
    character_x_position += to_x
    character_y_position += to_y


    #screen.fill((0,0,255))
    screen.blit(background, (0,0)) 
    screen.blit(character, (character_x_position,character_y_position))
    pygame.display.update()

pygame.quit()

i coudln't get it to work

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
June Lim
  • 1
  • 3

1 Answers1

1
  1. event is an object. You have to get the type of the event, e.g.:

    if event == pygame.KEYDOWN

    if event.type == pygame.KEYDOWN:
    
  2. You have to limit the frames per second to limit CPU usage with pygame.time.Clock.tick and to control the speed of the game. Otherwise, the player moves much too fast and immediately disappears from the screen. The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time.

clock = pygame.time.Clock()
running = True 
while running:
    clock.tick(100)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                to_x -= 5
            elif event.key == pygame.K_RIGHT:
                to_x += 5
            elif event.key == pygame.K_UP:
                to_y -= 5
            elif event.key == pygame.K_DOWN:
                to_y += 5

        if event.type == pygame.KEYUP: 
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                to_x = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                to_y = 0
    character_x_position += to_x
    character_y_position += to_y

    #screen.fill((0,0,255))
    screen.blit(background, (0,0)) 
    screen.blit(character, (character_x_position,character_y_position))
    pygame.display.update()

However, there is a better method to move an object when a key is held down (See How can I make a sprite move when key is held down):

to_x = 0
to_y = 0
speed = 5

clock = pygame.time.Clock()
running = True 
while running:
    clock.tick(100)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running = False
    
    keys = pygame.key.get_pressed()
    to_x = (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
    to_y = (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * speed

    character_x_position += to_x
    character_y_position += to_y

    screen.blit(background, (0,0)) 
    screen.blit(character, (character_x_position,character_y_position))
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174