飞道的博客

简易版挖矿小游戏

304人阅读  评论(0)

大家好,今天我们来说一说怎么制作一个简易版的挖矿小游戏

先写初始设置的代码:


  
  1. import pygame, sys, random, time
  2. pygame.init()
  3. screen = pygame.display.set_mode(( 660, 640))
  4. pygame.display.set_caption( "宝石矿工") #标题
  5. gemImg = pygame.image.load( "gem.png") #宝石
  6. heroImg = pygame.image.load( "hero.png") #挖矿者
  7. gameoverbg = pygame.image.load( "gameover.png") #失败提示
  8. bgImg = pygame.image.load( "bg.png") #背景
  9. myFont = pygame.font.SysFont( "kaiti", 30)
  10. sound = pygame.mixer.Sound( "moveMusic.wav")
  11. pygame.mixer.music.load( "bgMusic.mp3") #背景音乐
  12. pygame.mixer.music.play( -1)
  13. heroRect = pygame.Rect( 270, 0, 80, 80)

创建宝石的列表:


  
  1. gemRectList = [] # 宝石列表
  2. number = 0 # 挖到的宝石数量
  3. start = False # False表示不退出第一层循环,True表示退出
  4. #创建宝石Rect列表
  5. for i in range( 5):
  6. x = random.randint( 0, 610)
  7. y = random.randint( 30, 590)
  8. gemRect = pygame.Rect(x, y, 50, 50)
  9. gemRectList.append(gemRect)

是否继续上一次游戏的代码:


  
  1. while True:
  2. for event in pygame.event.get():
  3. if event.type == pygame.QUIT:
  4. pygame.quit()
  5. sys.exit()
  6. elif event.type == pygame.KEYDOWN:
  7. if event.key == pygame.K_y:
  8. with open( "record.txt", "r") as file:
  9. number = int(file.read())
  10. start = True
  11. elif event.key == pygame.K_n:
  12. start = True
  13. screen.fill(( 0, 0, 0))
  14. myWord = "是否继续上次的游戏成绩?y/n"
  15. text = myFont.render(myWord, True, ( 255, 0, 0))
  16. screen.blit(text, ( 130, 280))
  17. if start== True:
  18. break
  19. pygame.display.update()

把结尾写一下:


  
  1. t1 = time.time()
  2. while True:
  3. for event in pygame.event.get():
  4. if event.type == pygame.QUIT:
  5. pygame.quit()
  6. sys.exit()
  7. elif event.type == pygame.KEYDOWN:
  8. sound.play()
  9. if event.key == pygame.K_UP and heroRect.y > 0:
  10. heroRect.y = heroRect.y - 40
  11. elif event.key == pygame.K_DOWN and heroRect.y < 560:
  12. heroRect.y = heroRect.y + 40
  13. elif event.key == pygame.K_LEFT and heroRect.x > 0:
  14. heroRect.x = heroRect.x - 40
  15. elif event.key == pygame.K_RIGHT and heroRect.x < 560:
  16. heroRect.x = heroRect.x + 40
  17. t2 = time.time()
  18. t = t2 - t1 # t表示经过的时间
  19. # 绘制地图
  20. screen.blit(bgImg, ( 0, 0))
  21. # 绘制宝石
  22. for n in gemRectList:
  23. if n.colliderect(heroRect):
  24. number = number + 1
  25. n.x = random.randint( 0, 610)
  26. n.y = random.randint( 40, 590)
  27. screen.blit(gemImg, n)
  28. # 绘制英雄
  29. screen.blit(heroImg, heroRect)
  30. # 绘制字体
  31. myWord = "挖到的宝石数:" + str(number)
  32. myText = myFont.render(myWord, True, ( 0, 0, 0))
  33. screen.blit(myText, ( 420, 10))
  34. myWord = "剩余时间:" + str(int( 20 - t))
  35. myText = myFont.render(myWord, True, ( 0, 0, 0))
  36. screen.blit(myText, ( 10, 10))
  37. #时间结束,游戏结束,保存成绩,显示成绩
  38. if int(t) >= 20:
  39. with open( "record.txt", "w") as file:
  40. file.write(str(number))
  41. screen.blit(gameoverbg, ( 0, 0))
  42. myWord = "时间到了,你的成绩为:" + str(number)
  43. myText = myFont.render(myWord, True, ( 0, 0, 0))
  44. screen.blit(myText, ( 160, 80))
  45. pygame.display.update()

