小学生python编程--红包雨
from pgzrun import *
from random import *
#{
music.play("bg.mp3")
WIDTH = 370
HEIGHT = 596
bg0 = Actor("bg0.png")
bg1 = Actor("bg1.png")
score_show = Actor("score.png", [60, 36])
time_show = Actor("time.png", [300, 36])
over = Actor("over.png")
again = Actor("again.png",[175,375])
#}
#创建红包
red = []
def create_red():
r = Actor("hongbao1.png")
r.x = randint(20,330)
r.y = randint(0, 50)
red.append(r)
clock.schedule_interval(create_red, 0.6)
#按下按键让state变为 "run",开启游戏
#{
state = "ready"
def on_key_down(key):
global state
if keyboard.space == True:
state = "run"
#}
#倒计时
#{
total = 30
def timing():
global total, state
total = total - 1
if total == 0:
state = "over"
clock.unschedule(timing)
clock.schedule_interval(timing, 1)
#}
#根据state的值来绘制不同游戏状态的角色
#{
def draw():
if state == "ready":
bg0.draw()
if state == "run":
bg1.draw()
for r in red:
r.draw()
score_show.draw()
time_show.draw()
screen.draw.text(
"score: "+str(score),
fontsize=30,
center=[64, 38]
)
screen.draw.text(
"time: "+str(total),
fontsize=30,
center=[292, 38]
)
if state == "over":
over.draw()
again.draw()
screen.draw.text(
str(score),
fontsize=80,
center=[178, 110],
)
music.stop()
#}
#移动红包
#{
def move_red():
for r in red:
r.y = r.y + 3
if r.bottom < 0:
red.remove(r)
def update():
if state == "run":
move_red()
#}
#鼠标点击红包
score = 0
def on_mouse_down(pos):
global score, total, red, state
for r in red:
if r.collidepoint(pos):
red.remove(r)
score = score + 1
sounds.get.play()
#鼠标点击重玩按钮则重新开始
if state == "over":
if again.collidepoint(pos):
score = 0
total = 30
red = []
state = "ready"
music.play("bg.mp3")
clock.schedule_interval(timing, 1)
go()
相关文章
- python如何将网络上的图片url和base64编码的图片保存在本地
- Python分享:python爬虫可以用来做什么?
- 162 python网络编程 - 协程
- 161 python网络编程 - 单进程服务器(epoll版)
- Python爬虫技术--基础篇--函数式编程(上篇)
- 《Python密码学编程》——1.11 双重强度加密
- 《树莓派Python编程入门与实战(第2版)》——3.11 练习
- 《教孩子学编程(Python语言版)》——1.3 运行Python程序
- 《Python数据可视化编程实战》——1.7 安装requests模块
- 《趣学Python编程》——2.3 使用变量
- 转【Python】Python-skier游戏[摘自.与孩子一起学编程]
- python之time模块:获取当前时间
- 华为OD机试 - 最少停车数(Python)
- [Spark][Python][Application]非交互式运行Spark Application 的例子
- 小学生python游戏编程arcade----动画图片实现爆炸效果
- PyCharm设置Python版本
- 学习笔记(51):Python实战编程-ListBox
- 学习笔记(41):Python实战编程-按钮