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

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

添加icon需要用的函数是:

  1. gameIcon = pygame.image.load("carIcon.png")
  2. pygame.display.set_icon(gameIcon)

添加bgm和音效的函数是:

  1. crash_sound = pygame.mixer.Sound("crashed.wav")
  2. pygame.mixer.music.load("bgm.wav")

源码:

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

结果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持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号