完成!

 

 

 

完整代码:


  
  1. import pygame, sys, random, time
  2. pygame.init()
  3. screen = pygame.display.set_mode(( 660, 640))
  4. pygame.display.set_caption( "宝石矿工") #标题
  5. gemImg = pygame.image.load( "gem.png") #宝石
  6. heroImg = pygame.image.load( "hero.png") #挖矿者
  7. gameoverbg = pygame.image.load( "gameover.png") #失败提示
  8. bgImg = pygame.image.load( "bg.png") #背景
  9. myFont = pygame.font.SysFont( "kaiti", 30)
  10. sound = pygame.mixer.Sound( "moveMusic.wav")
  11. pygame.mixer.music.load( "bgMusic.mp3") #背景音乐
  12. pygame.mixer.music.play( -1)
  13. heroRect = pygame.Rect( 270, 0, 80, 80)
  14. gemRectList = [] # 宝石列表
  15. number = 0 # 挖到的宝石数量
  16. start = False # False表示不退出第一层循环,True表示退出
  17. #创建宝石Rect列表
  18. for i in range( 5):
  19. x = random.randint( 0, 610)
  20. y = random.randint( 30, 590)
  21. gemRect = pygame.Rect(x, y, 50, 50)
  22. gemRectList.append(gemRect)
  23. while True:
  24. for event in pygame.event.get():
  25. if event.type == pygame.QUIT:
  26. pygame.quit()
  27. sys.exit()
  28. elif event.type == pygame.KEYDOWN:
  29. if event.key == pygame.K_y:
  30. with open( "record.txt", "r") as file:
  31. number = int(file.read())
  32. start = True
  33. elif event.key == pygame.K_n:
  34. start = True
  35. screen.fill(( 0, 0, 0))
  36. myWord = "是否继续上次的游戏成绩?y/n"
  37. text = myFont.render(myWord, True, ( 255, 0, 0))
  38. screen.blit(text, ( 130, 280))
  39. if start== True:
  40. break
  41. pygame.display.update()
  42. t1 = time.time()
  43. while True:
  44. for event in pygame.event.get():
  45. if event.type == pygame.QUIT:
  46. pygame.quit()
  47. sys.exit()
  48. elif event.type == pygame.KEYDOWN:
  49. sound.play()
  50. if event.key == pygame.K_UP and heroRect.y > 0:
  51. heroRect.y = heroRect.y - 40
  52. elif event.key == pygame.K_DOWN and heroRect.y < 560:
  53. heroRect.y = heroRect.y + 40
  54. elif event.key == pygame.K_LEFT and heroRect.x > 0:
  55. heroRect.x = heroRect.x - 40
  56. elif event.key == pygame.K_RIGHT and heroRect.x < 560:
  57. heroRect.x = heroRect.x + 40
  58. t2 = time.time()
  59. t = t2 - t1 # t表示经过的时间
  60. # 绘制地图
  61. screen.blit(bgImg, ( 0, 0))
  62. # 绘制宝石
  63. for n in gemRectList:
  64. if n.colliderect(heroRect):
  65. number = number + 1
  66. n.x = random.randint( 0, 610)
  67. n.y = random.randint( 40, 590)
  68. screen.blit(gemImg, n)
  69. # 绘制英雄
  70. screen.blit(heroImg, heroRect)
  71. # 绘制字体
  72. myWord = "挖到的宝石数:" + str(number)
  73. myText = myFont.render(myWord, True, ( 0, 0, 0))
  74. screen.blit(myText, ( 420, 10))
  75. myWord = "剩余时间:" + str(int( 20 - t))
  76. myText = myFont.render(myWord, True, ( 0, 0, 0))
  77. screen.blit(myText, ( 10, 10))
  78. #时间结束,游戏结束,保存成绩,显示成绩
  79. if int(t) >= 20:
  80. with open( "record.txt", "w") as file:
  81. file.write(str(number))
  82. screen.blit(gameoverbg, ( 0, 0))
  83. myWord = "时间到了,你的成绩为:" + str(number)
  84. myText = myFont.render(myWord, True, ( 0, 0, 0))
  85. screen.blit(myText, ( 160, 80))
  86. pygame.display.update()

 


转载:https://blog.csdn.net/Zhaosw2009/article/details/110294504
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场