The cube is only moving once.
import time
import pygame
pygame.init()
class Window(object):
def __init__(self, width, height, bg):
self.width = width
self.height = height
self.bg = bg
def create(self):
return pygame.display.set_mode((self.width, self.height))
The problem is somewhere here
class Cube(object):
def __init__(self, surface, x, y, width, height, color):
self.surface = surface
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
def move(self, lead_x_change=0, lead_y_change=0):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -20
elif event.key == pygame.K_RIGHT:
lead_x_change = 20
elif event.key == pygame.K_UP:
lead_y_change = -20
elif event.key == pygame.K_DOWN:
lead_y_change = 20
self.x += lead_x_change
self.y += lead_y_change
def draw(self):
pygame.draw.rect(self.surface, self.color, (self.x, self.y, self.width, self.height))
window = Window(800, 600, (0, 0, 0))
surface = window.create()
head = Cube(surface, 400, 300, 20, 20, (255, 255, 255))
starting gameloop
gameloop = True
while gameloop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
head.move()
surface.fill((0, 0, 0))
head.draw()
pygame.display.update()
The Cube moves, but only one time after a keypress. it should continiously move in one direction though, after pressing the direction. I dont know how to make it move continiously, even watched ton of videos about it and still dont understand.