[关闭]
@zhongwei1234 2017-10-14T00:38:08.000000Z 字数 1885 阅读 60

pygame 抛体游戏 真的不会写


只有欣赏一下雷珉毅楠同学

  1. Created on Fri Sep 22 20:57:42 2017
  2. @author: ppsair
  3. """
  4. background_image_filename = "C:/Users/ppsair/Desktop/python.PNG"
  5. sprite_image_filename = "C:/Users/ppsair/Desktop/pythoncanon1.PNG"
  6. small_fire_image_filename = "C:/Users/ppsair/Desktop/smallfire.PNG"
  7. information="your score : "
  8. import pygame
  9. from pygame.locals import*
  10. from sys import exit
  11. import pygame as py
  12. import math
  13. pygame.init()
  14. screen = pygame.display.set_mode((815,450), 0, 32)
  15. background = pygame.image.load(background_image_filename)
  16. sprite = pygame.image.load(sprite_image_filename)
  17. smallfire = pygame.image.load(small_fire_image_filename)
  18. delt_t = 0.5
  19. B = 0.00004
  20. g = 9.8
  21. h = 2.5
  22. a = 0.0065
  23. T_ref = 300
  24. T = 300
  25. angel = 0
  26. while True:
  27. for event in pygame.event.get():
  28. if event.type == pygame.QUIT:
  29. exit()
  30. screen.blit(background,(0,0))
  31. screen.blit(sprite,(0,350))
  32. for event in pygame.event.get():
  33. if event.type == pygame.KEYDOWN:
  34. if event.type == pygame.K_UP:
  35. angel += 1
  36. elif event.key == pygame.K_DOWN:
  37. angel += -1
  38. elif event.key == pygame.K_SPACE:
  39. v = 700
  40. v_x = v*math.cos(math.radians(angel))
  41. v_y = v*math.sin(math.radians(angel))
  42. x = 0
  43. y = 0
  44. while True:
  45. x = x + v_x*delt_t
  46. y = y + v_y*delt_t
  47. p=(1-a*y/T)*(T/T_ref)
  48. B_cor = B*math.pow(p,h)
  49. v_x = v_x - B_cor*v*v_x*delt_t
  50. v_y = v_y - g*delt_t - B_cor*v*v_y*delt_t
  51. v = math.sqrt(v_x*v_x + v_y*v_y)
  52. score_percentage = int(abs(x/30000*800-779)/779*100)
  53. score = str(score_percentage)
  54. score = information+score
  55. my_font=pygame.font.SysFont("arial",64)
  56. name_surface=my_font.render(score,True,(0,0,0),(255,255,255))
  57. show_angel=pygame.font.SysFont("arial",64)
  58. angel_show=show_angel.render(str(angel),True,(0,0,0),(255,255,255))
  59. screen.blit(angel_show,(700,0))
  60. screen.blit(name_surface,(0,0))
  61. if y > 0:
  62. pygame.draw.circle(screen,(23,23,23),(int(x/30000*800),int(450-(y/10000*300))),5,5)
  63. pygame.display.update()
  64. else:
  65. screen.blit(smallfire,(x,300))
  66. break
  67. pygame.display.update()

游戏用的绝对路径,于surface打包封装后可以使用相对路径.pygame也是才安装, 在pygame prompt中输入pip install pygame 安装成,但玩不来

这是一个用上下键控制初射角度的抛体运动游戏,有一个根据抛出距离远近打分的机制·,越远分越高,如果在surface上贴入背景图比如大炮和战场,就勉强算一个游戏了

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注