import pygame
import random
import sys
FPS = 60
W = 500
H = 500
WHITE = (255, 255, 255)
BLUE = (0, 70, 225)
sc = pygame.display.set_mode((W, H))
clock = pygame.time.Clock()
sc.fill(WHITE)
x = 10
y = 10
surf = pygame.Surface((10, 10))
surf.fill((255, 0, 0))
sc.fill((200, 255, 200))
direction = 0
while 1:
for i in pygame.event.get():
if i.type == pygame.QUIT:
sys.exit()
elif i.type == pygame.KEYDOWN:
if i.key == pygame.K_RIGHT:
direction = 0
elif i.key == pygame.K_LEFT:
direction = 1
elif i.key == pygame.K_DOWN:
direction = 2
elif i.key == pygame.K_UP:
direction = 3
if direction == 0: x = x + 10
if direction == 1: x = x - 10
if direction == 2: y = y + 10
if direction == 3: y = y - 10
if (x < 10):
x = 400
y = random.randint(10,400)
if (x > 400):
x = 10
y = random.randint(10,400)
if (y < 10):
y = 400
x = random.randint(10,400)
if (y > 400):
y = 10
x = random.randint(10,400)
rect = pygame.Rect((x, y, 0, 0))
sc.blit(surf, rect)
pygame.display.update()
clock.tick(FPS)
|