经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
pygame游戏之旅 添加游戏暂停功能
来源:jb51  时间:2018/11/22 10:00:24  对本文有异议

本文为大家分享了pygame游戏之旅的第13篇,供大家参考,具体内容如下

定义暂停函数:

  1. def paused():
  2. largeText = pygame.font.SysFont('comicsansms',115)
  3. TextSurf, TextRect = text_objects('Paused', largeText)
  4. TextRect.center = ((display_width/2),(display_height/2))
  5. gameDisplay.blit(TextSurf, TextRect)
  6. while pause:
  7. for event in pygame.event.get():
  8. print(event)
  9. if event.type == pygame.QUIT:
  10. pygame.quit()
  11. quit()
  12. ## gameDisplay.fill(white)
  13. button("Continue", 150, 450, 100, 50, green, bright_green,game_loop)
  14. button("Quit",550, 450, 100, 50, red, bright_red,quitgame)
  15. pygame.display.update()
  16. clock.tick(15)

重新定义原来的crah函数:

  1. def crash():
  2. largeText = pygame.font.SysFont('comicsansms',115)
  3. TextSurf, TextRect = text_objects('You Crashed!', largeText)
  4. TextRect.center = ((display_width/2),(display_height/2))
  5. gameDisplay.blit(TextSurf, TextRect)
  6. while True:
  7. for event in pygame.event.get():
  8. print(event)
  9. if event.type == pygame.QUIT:
  10. pygame.quit()
  11. quit()
  12. ## gameDisplay.fill(white)
  13. button("Play Again", 150, 450, 100, 50, green, bright_green,game_loop)
  14. button("Quit",550, 450, 100, 50, red, bright_red,quitgame)
  15. pygame.display.update()
  16. clock.tick(15)

