Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Classes file
- import pygame
- # Create ball sprite
- class Ball(pygame.sprite.Sprite):
- # Used to initiate the ball
- def __init__(self):
- # Calls inhertance (pygame.sprite.Sprite) and initializes the sprite
- super(Ball, self).__init__()
- # Set the image to premade image...
- self.image = pygame.image.load("ball.png")
- # Need to get the rect of the image in order to repos it later on
- self.rect = self.image.get_rect()
- # Position function, set the position of the image
- def set_pos(self, x, y):
- self.rect.x = x
- self.rect.y = y
- class Boundary(pygame.sprite.Sprite):
- def __init__(self, color, width, height):
- super(Boundary, self).__init__()
- self.image = pygame.Surface([width, height])
- self.image.fill(color)
- self.rect = self.image.get_rect()
- def set_pos(self, x, y):
- self.rect.x = x
- self.rect.y = y
- # main file
- import pygame
- from classes import *
- from colors import *
- pygame.init()
- # Settings
- DISP_W = 1000
- DISP_H = 700
- FPS = 60
- boundary_thickness = 15
- # Initiate window
- GAME_DISP = pygame.display.set_mode((DISP_W, DISP_H))
- pygame.display.set_caption("Pong")
- # Allows us to limit the tick rate
- clock = pygame.time.Clock()
- # Sprite groups
- ball_group = pygame.sprite.Group()
- collision_group = pygame.sprite.Group()
- # Initiating sprites
- ball = Ball()
- ceil = Boundary(grey, DISP_W, boundary_thickness)
- floor = Boundary(grey, DISP_W, boundary_thickness)
- # Adding to groups
- ball_group.add(ball)
- collision_group.add(ceil)
- collision_group.add(floor)
- # ----- Game Loop ----- #
- # Setting the loop breakers
- game_exit = False
- # ball positon/velocity
- ball_x = DISP_W/2
- ball_y = DISP_H/2
- velocity = 5
- # Game loop
- while not game_exit:
- # Gets all events
- for event in pygame.event.get():
- # Close event
- if event.type == pygame.QUIT:
- # Closes game loop
- game_exit = True
- # Background fill
- GAME_DISP.fill(black)
- # Setting positions
- ball.set_pos(ball_x, ball_y)
- # ceil.set_pos(0, 0)
- # floor.set_pos(0, DISP_H - boundary_thickness)
- ball_y += velocity
- # Drawing sprites
- ball_group.draw(GAME_DISP)
- # collision_group.draw(GAME_DISP)
- pygame.display.update()
- # Setting FPS
- clock.tick(FPS)
- # ----- Game exit ----- #
- pygame.quit()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement