• pgzrun 拼图游戏制作过程详解(8,9)


    8. 添加音效和背景音乐

    • 循环播放背景音乐
    sounds.bg_music.play(-1)
    • 当两个拼图进行互换时播放效果音
    1. def swap_Square(i,j): # 两个拼图的位置互换
    2. sounds.chick.play()
    3. # 略
    • 建立标记变量Win_music用以标记胜利音乐是否被播放过
    Win_music=0
    • 当游戏胜利时播放一次胜利音乐,并将标记取消
    1. if is_win:
    2. if Win_music==0:
    3. sounds.win_music.play()
    4. Win_music=1
    5. Is_Win=True

    9. 添加其他功能和优化

    • 从txt文件中读取历史最好成绩,如果没有成绩记录则写入初始化成绩。
    1. try:
    2. txtFile=open("rank.txt",'r')
    3. score=txtFile.readline()
    4. except:
    5. txtFile=open("rank.txt",'w')
    6. score = "您是第一个玩家"
    7. txtFile.write(score)
    8. txtFile.close()
    • 导入日期时间模块
    import datetime
    • 记录游戏开始时间startTime,初始化历史最好成绩oldTime,newTime是本轮游戏的计时器
    1. startTime=datetime.datetime.now()
    2. oldTime=int(score) if score.isdigit() else 9999
    3. newTime=0
    •  在游戏进行时不断地更新计时器newTime
    1. def update():
    2. global newTime,startTime
    3. if not Is_Win:
    4. endTime = datetime.datetime.now()
    5. newTime=(endTime-startTime).seconds
    6. # 略
    • 在游戏胜利是判断是否更新最好成绩并写入文档
    1. def on_mouse_down(pos,button): # 当鼠标被点击时
    2. # 略
    3. if is_win:
    4. # 略
    5. if newTime
    6. txtFile=open("rank.txt",'w')
    7. txtFile.write(str(newTime))
    8. txtFile.close()
    • 将历史最好成绩级计时器显示在窗口上
    1. def draw():
    2. # 略
    3. screen.draw.text("游戏最佳记录: "+str(oldTime), (20, 10), fontsize=20,\
    4. fontname='s', color="blue")
    5. screen.draw.text("游戏运行时间: " + str(newTime), (WIDTH/2, 10), \
    6. fontsize=20, fontname='s', color="blue")

    执行效果如下图所示:

    完整代码如下:

    1. import pgzrun
    2. import random
    3. import datetime
    4. try:
    5. txtFile=open("rank.txt",'r')
    6. score=txtFile.readline()
    7. except:
    8. txtFile=open("rank.txt",'w')
    9. score = "您是第一个玩家"
    10. txtFile.write(score)
    11. txtFile.close()
    12. startTime=datetime.datetime.now()
    13. oldTime=int(score) if score.isdigit() else 9999
    14. newTime=0
    15. TITLE="pgzrun 拼图游戏"
    16. Square_size=125
    17. WIDTH=Square_size*4
    18. HEIGHT=Square_size*6
    19. click_time=0
    20. clickID_1=clickID_2=-1
    21. Is_Win=False
    22. Win_music=0
    23. sounds.bg_music.play(-1)
    24. Squares=[Actor("girl_01"),Actor("girl_02"),Actor("girl_03"),Actor("girl_04")
    25. ,Actor("girl_05"),Actor("girl_06"),Actor("girl_07"),Actor("girl_08")
    26. ,Actor("girl_09"),Actor("girl_10"),Actor("girl_11"),Actor("girl_12")
    27. ,Actor("girl_13"),Actor("girl_14"),Actor("girl_15"),Actor("girl_16")
    28. ,Actor("girl_17"),Actor("girl_18"),Actor("girl_19"),Actor("girl_20")
    29. ,Actor("girl_21"),Actor("girl_22"),Actor("girl_23"),Actor("girl_24")
    30. ]
    31. Gird=[]
    32. for i in range(6):
    33. for j in range(4):
    34. Square=Squares[i*4+j]
    35. Square.left=Square_size*j
    36. Square.top=Square_size*i
    37. Gird.append(Square)
    38. def swap_Square(i,j): # 两个拼图的位置互换
    39. sounds.chick.play()
    40. temp_pos=Gird[i].pos
    41. Gird[i].pos=Gird[j].pos
    42. Gird[j].pos=temp_pos
    43. for k in range(10): # 随机抽取10组拼图 进行位置互换
    44. i=random.randint(0,23)
    45. j=random.randint(0,23)
    46. swap_Square(i, j)
    47. def on_mouse_down(pos,button): # 当鼠标被点击时
    48. global click_time ,clickID_1 , clickID_2,Is_Win,Win_music
    49. for i in range(24):
    50. if Gird[i].collidepoint(pos): # 拼图对象被点击
    51. break
    52. if click_time%2==0 :
    53. clickID_1=i
    54. else:
    55. clickID_2=i
    56. swap_Square(clickID_1,clickID_2)
    57. click_time += 1
    58. # 成功判断
    59. is_win = True
    60. for i in range(6):
    61. for j in range(4):
    62. Square = Squares[i * 4 + j]
    63. if not (Square.left == Square_size * j and Square.top == Square_size * i) :
    64. is_win = False
    65. break
    66. if is_win:
    67. if Win_music==0:
    68. sounds.win_music.play()
    69. Win_music=1
    70. Is_Win=True
    71. if newTime
    72. txtFile=open("rank.txt",'w')
    73. txtFile.write(str(newTime))
    74. txtFile.close()
    75. def draw():
    76. screen.clear()
    77. for Square in Gird:
    78. Square.draw()
    79. screen.draw.text("游戏最佳记录: "+str(oldTime), (20, 10), fontsize=20, fontname='s', color="blue")
    80. screen.draw.text("游戏运行时间: " + str(newTime), (WIDTH/2, 10), fontsize=20, fontname='s', color="blue")
    81. if Is_Win:
    82. screen.draw.text("游戏胜利!",(WIDTH/2-100,HEIGHT/2-50),fontsize=50,fontname='s',color="blue")
    83. else :
    84. for i in range(5):
    85. screen.draw.line((i*Square_size,0),(i*Square_size,HEIGHT),"black")
    86. for i in range(7):
    87. screen.draw.line((0,i*Square_size),(WIDTH,i*Square_size),"black")
    88. if clickID_1!=-1:
    89. screen.draw.rect(Rect((Gird[clickID_1].left,Gird[clickID_1].top),(Square_size,Square_size)),"red")
    90. def update():
    91. global newTime,startTime
    92. if not Is_Win:
    93. endTime = datetime.datetime.now()
    94. newTime=(endTime-startTime).seconds
    95. pgzrun.go()

    pgzrun拼图游戏素材包下载

  • 相关阅读:
    BFS:多源BFS问题
    介绍Mybatis与使用(什么是Mybatis?)
    小米14 ULTRA:重新定义手机摄影的新篇章
    力扣1047删除字符串中的所有相邻重复项(java,栈解法)
    【HDLBits 刷题 15】Verification Writing Testbenches
    ps打开找不到MSVCP140.dll重新安装方法,安装ps出现msvcp140.dll缺失解决方法
    mysql中的这些日志,你都知道吗?
    公司电脑如何防止资料外泄、部署驱动层透明加密防泄密管理系统——「天锐绿盾」
    安卓sqlite数据库问题请教
    Android音视频开发:AudioRecord录制音频
  • 原文地址:https://blog.csdn.net/lybc2019/article/details/132830975