import pygame
import random
import time
pygame.init()
screen = pygame.display.set_mode((600, 500))
running=1
x,y=400,440
s=0
r=0
v_y=5
f=0
myfont1 = pygame.font.Font(None,100)
while running:
if r==600:
r=0
if s%600==0:
m = random.randint(0, 60)
n = random.randint(0, 20)
a = random.randint(0, 60)
b = random.randint(0, 20)
c = random.randint(40, 120)
d = random.randint(40, 120)
m*=5
n*=5
a*=5
b*=5
c*=5
d*=5
for event in pygame.event.get():
if event.type == pygame.QUIT:
running=0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
f=1
v_y=-v_y
if y<40+n:
if x+20==c-r:
x-=1
if y+20>460-b:
if x+20==d-r:
x-=1
if x+20>c-r and x<c-r+m:
if y==40+n:
f=0
if x+20>d-r and x<d-r+a:
if y==440-b:
f=0
if y==40+n:
if x>=c-r+m:
f=1
if y==440-b:
if x>=d-r+a:
f=1
if f==1:
y+=v_y
if y==40 or y==440:
f=0
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255,0,0), (x,y,20,20), 0)
pygame.draw.rect(screen, (255,255,255), (0,40,600,1), 0)
pygame.draw.rect(screen, (255,255,255), (0,460,600,1), 0)
pygame.draw.rect(screen, (255,255,255), (c-r,40,m,n), 0)
pygame.draw.rect(screen, (255,255,255), (d-r,460,a,-b), 0)
pygame.display.update()
s+=1
r+=1
if x+20==0:
screen.fill((0, 0, 0))
text_image1 = myfont1.render("游戏结束", True, (255,0,0))
screen.blit(text_image1, (175,200))
pygame.display.update()
time.sleep(10)
running = False
time.sleep(0.000000000001)