python代码练习
import pgzero
import random
from random import randint
import string
WIDTH = 1000
HEIGHT = 600
SIZE = 127 #本案例中气球图片像素大小为127*127,这个可以根据自己图片的大小来设置
def set_random_pos(actor):
rand_x = randint(0+SIZE, WIDTH-SIZE)
rand_y = randint(0+SIZE, HEIGHT - SIZE)
actor.x = rand_x
actor.y = rand_y
#设置N个气球,作为一局
N = 10
balloons = []
#随机从26个英文字母中抽取一个作为当前气球的字母
for i in range(10):
j = randint(0,25)
random_letter = random.choice(string.ascii_uppercase[j])
balloon = Actor("balloon")
set_random_pos(balloon)
balloon.text = random_letter
balloon.typed = False
balloons.append(balloon)
# for i in balloons:
# print(i.text)
global SCORE
SCORE = 0
global TRYTIME
TRYTIME = 0
global FINISH
FINISH = False
def update():
pass
def draw():
global SCORE
global FINISH
screen.fill((255,255,255))
for b in balloons:
b.draw()
if b.typed:
screen.draw.text(b.text, center=b.center, color="red", fontsize=100)
else:
screen.draw.text(b.text, center=b.center, color="grey", fontsize=60)
check_finish()
# print(FINISH)
if FINISH:
screen.draw.text("GAME OVER!", (200, 200), color="green", fontsize=150)
return
def on_key_down(key):
#获取键盘上按下的按键(key)
#它的枚举值详见https://pygame-zero.readthedocs.io/zh_CN/latest/hooks.html#buttons-and-keys
# print(str(key))
for b in balloons:
if str(key) == "keys." + str(b.text):
b.typed = True
def check_finish():
global FINISH
typed_num = 0
for b in balloons:
if b.typed:
typed_num += 1
if typed_num == len(balloons):
FINISH = True
else:
FINISH = False