import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
x = 200
y = 200
width = 10
height = 10
speed = 10
run = True
while run:
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
x = event.pos[0]
y = event.pos[1]
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > 0:
x -= speed
if keys[pygame.K_RIGHT] and x < 500 - width:
x += speed
if keys[pygame.K_UP] and y > 0:
y -= speed
if keys[pygame.K_DOWN] and y < 500 - height:
y += speed
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()
|