猫史档案馆


python好玩小游戏:键盘躲避陷阱

用户:小苔藓小苔藓查看:0 回复:0 评论:0 创建时间:2022-09-27T19:19:40


import pygame #导入pygame
import random #导入随机数
import sys #道路系统模块
pygame.init() #初始化
screen=pygame.display.set_mode((500,500))
pygame.display.set_caption('躲避陷阱')
r1=pygame.Rect(0,0,100,100) #创建矩形
r=pygame.Rect(0,0,100,10) #创建矩形
#主程序
x0=250 #中间位置
y0=500
x1=random.randint(20,450)
y1=0
r1.center=x1,y1
q=0
while 1:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit() #退出pygame
sys.exit() #退出

if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
x0=x0-0.3
if x0<0:
x0=0
if event.key==pygame.K_RIGHT:
x0=x0+0.3
if x0>500:
x0=500
y1=y1+0.2
if y1>500:
y1=0
x1=random.randint(20,450)
r1.center=x1,y1
screen.fill((0,0,0))
#获取鼠标坐标
x,y=pygame.mouse.get_pos() #获取坐标
r.center=x0,y0
pygame.draw.rect(screen,(220,2,10),r1) #显示矩形
pygame.draw.rect(screen,(220,2,100),r) #显示矩形
pygame.draw.circle(screen,(220,220,0),(x,y),20)#绘制圆形
pygame.display.set_caption('躲避障碍'+str(x)+' '+str(y)+' '+str(round(q))+'秒')
q=q+0.001
if r1.left<x0<r1.right and r1.top<y0<r1.bottom:
q=round(q)
print("你坚持了",q,'秒')
pygame.quit()
sys.exit()
pygame.display.update() #刷新


回复

上一页1 页 / 共 0下一页