Advertisement
Frogboxe

Stackoverflow_PygameGenerators

May 27th, 2017
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. import pygame
  2. import time
  3. import random
  4.  
  5. class Player:
  6.     def __init__(self, surface, surface_height, surface_width, size, jump_height, color):
  7.         self.surface = surface
  8.         self.surface_height = surface_height
  9.         self.surface_width = surface_width
  10.         self.size = size
  11.         self.jump_height = jump_height
  12.         self.color = color
  13.         self.x = (0 + (self.surface_width/10))
  14.         self.y = self.surface_height - self.size
  15.         self.x_move = 0
  16.         self.y_move = 0
  17.         self.rect = pygame.draw.rect(self.surface, self.color, [self.x, self.y, self.size, self.size])
  18.  
  19.     def keyDown(self, key):
  20.         if key == pygame.K_w:
  21.             self.y -= 10
  22.         elif key == pygame.K_a:
  23.             self.x -= 10
  24.         elif key == pygame.K_d:
  25.             self.x += 10
  26.  
  27.  
  28.     def checkBoundaries(self):
  29.         # Checking for Boundaries
  30.         #if self.y <= self.jump_height:
  31.         #    self.y_move = -10
  32.         if self.y > self.surface_height - self.size:
  33.             self.y_move = 0
  34.         if self.y <= self.jump_height + self.size:
  35.             self.y_move = -10
  36.  
  37.     def draw(self):
  38.         pygame.draw.rect(self.surface, self.color, [self.x, self.y, self.size, self.size])
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. # Initialize Pygame
  46. pygame.init()
  47. game_over = False
  48.  
  49. # Color
  50. BLACK = (0, 0, 0)
  51. WHITE = (255, 255, 255)
  52. GRAY = (192, 192, 192)
  53.  
  54. # Setting up the window
  55. surfaceWidth = 800
  56. surfaceHeight = 500
  57.  
  58. surface = pygame.display.set_mode((surfaceWidth, surfaceHeight))
  59. pygame.display.set_caption("Rectangle Runner")
  60. clock = pygame.time.Clock()
  61.  
  62. #Player(surface, surface_height, surface_width, size, jump_height, color)
  63. player = Player(surface, surfaceHeight, surfaceWidth, 50, 200, BLACK)
  64.  
  65. # Game Loop
  66. while True:
  67.     for event in pygame.event.get():
  68.         if event.type == pygame.QUIT:
  69.             game_over = True
  70.         if event.type == pygame.KEYDOWN:
  71.             player.keyDown(event.key)
  72.         if game_over == True:
  73.             pygame.quit()
  74.             quit()
  75.     surface.fill(GRAY)
  76.     player.checkBoundaries()
  77.     player.draw()
  78.     pygame.display.update()
  79.     clock.tick(60)
  80.  
  81. pygame.quit()
  82. quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement