Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import colour
- import pygame
- SCREEN_WIDTH = 1280
- SCREEN_HEIGHT = 720
- class Wall(pygame.sprite.Sprite):
- def __init__(self, x, y, width, height, col):
- super().__init__()
- # Colour and size
- self.image = pygame.Surface([width, height])
- self.image.fill(col)
- # Starting location
- self.rect = self.image.get_rect()
- self.rect.y = y
- self.rect.x = x
- class Player(pygame.sprite.Sprite):
- # Constructor Function
- def __init__(self, x, y):
- # Calls The Parent Constructor
- super().__init__()
- # set image height/width
- self.image = pygame.Surface([15, 15])
- self.image.fill(colour.WHITE)
- # Make our top-left corner the passed-in location.
- self.rect = self.image.get_rect()
- self.rect.y = y
- self.rect.x = x
- # set speed vector
- self.change_x = 0
- self.change_y = 0
- self.walls = None
- def changespeed(self, x, y):
- """ Change the speed of the player. """
- self.change_x += x
- self.change_y += y
- def update(self):
- """ Gravity"""
- self.calc_grav()
- """ Update the player position. """
- # move left/right
- self.rect.x += self.change_x
- # did this update cause us to hit a wall?
- block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
- for block in block_hit_list:
- # if we are moving right, set our right side to the left side of the item we hit
- if self.change_x > 0:
- self.rect.right = block.rect.left
- else:
- # Otherwise if we are moving left, do the opposite
- self.rect.left = block.rect.right
- # move up/down
- self.rect.y += self.change_y
- # Check and see if we hit anything
- block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
- for block in block_hit_list:
- # Reset our position based on the top/bottom of the object.
- if self.change_y > 0:
- self.rect.bottom = block.rect.top
- else:
- self.rect.top = block.rect.bottom
- def calc_grav(self):
- """ Calculate effect of gravity. """
- if self.change_y == 0:
- self.change_y = 1
- else:
- self.change_y += .35
- # See if we are on the ground.
- if self.rect.y >= SCREEN_HEIGHT - self.rect.height and self.change_y >= 0:
- self.change_y = 0
- self.rect.y = SCREEN_HEIGHT - self.rect.height
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement