当前位置:   article > 正文

学习Python开发小游戏(一)-----见缝插针_python游戏素材放在哪里

python游戏素材放在哪里

需要安装的模块:

        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

以下是代码:

  1. import pgzrun
  2. # 存储所有针的信息
  3. needles = []
  4. # 针
  5. startNeedle = Actor('needle', anchor=(170 + 50, 1.5))
  6. startNeedle.x = 200 # 针的位置
  7. startNeedle.y = 300
  8. rotateSpeed = 1 # 旋转速度
  9. score = 0 # 初始分数
  10. def draw():
  11. screen.fill('white') # 幕布颜色
  12. screen.draw.filled_circle((400, 300), 100, 'green') # 圆盘位置、半径以及颜色
  13. screen.draw.text(str(score), (50, 250), fontsize=50, color='green') # 显示分数
  14. startNeedle.draw() # 绘制针
  15. for needle in needles:
  16. needle.draw()
  17. # 如果旋转速度为0代表游戏结束
  18. if rotateSpeed == 0:
  19. screen.draw.text('Game Over !', (2, 320), fontsize=35, color='red')
  20. def update():
  21. global rotateSpeed
  22. for needle in needles:
  23. if needle.x == 400:
  24. needle.angle = needle.angle + rotateSpeed # 增加每个针的角度,实现旋转效果
  25. def on_key_down():
  26. global rotateSpeed, score
  27. if rotateSpeed > 0:
  28. music.play_once('music1') # 播放针插入圆盘的声音
  29. newNeedle = Actor('needle', anchor=(170 + 50, 1.5))
  30. newNeedle.x = 400
  31. newNeedle.y = 300
  32. # 游戏失败
  33. for needle in needles:
  34. if newNeedle.colliderect(needle):
  35. rotateSpeed = 0 # 游戏结束停止旋转
  36. music.play_once('music2')
  37. needles.append(newNeedle)
  38. if rotateSpeed > 0:
  39. score += 1
  40. # 分数每增加20分,速度增加1
  41. if score > 0 and score % 20 == 0:
  42. rotateSpeed += 1
  43. pgzrun.go()

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/blog/article/detail/47916
推荐阅读
相关标签
  

闽ICP备14008679号