Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- screen = pygame.display.set_mode((900, 500))
- pygame.display.set_caption('My game')
- clock = pygame.time.Clock()
- player = pygame.rect.Rect(10, 200, 20, 100)
- bot = pygame.rect.Rect(870, 200, 20, 100)
- ball = pygame.rect.Rect(445, 245, 10, 10)
- running = True
- player_speed_y = 0
- ball_speed_x = -2
- ball_speed_y = 2
- def move_bot():
- if ball.centery > bot.centery and ball.centerx>450:
- bot.y += 1
- if ball.centery < bot.centery and ball.centerx>450:
- bot.y -= 1
- def ball_check():
- global ball_speed_y
- global ball_speed_x
- if ball.top <= 0:
- ball_speed_y = -ball_speed_y
- if ball.bottom >= 500:
- ball_speed_y = - ball_speed_y
- if ball.colliderect(bot) or ball.colliderect(player):
- ball_speed_x = -ball_speed_x
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_UP:
- player_speed_y -= 1
- if event.key == pygame.K_DOWN:
- player_speed_y += 1
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_UP:
- player_speed_y += 1
- if event.key == pygame.K_DOWN:
- player_speed_y -= 1
- player.y += player_speed_y
- ball.x += ball_speed_x
- ball.y += ball_speed_y
- ball_check()
- move_bot()
- screen.fill((0, 0, 0))
- pygame.draw.rect(screen, (20, 200, 20), player)
- pygame.draw.rect(screen, (200, 20, 20), bot)
- pygame.draw.ellipse(screen, (255, 255, 255), ball)
- pygame.display.flip()
- clock.tick(60)
Advertisement
Add Comment
Please, Sign In to add comment