赞
踩
需要安装的模块:
pygame,pgzero,numpy(1.19.3)
注意以下几点:
1.windows安装numpy的时候需要安装numpy 1.19.3版本,要不然会报错
2.图片需要放在images文件夹中,音频放在music文件夹中
3.images和music文件夹需要和新建的py文件放在同一级
附:
代码中涉及到的素材来自异步社区:《Python游戏趣味编程》一书中提供的素材:https://www.epubit.com/bookDetails?id=UB72096d97d6149
以下是代码:
- import pgzrun
-
- # 存储所有针的信息
- needles = []
- # 针
- startNeedle = Actor('needle', anchor=(170 + 50, 1.5))
- startNeedle.x = 200 # 针的位置
- startNeedle.y = 300
- rotateSpeed = 1 # 旋转速度
- score = 0 # 初始分数
-
-
- def draw():
- screen.fill('white') # 幕布颜色
- screen.draw.filled_circle((400, 300), 100, 'green') # 圆盘位置、半径以及颜色
- screen.draw.text(str(score), (50, 250), fontsize=50, color='green') # 显示分数
- startNeedle.draw() # 绘制针
- for needle in needles:
- needle.draw()
- # 如果旋转速度为0代表游戏结束
- if rotateSpeed == 0:
- screen.draw.text('Game Over !', (2, 320), fontsize=35, color='red')
-
-
- def update():
- global rotateSpeed
- for needle in needles:
- if needle.x == 400:
- needle.angle = needle.angle + rotateSpeed # 增加每个针的角度,实现旋转效果
-
-
- def on_key_down():
- global rotateSpeed, score
- if rotateSpeed > 0:
- music.play_once('music1') # 播放针插入圆盘的声音
-
- newNeedle = Actor('needle', anchor=(170 + 50, 1.5))
- newNeedle.x = 400
- newNeedle.y = 300
-
- # 游戏失败
- for needle in needles:
- if newNeedle.colliderect(needle):
- rotateSpeed = 0 # 游戏结束停止旋转
- music.play_once('music2')
-
- needles.append(newNeedle)
-
- if rotateSpeed > 0:
- score += 1
-
- # 分数每增加20分,速度增加1
- if score > 0 and score % 20 == 0:
- rotateSpeed += 1
-
-
- pgzrun.go()

赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。