源代码:

  1. import pygame
  2. import time
  3. import random
  4. pygame.init()
  5. white = (255,255,255)
  6. black = (0,0,0)
  7. gray = (128,128,128)
  8. red = (200,0,0)
  9. green = (0,200,0)
  10. bright_red = (255,0,0)
  11. bright_green = (0,255,0)
  12. blue = (0,0,255)
  13.  
  14. car_width = 100
  15. display_width = 800
  16. display_height = 600
  17.  
  18. gameDisplay = pygame.display.set_mode( (display_width,display_height) )
  19. pygame.display.set_caption('A bit Racey')
  20. clock = pygame.time.Clock()
  21. carImg = pygame.image.load('car.png')
  22. pause = False
  23. ##crash = True
  24. def things_dodged(count):
  25. font = pygame.font.SysFont(None, 25)
  26. text = font.render("Dodged:"+str(count), True, black)
  27. gameDisplay.blit(text,(0,0))
  28. def things(thingx, thingy, thingw, thingh, color):
  29. pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
  30. def car(x, y):
  31. gameDisplay.blit(carImg, (x,y))
  32. def text_objects(text, font):
  33. textSurface = font.render(text, True, black)
  34. return textSurface, textSurface.get_rect()
  35. def crash():
  36. largeText = pygame.font.SysFont('comicsansms',115)
  37. TextSurf, TextRect = text_objects('You Crashed!', largeText)
  38. TextRect.center = ((display_width/2),(display_height/2))
  39. gameDisplay.blit(TextSurf, TextRect)
  40. while True:
  41. for event in pygame.event.get():
  42. print(event)
  43. if event.type == pygame.QUIT:
  44. pygame.quit()
  45. quit()
  46. ## gameDisplay.fill(white)
  47. button("Play Again", 150, 450, 100, 50, green, bright_green,game_loop)
  48. button("Quit",550, 450, 100, 50, red, bright_red,quitgame)
  49. pygame.display.update()
  50. clock.tick(15)
  51. def button (msg, x, y, w, h, ic, ac, action=None):
  52. mouse =pygame.mouse.get_pos()
  53. click = pygame.mouse.get_pressed()
  54. print(click)
  55. if x + w > mouse[0] > x and y + h > mouse[1] > y:
  56. pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
  57. if click[0] == 1 and action != None:
  58. action()
  59. ## if action == "play":
  60. ## action()
  61. ## if action == "quit":
  62. ## pygame.quit()
  63. ## quit()
  64. else:
  65. pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
  66. smallText = pygame.font.SysFont('comicsansms', 20)
  67. textSurf, textRect = text_objects(msg, smallText)
  68. textRect.center = ( (x+(w/2)), (y+(h/2)))
  69. gameDisplay.blit(textSurf, textRect)
  70. def quitgame():
  71. pygame.quit()
  72. quit()
  73. def unpause():
  74. global pause
  75. pause = False
  76. def paused():
  77. largeText = pygame.font.SysFont('comicsansms',115)
  78. TextSurf, TextRect = text_objects('Paused', largeText)
  79. TextRect.center = ((display_width/2),(display_height/2))
  80. gameDisplay.blit(TextSurf, TextRect)
  81. while pause:
  82. for event in pygame.event.get():
  83. print(event)
  84. if event.type == pygame.QUIT:
  85. pygame.quit()
  86. quit()
  87. ## gameDisplay.fill(white)
  88. button("Continue", 150, 450, 100, 50, green, bright_green,unpause)
  89. button("Quit",550, 450, 100, 50, red, bright_red,quitgame)
  90. pygame.display.update()
  91. clock.tick(15)
  92. def game_intro():
  93. global pasue
  94. pause = False
  95. intro = True
  96. while intro:
  97. for event in pygame.event.get():
  98. print(event)
  99. if event.type == pygame.QUIT:
  100. pygame.quit()
  101. quit()
  102. gameDisplay.fill(white)
  103. largeText = pygame.font.SysFont('comicsansms',115)
  104. TextSurf, TextRect = text_objects('A bit Racey', largeText)
  105. TextRect.center = ((display_width/2),(display_height/2))
  106. gameDisplay.blit(TextSurf, TextRect)
  107. button("GO", 150, 450, 100, 50, green, bright_green,game_loop)
  108. button("Quit",550, 450, 100, 50, red, bright_red,quitgame)
  109. pygame.display.update()
  110. clock.tick(15)
  111. def game_loop():
  112. global pause
  113. x = display_width * 0.45
  114. y = display_height * 0.8
  115. x_change = 0
  116. dodged = 0
  117.  
  118. gameExit = False
  119. thing_startx = random.randrange(0, display_width)
  120. thing_starty = -600
  121. thing_speed = 7
  122. thing_width = 100
  123. thing_height = 100
  124. while not gameExit:
  125. for event in pygame.event.get():
  126. if event.type == pygame.QUIT:
  127. pygame.quit()
  128. quit()
  129. if event.type == pygame.KEYDOWN:
  130. if event.key == pygame.K_LEFT:
  131. x_change = -5
  132. elif event.key == pygame.K_RIGHT:
  133. x_change = 5
  134. elif event.key == pygame.K_p:
  135. pause = True
  136. paused()
  137. if event.type == pygame.KEYUP:
  138. if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  139. x_change = 0
  140. print(event)
  141. x += x_change
  142. gameDisplay.fill(white)
  143. things(thing_startx, thing_starty, thing_width, thing_height, black)
  144. thing_starty += thing_speed
  145. car(x,y)
  146. things_dodged(dodged)
  147. if x > display_width - car_width or x < 0:
  148. gameExit = True
  149. if thing_starty > display_height:
  150. thing_starty = 0 - thing_height
  151. thing_startx = random.randrange(0, display_width)
  152. dodged += 1
  153. thing_speed += 1
  154. thing_width += (dodged * 1.2)
  155. if y < thing_starty + thing_height:
  156. print('y crossover')
  157. if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
  158. print('x crossover')
  159. crash()
  160. pygame.display.update()
  161. clock.tick(60)
  162. #crash()
  163. game_intro()
  164. game_loop()
  165. pygame.quit()
  166. quit()
  167.  

结果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号