Guest User

Untitled

a guest
Sep 1st, 2022
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. import pygame
  2. import os
  3. import time
  4. import random
  5. import math
  6. from player import Player
  7. import re
  8.  
  9. pygame.font.init()
  10. pygame.mixer.init()
  11. fps = 60
  12. background_rgb = (255, 255, 255)
  13. width, height = 1080, 720
  14. win = pygame.display.set_mode((width, height))
  15. clock = pygame.time.Clock()
  16.  
  17. space_debounce = False
  18.  
  19. gravity = 0.03
  20. flap_power = -2.5
  21.  
  22. player = Player(width / 2 - 32, height / 2 - 64, 0, 0)
  23.  
  24. starting_height = player.y_pos
  25. score = round(-(player.y_pos - starting_height) / win.get_height())
  26.  
  27. colors = [
  28. name for name in pygame.color.THECOLORS
  29. # avoid hard to see colors
  30. if re.search('(red|green|blue)', name)
  31. ]
  32.  
  33.  
  34. def random_sprite(inside, min_size=(100, 100), max_size=(200, 200)):
  35. min_width, min_height = min_size
  36. max_width, max_height = max_size
  37. sprite = pygame.sprite.Sprite()
  38. size = (
  39. random.randint(min_width, max_width),
  40. random.randint(min_height, max_height)
  41. )
  42. position = (
  43. random.randint(0, inside.width - size[0]),
  44. random.randint(0, inside.height - size[1])
  45. )
  46. sprite.rect = pygame.Rect(position, size)
  47. color = random.choice(colors)
  48. sprite.image = pygame.Surface(sprite.rect.size)
  49. sprite.image.fill(color)
  50. return sprite
  51.  
  52.  
  53. frame = win.get_rect()
  54. camera = win.get_rect()
  55. space = frame.inflate(frame.width / 2, frame.height / 2)
  56.  
  57. camera.center = 100, player.y_pos
  58.  
  59. beans = [random_sprite(space) for _ in range(1)]
  60.  
  61.  
  62. def check_collisions(a_x, a_y, a_width, a_height, b_x, b_y, b_width, b_height):
  63. return (a_x + a_width > b_x) and (a_x < b_x + b_width) and (a_y + a_height > b_y) and (a_y < b_y + b_height)
  64.  
  65.  
  66. def draw():
  67. camera.center = player.x_pos, player.y_pos
  68. win.fill(background_rgb)
  69. for bean in beans:
  70. if check_collisions(player.x_pos - camera.x, player.y_pos - camera.y, player.current_img.get_width(), player.current_img.get_height(), bean.rect.x - camera.x, bean.rect.y - camera.y, bean.image.get_width(), bean.image.get_height()):
  71. print(player.x_pos, player.y_pos, bean.rect.x, bean.rect.y,)
  72. win.blit(bean.image, (bean.rect.x, bean.rect.y - camera.y))
  73. win.blit(player.current_img, (player.x_pos, player.y_pos))
  74. camera.center = player.x_pos, player.y_pos
  75.  
  76.  
  77. def update_positions():
  78. camera.center = player.x_pos, player.y_pos
  79. if player.x_pos >= width - 64:
  80. player.current_img = player.right_img
  81. player.x_vel = -player.x_vel
  82. elif player.x_pos <= 0:
  83. player.current_img = player.left_img
  84. player.x_vel = -player.x_vel
  85.  
  86. player.y_vel += gravity
  87.  
  88. player.x_pos += player.x_vel
  89. # player.y_pos += player.y_vel
  90. camera.center = player.x_pos, player.y_pos
  91.  
  92.  
  93. def check_keys():
  94. global space_debounce
  95. keys = pygame.key.get_pressed()
  96. if keys[pygame.K_UP]:
  97. player.y_pos += flap_power
  98. if keys[pygame.K_DOWN]:
  99. player.y_pos -= flap_power
  100. if keys[pygame.K_RIGHT]:
  101. player.x_pos -= flap_power
  102. if keys[pygame.K_LEFT]:
  103. player.x_pos += flap_power
  104. # if keys[pygame.K_SPACE] and not space_debounce:
  105. # player.y_vel = flap_power
  106. # space_debounce = True
  107. # if not keys[pygame.K_SPACE] and space_debounce:
  108. # space_debounce = False
  109.  
  110.  
  111. while True:
  112. clock.tick(fps)
  113. for event in pygame.event.get():
  114. if event.type == pygame.QUIT:
  115. pygame.quit()
  116. draw()
  117. update_positions()
  118. check_keys()
  119. pygame.display.update()
  120.  
Advertisement
Add Comment
Please, Sign In to add comment