import pygame
import random
WIDTH = 500
HEIGHT = 500
FPS = 5
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
speedY = 0
speedX = random.randint(0,19)*25
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and speedX > 0:
speedX -= 25
speedY -= 25
if event.key == pygame.K_RIGHT and speedX < 475:
speedX += 25
speedY -= 25
screen.fill(BLACK)
for it_1 in range(0,500,25):
for it_2 in range(0,500,25):
pygame.draw.rect(screen,WHITE, (it_1,it_2,24,24))
pygame.draw.rect(screen,RED, (speedX,speedY,24,24))
pygame.display.flip()
speedY = speedY + 25
if speedY > 475:
speedY = 0
speedX = random.randint(0,19)*25
pygame.quit()
|