Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import colour
- SCREEN_WIDTH = 1280
- SCREEN_HEIGHT = 720
- 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
- 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
- def jump(self):
- """ Called when user hits 'jump' button. """
- # move down a bit and see if there is a platform below us.
- # Move down 2 pixels because it doesn't work well if we only move down
- # 1 when working with a platform moving down.
- self.rect.y += 2
- platform_hit_list = pygame.sprite.spritecollide(self, self.level.platform_list, False)
- self.rect.y -= 2
- # If it is ok to jump, set our speed upwards
- if len(platform_hit_list) > 0 or self.rect.bottom >= SCREEN_HEIGHT:
- self.change_y = -10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement