[关闭]
@zhongwei1234 2017-12-11T04:47:26.000000Z 字数 5338 阅读 68

计算物理期中作业 pygame

钟伟 2015301020164


一.内容
小球的碰撞游戏
游戏规则:
1.游戏初始化的时候,有5个不同颜色的小球进行碰撞
2.玩家可以通过在窗口中单击鼠标左键进行增加小球个数
3.玩家可以通过在窗口中单击鼠标右键进行删减小球个数
4.玩家可以通过键盘的方向键:上,右键进行对小球加速
5.玩家可以通过键盘的方向键:下,左键进行对小球减速
6.玩家可以按键盘:f键实现全屏显示
7.玩家可以按键盘:Esc键实现退出全屏操作
8.窗口左下角显示小球个数,右下角显示作者邮箱

  *感谢pong的代码我写的大炮游戏太挫运行不出来*
  ---

二.赏析

  1. if not pygame.font: print('Warning, fonts disabled')
  2. if not pygame.mixer: print('Warning, sound disabled')

让程序更稳定,若音频与图片加载不成功直接报错

  1. SCREEN_WIDTH = 600
  2. SCREEN_HEIGHT = 500
  3. SPEED = 1
  4. VOLUME = 5
  5. SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT + 20)
  6. SCREEN_DEFAULT_COLOR = (255, 255 ,255)
  7. READY = 0

设定初始条件

  1. screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
  2. screen.fill(SCREEN_DEFAULT_COLOR)
  3. bg = pygame.image.load('data\\image\\bg.jpg').convert()
  4. font = pygame.font.Font('data\\font\\TORK____.ttf', 14)
  5. new_sound = pygame.mixer.Sound('data\\sound\\new.wav')
  6. bg_sound = pygame.mixer.Sound('data\\sound\\bg.ogg')
  7. bg_sound.set_volume(0.1 * VOLUME)
  8. bg_sound.play(-1)
  9. new_sound.set_volume(0.1 * VOLUME)

