Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. # Classes file
  2.  
  3. import pygame
  4.  
  5. # Create ball sprite
  6. class Ball(pygame.sprite.Sprite):
  7.     # Used to initiate the ball
  8.     def __init__(self):
  9.         # Calls inhertance (pygame.sprite.Sprite) and initializes the sprite
  10.         super(Ball, self).__init__()
  11.         # Set the image to premade image...
  12.         self.image = pygame.image.load("ball.png")
  13.         # Need to get the rect of the image in order to repos it later on
  14.         self.rect = self.image.get_rect()
  15.  
  16.     # Position function, set the position of the image
  17.     def set_pos(self, x, y):
  18.         self.rect.x = x
  19.         self.rect.y = y
  20.  
  21. class Boundary(pygame.sprite.Sprite):
  22.     def __init__(self, color, width, height):
  23.         super(Boundary, self).__init__()
  24.         self.image = pygame.Surface([width, height])
  25.         self.image.fill(color)
  26.         self.rect = self.image.get_rect()
  27.     def set_pos(self, x, y):
  28.         self.rect.x = x
  29.         self.rect.y = y
  30.  
  31. # main file
  32.  
  33. import pygame
  34. from classes import *
  35. from colors import *
  36.  
  37. pygame.init()
  38.  
  39. # Settings
  40. DISP_W = 1000
  41. DISP_H = 700
  42. FPS = 60
  43. boundary_thickness = 15
  44.  
  45. # Initiate window
  46. GAME_DISP = pygame.display.set_mode((DISP_W, DISP_H))
  47. pygame.display.set_caption("Pong")
  48.  
  49. # Allows us to limit the tick rate
  50. clock = pygame.time.Clock()
  51.  
  52. # Sprite groups
  53. ball_group = pygame.sprite.Group()
  54. collision_group = pygame.sprite.Group()
  55.  
  56. # Initiating sprites
  57. ball = Ball()
  58. ceil = Boundary(grey, DISP_W, boundary_thickness)
  59. floor = Boundary(grey, DISP_W, boundary_thickness)
  60.  
  61. # Adding to groups
  62. ball_group.add(ball)
  63. collision_group.add(ceil)
  64. collision_group.add(floor)
  65.  
  66.  
  67. # ----- Game Loop ----- #
  68.  
  69. # Setting the loop breakers
  70. game_exit = False
  71.  
  72. # ball positon/velocity
  73. ball_x = DISP_W/2
  74. ball_y = DISP_H/2
  75. velocity = 5
  76.  
  77. # Game loop
  78. while not game_exit:
  79.     # Gets all events
  80.     for event in pygame.event.get():
  81.         # Close event
  82.         if event.type == pygame.QUIT:
  83.             # Closes game loop
  84.             game_exit = True
  85.  
  86.     # Background fill
  87.     GAME_DISP.fill(black)
  88.  
  89.     # Setting positions
  90.     ball.set_pos(ball_x, ball_y)
  91.     # ceil.set_pos(0, 0)
  92.     # floor.set_pos(0, DISP_H - boundary_thickness)
  93.  
  94.     ball_y += velocity
  95.  
  96.    
  97.  
  98.     # Drawing sprites
  99.     ball_group.draw(GAME_DISP)
  100.     # collision_group.draw(GAME_DISP)
  101.  
  102.     pygame.display.update()
  103.  
  104.     # Setting FPS
  105.     clock.tick(FPS)
  106.  
  107. # ----- Game exit ----- #
  108. pygame.quit()
  109. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement