Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- SIZE = (1000, 600)
- FRAMES = 60
- win = pygame.display.set_mode(SIZE)
- clock = pygame.time.Clock()
- class Player:
- def __init__(self, x, y):
- self.size = (15, 120)
- self.x = x
- self.y = y
- def draw(self, surface):
- pygame.draw.rect(surface, (255, 255, 255),
- pygame.Rect((self.x, self.y), self.size))
- class Ball:
- def __init__(self):
- self.x = SIZE[0] // 2
- self.y = SIZE[1] // 2
- self.vel = [-5, 0]
- def move(self):
- self.x += self.vel[0]
- self.y += self.vel[1]
- if self.y <= 20 or self.y >= SIZE[1] - 20:
- self.vel[1] *= -1
- if (self.x <= 45 and bot.y < self.y < bot.y + bot.size[1]) or (self.x >= SIZE[0] - 45 and p.y < self.y < p.y + p.size[1]):
- self.vel[0] *= -1
- def draw(self, surface):
- pygame.draw.circle(surface, (255, 255, 255), (self.x, self.y), 10)
- class Bot(Player):
- def __init__(self, x, y):
- super().__init__(x, y)
- self.ball_vel = b.vel
- self.ball_x = b.x
- self.ball_y = b.y
- def draw_screen(surface):
- surface.fill((0, 0, 0))
- p.draw(surface)
- b.draw(surface)
- bot.draw(surface)
- b.move()
- pygame.display.update()
- def key_handler():
- keys = pygame.key.get_pressed()
- if (keys[pygame.K_UP] or keys[pygame.K_w]) and p.y >= 20:
- p.y -= 5
- elif (keys[pygame.K_DOWN] or keys[pygame.K_s]) and p.y + p.size[1] <= SIZE[1] - 20:
- p.y += 5
- def main_loop():
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- key_handler()
- draw_screen(win)
- clock.tick(FRAMES)
- if __name__ == "__main__":
- pygame.init()
- p = Player(SIZE[0] - 45, SIZE[1] // 2 - 60)
- b = Ball()
- bot = Bot(20, SIZE[1] // 2 - 60)
- main_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement