Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import time
- import random
- class Player:
- def __init__(self, surface, surface_height, surface_width, size, jump_height, color):
- self.surface = surface
- self.surface_height = surface_height
- self.surface_width = surface_width
- self.size = size
- self.jump_height = jump_height
- self.color = color
- self.x = (0 + (self.surface_width/10))
- self.y = self.surface_height - self.size
- self.x_move = 0
- self.y_move = 0
- self.rect = pygame.draw.rect(self.surface, self.color, [self.x, self.y, self.size, self.size])
- def keyDown(self, key):
- if key == pygame.K_w:
- self.y -= 10
- elif key == pygame.K_a:
- self.x -= 10
- elif key == pygame.K_d:
- self.x += 10
- def checkBoundaries(self):
- # Checking for Boundaries
- #if self.y <= self.jump_height:
- # self.y_move = -10
- if self.y > self.surface_height - self.size:
- self.y_move = 0
- if self.y <= self.jump_height + self.size:
- self.y_move = -10
- def draw(self):
- pygame.draw.rect(self.surface, self.color, [self.x, self.y, self.size, self.size])
- # Initialize Pygame
- pygame.init()
- game_over = False
- # Color
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- GRAY = (192, 192, 192)
- # Setting up the window
- surfaceWidth = 800
- surfaceHeight = 500
- surface = pygame.display.set_mode((surfaceWidth, surfaceHeight))
- pygame.display.set_caption("Rectangle Runner")
- clock = pygame.time.Clock()
- #Player(surface, surface_height, surface_width, size, jump_height, color)
- player = Player(surface, surfaceHeight, surfaceWidth, 50, 200, BLACK)
- # Game Loop
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- game_over = True
- if event.type == pygame.KEYDOWN:
- player.keyDown(event.key)
- if game_over == True:
- pygame.quit()
- quit()
- surface.fill(GRAY)
- player.checkBoundaries()
- player.draw()
- pygame.display.update()
- clock.tick(60)
- pygame.quit()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement