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
state = []
for index in range(0,500,1):
state.append(1)
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)
counter = 0
for it_1 in range(0,500,25):
for it_2 in range(0,500,25):
if (state[counter]==1):
pygame.draw.rect(screen,WHITE, (it_1,it_2,24,24))
if (state[counter]==2):
pygame.draw.rect(screen,RED, (it_1,it_2,24,24))
counter = counter + 1
pygame.draw.rect(screen,RED, (speedX,speedY,24,24))
pygame.display.flip()
speedY = speedY + 25
if speedY > 475:
x = speedX//25
state[19*x +(x-1) + 20] = 2
state[19*x +(x-1) + 19] = 2
speedY = 0
speedX = random.randint(0,19)*25
pygame.quit()
|