Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pygame import *
- class GameSprite(sprite.Sprite):
- def __init__(self, player_image, player_x, player_y, player_speed, width, height):
- super().__init__()
- self.image = transform.scale(image.load(player_image), (width, height))
- self.speed = player_speed
- self.rect = self.image.get_rect()
- self.rect.x = player_x
- self.rect.y = player_y
- def reset(self):
- window.blit(self.image, (self.rect.x, self.rect.y))
- class Player(GameSprite):
- def update_r(self):
- keys = key.get_pressed()
- if keys[K_UP] and self.rect.y > 5:
- self.rect.y -= self.speed
- if keys[K_DOWN] and self.rect.y < win_height - 80:
- self.rect.y += self.speed
- def update_l(self):
- keys = key.get_pressed()
- if keys[K_w] and self.rect.y > 5:
- self.rect.y -= self.speed
- if keys[K_s] and self.rect.y < win_height - 80:
- self.rect.y += self.speed
- # Background color (background - light blue in example)
- back = (100, 255, 255)
- win_width = 600
- win_height = 500
- window = display.set_mode((win_width, win_height))
- display.set_caption("The name of your game")
- window.fill(back)
- # Flags responsible for game state
- game = True
- finish = False
- clock = time.Clock()
- FPS = 60
- # Creating ball and paddles - Update the player_image with your file name
- # Inside the bracket: player_image, player_x, player_y, player_speed, width, height
- # You may change the size of your characters in the last two numbers
- racket1 = Player('your_picture.png', 30, 200, 4, 50, 150)
- racket2 = Player('your_picture.png', 520, 200, 4, 50, 150)
- ball = GameSprite('your_picture.png', 200, 200, 4, 50, 50)
- font.init()
- font = font.SysFont('comicsansms', 40)
- lose1 = font.render('PLAYER 2 WINS!', True, (180, 0, 0))
- lose2 = font.render('PLAYER 1 WINS!', True, (180, 0, 0))
- speed_x = 3
- speed_y = 3
- # GAME LOOP
- while game:
- for e in event.get():
- if e.type == QUIT:
- game = False
- if finish != True:
- window.fill(back) # Fill the background with colour
- racket1.update_l() # Update the positions of your rackets/ paddles
- racket2.update_r()
- ball.rect.x += speed_x # CONSTANT MOVEMENT OF THE BALL
- ball.rect.y += speed_y
- if sprite.collide_rect(racket1, ball) or sprite.collide_rect(racket2, ball):
- speed_x *= -1
- speed_y *= 1
- # If the ball reaches screen edges, change its movement direction
- if ball.rect.y > win_height-50 or ball.rect.y < 0:
- speed_y *= -1
- # If ball flies behind this paddle, display loss condition for player 1
- # if ball goes beyond the left side of the screen
- if ball.rect.x < 0:
- finish = True
- window.blit(lose1, (150, 225))
- game_over = True
- # If the ball flies behind this paddle, display loss condition for player 2
- # If ball goes beyond the right side of the screen
- if ball.rect.x > win_width:
- finish = True
- window.blit(lose2, (150, 225))
- game_over = True
- racket1.reset()
- racket2.reset()
- ball.reset()
- display.update()
- clock.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement