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

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

实现点击功能:

  1. click = pygame.mouse.get_pressed()
  2. print(click)
  3. if x + w > mouse[0] > x and y + h > mouse[1] > y:
  4. pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
  5. if click[0] == 1 and action != None:
  6. action()

修改显示文字:

  1. pygame.font.SysFont('comicsansms',115)

源代码:

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

结果图:

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