调入音频 调节音量

  1. balls = []
  2. BALL_R = 30
  3. BALL_COLORS = [(255,165,0),(255,0,0),(135,206,235),(178,34,34),(34,139,34)]
  4. BALL_POINTS = [[40, 40],[40, 300],[400, 200],[150, 150],[80, 400]]
  5. BALL_VELOCITY = [[1.5, 1.2],[1.4, -1.3],[-1.5, -1.1],[-1.2, 1.5],[1.3, 1.1]]
  6. VOLUME_POINTS = []
  7. VOLUME_POINTS_START = []
  8. VOLUME_RECT_COLORS = []
  9. for p in range(170, 250, 7):
  10. VOLUME_POINTS.append([SCREEN_WIDTH - p,SCREEN_HEIGHT + 20])
  11. for ps in range(175, 250, 7):
  12. VOLUME_POINTS_START.append([SCREEN_WIDTH - ps, SCREEN_HEIGHT])
  13. VOLUME_RECT_COLORS.append((randint(0, 255), randint(0, 255), randint(0, 255)))
  14. print(VOLUME_POINTS[-10])
  15. print(VOLUME_POINTS_START[-10])
  16. for i in range(len(BALL_COLORS)):
  17. screen.fill(SCREEN_DEFAULT_COLOR)
  18. b = pygame.draw.circle(screen, BALL_COLORS[i], (int(BALL_POINTS[i][0]),int(BALL_POINTS[i][1])), BALL_R)
  19. balls.append(b)
  20. while 1:
  21. for event in pygame.event.get():
  22. if event.type == QUIT:
  23. bg_sound.stop()
  24. exit()
  25. elif event.type == KEYDOWN:
  26. if event.key == K_UP:
  27. SPEED += 0.1
  28. elif event.key == K_DOWN:
  29. SPEED -= 0.1
  30. elif event.key == K_LEFT:
  31. if VOLUME > 0:
  32. VOLUME -= 1
  33. elif event.key == K_RIGHT:
  34. if VOLUME <= 9:
  35. VOLUME += 1
  36. elif event.key == K_f:
  37. pygame.display.set_mode(SCREEN_DEFAULT_SIZE, FULLSCREEN, 32)
  38. elif event.key == 27:
  39. pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
  40. elif event.type == MOUSEBUTTONDOWN:
  41. pressed_array = pygame.mouse.get_pressed()
  42. for index in range(len(pressed_array)):
  43. if pressed_array[index]:
  44. if index == 0:
  45. new_sound.play(-1)
  46. c_color = (randint(0, 255), randint(0, 255), randint(0, 255))
  47. x, y = (BALL_R+1, BALL_R+1)
  48. c_r = randint(10, 100)
  49. c_v = [randint(11, 19)* 0.1, randint(11, 19) * 0.1]
  50. c = pygame.draw.circle(screen, c_color, (x, y), BALL_R)
  51. BALL_COLORS.append(c_color)
  52. BALL_POINTS.append([x, y])
  53. BALL_VELOCITY.append(c_v)
  54. balls.append(c)
  55. elif index == 2:
  56. if len(balls) > 1:
  57. balls.pop(0)
  58. BALL_COLORS.pop(0)
  59. BALL_POINTS.pop(0)
  60. BALL_VELOCITY.pop(0)
  61. elif event.type == MOUSEBUTTONUP:
  62. new_sound.stop()
  63. #print(balls)
  64. for i in range(len(balls)):
  65. screen.blit(bg, (-300, -100))
  66. #screen.fill(SCREEN_DEFAULT_COLOR)
  67. for n in range(len(balls)):
  68. '''
  69. if ((BALL_POINTS[i][0] - BALL_R) > 0 and (BALL_POINTS[i][0] - BALL_R) < BALL_R):
  70. pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] + BALL_R),int(BALL_POINTS[n][1])), BALL_R)
  71. elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_WIDTH and (BALL_POINTS[i][1] + BALL_R) > SCREEN_WIDTH - BALL_R):
  72. pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0] - BALL_R),int(BALL_POINTS[n][1])), BALL_R)
  73. elif ((BALL_POINTS[i][1] - BALL_R) > 0 and (BALL_POINTS[i][1] - BALL_R) < BALL_R):
  74. pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] + BALL_R)), BALL_R)
  75. elif ((BALL_POINTS[i][1] + BALL_R) < SCREEN_HEIGHT and (BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT - BALL_R):
  76. pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1] - BALL_R)), BALL_R)
  77. '''
  78. pygame.draw.circle(screen, BALL_COLORS[n], (int(BALL_POINTS[n][0]),int(BALL_POINTS[n][1])), BALL_R)
  79. if ((((BALL_POINTS[i][0] - BALL_R) < 0) or ((BALL_POINTS[i][0] + BALL_R) > SCREEN_WIDTH))):
  80. BALL_VELOCITY[i][0] = -1 * BALL_VELOCITY[i][0]
  81. if ((((BALL_POINTS[i][1] - BALL_R) < 0) or ((BALL_POINTS[i][1] + BALL_R) > SCREEN_HEIGHT))):
  82. BALL_VELOCITY[i][1] = -1 * BALL_VELOCITY[i][1]
  83. for j in range(len(balls)):
  84. for k in range(len(balls)):
  85. b_x = (BALL_POINTS[j][0] - BALL_POINTS[k][0])**2
  86. b_y = (BALL_POINTS[j][1] - BALL_POINTS[k][1])**2
  87. b_r =(BALL_R*2)**2
  88. if (round((b_x + b_y), 2) <= round(b_r, 2)):
  89. temp_x = BALL_VELOCITY[j][0]
  90. temp_y = BALL_VELOCITY[j][1]
  91. BALL_VELOCITY[j][0] = BALL_VELOCITY[k][0]
  92. BALL_VELOCITY[j][1] = BALL_VELOCITY[k][1]
  93. BALL_VELOCITY[k][0] = temp_x
  94. BALL_VELOCITY[k][1] = temp_y
  95. BALL_POINTS[j][0] += round(SPEED, 1) * BALL_VELOCITY[j][0]
  96. BALL_POINTS[j][1] += round(SPEED, 1) * BALL_VELOCITY[j][1]
  97. pygame.draw.line(screen, (165,42,42),(0, SCREEN_HEIGHT), (SCREEN_WIDTH,SCREEN_HEIGHT))
  98. bg_sound.set_volume(0.1 * VOLUME)
  99. new_sound.set_volume(0.1 * VOLUME)
  100. pygame.draw.rect(screen,
  101. (255, 255, 255),
  102. Rect((VOLUME_POINTS_START[-1][0],
  103. VOLUME_POINTS_START[-1][1]),
  104. (VOLUME_POINTS[-10][0] - VOLUME_POINTS_START[-1][0],
  105. 20)))
  106. for v in range(VOLUME+1):
  107. if v > 0:
  108. pygame.draw.rect(screen,
  109. VOLUME_RECT_COLORS[v],
  110. Rect((VOLUME_POINTS_START[-v][0],
  111. VOLUME_POINTS_START[-v][1]),
  112. (VOLUME_POINTS[-v][0] - VOLUME_POINTS_START[-v][0],
  113. 20)))
  114. game_info = 'Balls: ' + str(len(balls)) + ' Speed: ' + str(round(SPEED, 2)) + ' LastBall: ' + str(round(BALL_POINTS[-1][0])) + ',' + str(round(BALL_POINTS[-1][1]))
  115. text = font.render(game_info, True, (255,255,255))
  116. author_info = font.render('hongtenzone@foxmail.com', True, (255,255,255))
  117. volume_text = font.render('Volume: ' + str(VOLUME), True, (255, 255, 255))
  118. screen.blit(text, (0, SCREEN_HEIGHT+5))
  119. screen.blit(author_info, (SCREEN_WIDTH - 160, SCREEN_HEIGHT+5))
  120. screen.blit(volume_text, (SCREEN_WIDTH - 310, SCREEN_HEIGHT+5))
  121. pygame.display.update()

最主要的程序 小球的运动

三.效果
动图第二次作业.gif

谢谢pong的代码!!!!

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