Simple game - Help with beginner problem
I'm trying to move the "ball" using the arrow keys but i can't figure out what i am doing wrong.
When i launch the game from terminal and the window pops up the "ball" is still, arrows does not work.
Can you help me solve this problem?
import pygame
import os
pygame.init()
size = width, height = 750, 422
screen = pygame.display.set_mode(size)
img_path = os.path.join('--folder--', 'ball.png')
background_image = pygame.image.load('--folder/background.png').convert()
pygame.display.set_caption("BallGame")
class Ball(object):
def __init__(self):
self.image = pygame.image.load(img_path)
self.x = 0
self.y = 0
def handle_keys(self):
key = pygame.key.get_pressed()
dist = 5
if key[pygame.K_DOWN]:
self.y += dist # move down
elif key[pygame.K_UP]:
self.y -= dist # move up
if key[pygame.K_RIGHT]:
self.x += dist # move right
elif key[pygame.K_LEFT]:
self.x -= dist # move left
def draw(self, surface):
surface.blit(self.image, (self.x, self.y))
pygame.init()
screen = pygame.display.set_mode((750, 422))
ball = Ball()
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False
ball.handle_keys()
screen.blit(background_image, [0, 0])
ball.draw(screen)
pygame.display.update()
clock.tick(40)
Posted Jul 3, 2018, 9:02 AM UTC
522