Shcoder001

Pygame part3

Feb 12th, 2023
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. import pygame
  2. import pygame.freetype
  3.  
  4. pygame.init()
  5. screen = pygame.display.set_mode((900, 500))
  6. pygame.display.set_caption('My game')
  7. clock = pygame.time.Clock()
  8. font = pygame.freetype.Font(None, 30)
  9. player = pygame.rect.Rect(10, 200, 20, 100)
  10. bot = pygame.rect.Rect(870, 200, 20, 100)
  11. ball = pygame.rect.Rect(445, 245, 10, 10)
  12. running = True
  13. game_status = 'game'
  14. player_speed_y = 0
  15. ball_speed_x = -2
  16. ball_speed_y = 2
  17. max_score = 1
  18. score = [0, 0]
  19.  
  20.  
  21. def move_bot():
  22.     if ball.centery > bot.centery and ball.centerx > 450:
  23.         bot.y += 1
  24.     if ball.centery < bot.centery and ball.centerx > 450:
  25.         bot.y -= 1
  26.  
  27.  
  28. def ball_respawn():
  29.     global game_status
  30.     ball.center = (450, 250)
  31.     if score[0] >= max_score or score[1] >= max_score:
  32.         game_status = 'menu'
  33.  
  34.  
  35. def ball_check():
  36.     global ball_speed_y
  37.     global ball_speed_x
  38.     if ball.top <= 0:
  39.         ball_speed_y = -ball_speed_y
  40.     if ball.bottom >= 500:
  41.         ball_speed_y = - ball_speed_y
  42.     if ball.colliderect(bot) or ball.colliderect(player):
  43.         ball_speed_x = -ball_speed_x
  44.     if ball.right < 0:
  45.         score[1] += 1
  46.         ball_respawn()
  47.     if ball.left > 900:
  48.         score[0] += 1
  49.         ball_respawn()
  50.  
  51.  
  52. while running:
  53.     for event in pygame.event.get():
  54.         if event.type == pygame.QUIT:
  55.             running = False
  56.         if event.type == pygame.KEYDOWN:
  57.             if game_status == 'menu':
  58.                 game_status = 'game'
  59.                 score = [0, 0]
  60.             if event.key == pygame.K_UP:
  61.                 player_speed_y -= 1
  62.             if event.key == pygame.K_DOWN:
  63.                 player_speed_y += 1
  64.  
  65.         if event.type == pygame.KEYUP:
  66.             if event.key == pygame.K_UP:
  67.                 player_speed_y += 1
  68.             if event.key == pygame.K_DOWN:
  69.                 player_speed_y -= 1
  70.     if game_status == 'game':
  71.         player.y += player_speed_y
  72.         ball.x += ball_speed_x
  73.         ball.y += ball_speed_y
  74.         ball_check()
  75.         move_bot()
  76.     screen.fill((0, 0, 0))
  77.     font.render_to(screen, (440, 20), str(score[0]) + ':' + str(score[1]), (255, 255, 255))
  78.     pygame.draw.rect(screen, (20, 200, 20), player)
  79.     pygame.draw.rect(screen, (200, 20, 20), bot)
  80.     pygame.draw.ellipse(screen, (255, 255, 255), ball)
  81.     if game_status == 'menu':
  82.         if score[0] >= max_score:
  83.             font.render_to(screen, (200, 20), 'Вы выиграли!', (255, 255, 255))
  84.         else:
  85.             font.render_to(screen, (200, 20), 'Вы Проиграли', (255, 255, 255))
  86.     pygame.display.flip()
  87.     clock.tick(60)
  88.  
Advertisement
Add Comment
Please, Sign In to add comment