Advertisement
OtsoSilver

Untitled

Sep 25th, 2021
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. #Настройки окна
  5. WIDTH = 500
  6. HEIGHT = 500
  7. FPS = 60
  8.  
  9. # Pipes
  10. pipes = []
  11. pipecd = 1200
  12. lastPipeTime = 0
  13.  
  14. # Цвета
  15. YELLOW = (255, 255, 0)
  16. SKY = (133, 193, 233)
  17. GREEN = (46, 204, 113)
  18. WHITE = (255, 255, 255)
  19.  
  20. #Инициализация
  21. pygame.init()
  22. screen = pygame.display.set_mode((WIDTH,HEIGHT))
  23. clock = pygame.time.Clock()
  24.  
  25. # Настройки персонажа
  26. bird = pygame.Rect(40, 250, 30, 23)
  27. birdImg = pygame.image.load('fpbs1.png')
  28. points = 0
  29.  
  30. # Шрифты
  31. font = pygame.font.SysFont('comic sans ms', 30)
  32. game_over_font = pygame.font.SysFont('comic sans ms', 50)
  33. game_over_text = game_over_font.render('GAME OVER', 1, WHITE)
  34.  
  35. # Падение
  36. GRAVITY = 0.3
  37. y_change = 0
  38.  
  39. # Прыжок
  40. isJump = False
  41. jumpCount = 10
  42.  
  43. # Настройки труб
  44. pipes = []
  45. pipecd = 1500
  46.  
  47. # Добавление очков
  48. check = []
  49.  
  50. # Временные параметры
  51. clock = pygame.time.Clock()
  52. currentTime = 0
  53. lastPipeTime = 0
  54.  
  55. game_over = False
  56. running = True
  57. while running:
  58.     screen.fill(SKY)
  59.     for i in pygame.event.get():
  60.         if i.type == pygame.QUIT:
  61.             running = False
  62.         if i.type == pygame.KEYDOWN:
  63.             if i.key == pygame.K_SPACE:
  64.                 isJump = True
  65.                 hopCount = 0
  66.  
  67.     # Прыжок
  68.     if isJump:
  69.         hopCount += 1
  70.         bird.top -= 6
  71.         if hopCount == 5:
  72.             y_change = 0
  73.             isJump = False
  74.     # Падение
  75.     else:
  76.         y_change += GRAVITY
  77.         bird.top += y_change
  78.  
  79.    
  80.     # Текущее время
  81.     currentTime = pygame.time.get_ticks()
  82.     if currentTime - lastPipeTime > pipecd:
  83.         width_pipe = 40
  84.         height_up_pipe = random.randint(50,400)
  85.         height_down_pipe = HEIGHT - 100 - height_up_pipe
  86.        
  87.         up_pipe = pygame.Rect(WIDTH, 0, width_pipe, height_up_pipe)
  88.        
  89.        
  90.         y_down_pipe = 100 + height_up_pipe
  91.         down_pipe = pygame.Rect(WIDTH, y_down_pipe,width_pipe, height_down_pipe)
  92.         pipes.append((up_pipe, down_pipe))
  93.        
  94.         middle_pipe = pygame.Rect(WIDTH, height_up_pipe,width_pipe, 100 )
  95.         check.append(middle_pipe)
  96.         pipecd = random.randint(1500,2000)
  97.         lastPipeTime = currentTime
  98.        
  99. # Столкновение
  100.     # Столкновение птички с краями окна игры
  101.     if bird.top < 0 or bird.bottom > HEIGHT:
  102.         game_over = True
  103.    
  104.     screen.blit(birdImg, (bird.left, bird.top))
  105.     for pipe in pipes:
  106.         pygame.draw.rect(screen, GREEN, pipe[0])
  107.         pygame.draw.rect(screen, GREEN, pipe[1])
  108.  
  109.         pipe[0].x -= 2
  110.         pipe[1].x -=2
  111.         if pipe[0].colliderect(bird) or pipe[1].colliderect(bird):
  112.             game_over = True
  113.    
  114.     for ch in check:
  115.         ch.x -=  2
  116.         if ch.colliderect(bird):
  117.             points += 1
  118.             check.remove(ch)
  119.     point_text = font.render(points, True, WHITE)
  120.     screen.blit(point_text, (245,245))
  121.  
  122.     if game_over:
  123.         screen.fill(SKY)
  124.         screen.blit(game_over_text, (80,120))
  125.        
  126.     clock.tick(FPS)
  127.     pygame.display.update()
  128. pygame.quit()
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement