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

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

定义一个button函数,将文字,颜色等作为参数。

  1. def button (msg, x, y, w, h, ic, ac):
  2. mouse =pygame.mouse.get_pos()
  3. if x + w > mouse[0] > x and y + h > mouse[1] > y:
  4. pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
  5. else:
  6. pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
  7. smallText = pygame.font.Font("freesansbold.ttf", 20)
  8. textSurf, textRect = text_objects(msg, smallText)
  9. textRect.center = ( (x+(w/2)), (y+(h/2)))
  10. gameDisplay.blit(textSurf, textRect)
  11.  

全部代码为:

  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.Font('freesansbold.ttf',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):
  42. mouse =pygame.mouse.get_pos()
  43. if x + w > mouse[0] > x and y + h > mouse[1] > y:
  44. pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
  45. else:
  46. pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
  47. smallText = pygame.font.Font("freesansbold.ttf", 20)
  48. textSurf, textRect = text_objects(msg, smallText)
  49. textRect.center = ( (x+(w/2)), (y+(h/2)))
  50. gameDisplay.blit(textSurf, textRect)
  51. def game_intro():
  52. intro = True
  53. while intro:
  54. for event in pygame.event.get():
  55. print(event)
  56. if event.type == pygame.QUIT:
  57. pygame.quit()
  58. quit()
  59. gameDisplay.fill(white)
  60. largeText = pygame.font.Font('freesansbold.ttf',115)
  61. TextSurf, TextRect = text_objects('A bit Racey', largeText)
  62. TextRect.center = ((display_width/2),(display_height/2))
  63. gameDisplay.blit(TextSurf, TextRect)
  64. button("GO", 150, 450, 100, 50, green, bright_green)
  65. button("Quit",550, 450, 100, 50, red, bright_red)
  66. pygame.display.update()
  67. clock.tick(15)
  68. def game_loop():
  69. x = display_width * 0.45
  70. y = display_height * 0.8
  71. x_change = 0
  72. dodged = 0
  73. gameExit = False
  74. thing_startx = random.randrange(0, display_width)
  75. thing_starty = -600
  76. thing_speed = 7
  77. thing_width = 100
  78. thing_height = 100
  79. while not gameExit:
  80. for event in pygame.event.get():
  81. if event.type == pygame.QUIT:
  82. pygame.quit()
  83. quit()
  84. if event.type == pygame.KEYDOWN:
  85. if event.key == pygame.K_LEFT:
  86. x_change = -5
  87. elif event.key == pygame.K_RIGHT:
  88. x_change = 5
  89. if event.type == pygame.KEYUP:
  90. if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  91. x_change = 0
  92. print(event)
  93. x += x_change
  94. gameDisplay.fill(white)
  95. things(thing_startx, thing_starty, thing_width, thing_height, black)
  96. thing_starty += thing_speed
  97. car(x,y)
  98. things_dodged(dodged)
  99. if x > display_width - car_width or x < 0:
  100. gameExit = True
  101. if thing_starty > display_height:
  102. thing_starty = 0 - thing_height
  103. thing_startx = random.randrange(0, display_width)
  104. dodged += 1
  105. thing_speed += 1
  106. thing_width += (dodged * 1.2)
  107. if y < thing_starty + thing_height:
  108. print('y crossover')
  109. if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
  110. print('x crossover')
  111. crash()
  112. pygame.display.update()
  113. clock.tick(60)
  114. #crash()
  115. game_intro()
  116. game_loop()
  117. pygame.quit()
  118. 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号