import pygame
import random
pygame.init()
screen = pygame.display.set_mode([500, 500])
RED = (255, 0, 0)
GREEN =(0,255, 0)
BLUE = (0,0,255)
x1 = 0
y1 = 0
speedX = 1
speedY = 1
counter = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if x1 > 475:
speedX = random.randint(1,5)
speedX = -speedX
if x1 < 0: speedX = -speedX
if y1 > 475:
speedY = random.randint(1,5)
speedY = -speedY
if y1 < 0: speedY = -speedY
counter+=1
if counter < 10000:
pygame.draw.rect(screen,RED, (x1,y1, 25, 25))
pygame.draw.rect(screen,GREEN, (x1+10,y1+10, 25, 25))
pygame.draw.rect(screen,BLUE, (x1+20,y1+20, 25, 25))
pygame.display.flip()
x1 = x1 + speedX
y1 = y1 + speedY
pygame.quit()
|