当前位置: 首页 > 教学资源 > 打字游戏(Pygame Zero )

打字游戏(Pygame Zero )

2022年02月09日 15:49:00 访问量:1162


import random,time
WIDTH =800
HEIGHT = 600
balls = []
score = 0
stime = time.perf_counter()
ltime = 60
win = False
lost = False

def update():
    if win or lost:
        return
    if len(balls)< 5:
        addball()
    updateball()
    counttime()
    checkgame()

def addball():
    ball=Actor("typing_balloon",(random.randint(40,WIDTH-40),HEIGHT-20))
    ball.char=chr(random.randint(65,90))
    balls.append(ball)

def draw():
    screen.fill((255,255,255))
    for ball in balls:
        ball.draw()
        screen.draw.text(ball.char,center=ball.center,color="red")
    screen.draw.text("Score:"+str(score),bottomleft=(10,HEIGHT-10),color="black")
    screen.draw.text("Time:"+str(ltime),bottomleft=(WIDTH-80,HEIGHT-10),color="black")
    if win:
        screen.draw.text("You Win!",center=(WIDTH//2,HEIGHT//2),fontsize=50,color="red")
    elif lost:
        screen.draw.text("You Lost!",center=(WIDTH//2,HEIGHT//2),fontsize=50,color="red")

def updateball():
    global score
    for ball in balls:
        ball.y -= random.randint(1,2)
        if ball.bottom <0 :
            balls.remove(ball)
            score -= 1

def on_key_down(key):
    global score
    for ball in balls:
        if str(key)=="keys."+ball.char:
            score += 2
            sounds.eat.play()
            balls.remove(ball)

def counttime():
    global ltime
    ptime=int(time.perf_counter()-stime)
    ltime=60-ptime

def checkgame():
    global win,lost
    if score>=100:
        win = True
        sounds.win.play()
    if ltime<=0 or score < -10:
        lost = True
        sounds.fail.play()
  • 附件(1个)
  • typing.rar (795kb) 下载
编辑:郑志平
评论区
发表评论

评论仅供会员表达个人看法,并不表明网校同意其观点或证实其描述
教育部 中国现代教育网 不良信息 垃圾信息 网警110
郑重声明:本站全部内容均由本单位发布,本单位拥有全部运营和管理权,任何非本单位用户禁止注册。本站为教育公益服务站点,禁止将本站内容用于一切商业用途;如有任何内容侵权问题请务必联系本站站长,我们基于国家相关法律规定严格履行【通知—删除】义务。本单位一级域名因备案流程等原因,当前临时借用网校二级域名访问,使用此二级域名与本单位官网权属关系及运营管理权无关。韶山学校 特此声明。