Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- BLUE = (0, 0, 255)
- GREEN = (0, 255, 0)
- RED = (255, 0, 0)
- GREY = (160, 160, 160)
- PURPLE = (127, 0, 255)
- class SpriteSheet(object):
- """ Class used to grab images out of a sprite sheet. """
- def __init__(self, file_name):
- """ Constructor. Pass in the file name of the sprite sheet. """
- # Load the sprite sheet.
- self.sprite_sheet = pygame.image.load(file_name).convert()
- def get_image(self, x, y, width, height):
- """ Grab a single image out of a larger spritesheet
- Pass in the x, y location of the sprite
- and the width and height of the sprite. """
- # Create a new blank image
- image = pygame.Surface([width, height]).convert()
- # Copy the sprite from the large sheet onto the smaller image
- image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
- # Assuming black works as the transparent color
- image.set_colorkey(BLACK)
- # Return the image
- return image
- class Wall(pygame.sprite.Sprite):
- """ This class represents the walls that confine the player """
- def __init__(self, x, y, width, height, color):
- """ Constructor function """
- # Call the parent's constructor
- super().__init__()
- # Setting width, height, and color of wall
- self.image = pygame.Surface([width, height])
- self.image.fill(color)
- # Make our top-left corner the passed-in location.
- self.rect = self.image.get_rect()
- self.rect.y = y
- self.rect.x = x
- class Enemy(pygame.sprite.Sprite):
- """ This class represents the enemy sprites """
- def __init__(self, x, y, width, height, color):
- super().__init__()
- self.image = pygame.Surface([width, height])
- self.image.fill(color)
- self.rect = self.image.get_rect()
- self.rect.y = y
- self.rect.x = x
- class Player(pygame.sprite.Sprite):
- """ This class represents the character you play as """
- # Set speed vector
- def __init__(self, x, y):
- """ Constructor function """
- # Call the parent's constructor
- super().__init__()
- self.change_x = 0
- self.change_y = 0
- self.walking_frames_l = []
- self.walking_frames_r = []
- self.walking_frames_u = []
- self.walking_frames_d = []
- self.direction = "R"
- sprite_sheet = SpriteSheet("character.png")
- image = sprite_sheet.get_image(1, 6, 15, 22)
- self.walking_frames_d.append(image)
- image = sprite_sheet.get_image(17, 6, 15, 22)
- self.walking_frames_d.append(image)
- image = sprite_sheet.get_image(33, 6, 15, 22)
- self.walking_frames_d.append(image)
- image = sprite_sheet.get_image(49, 6, 15, 22)
- self.walking_frames_d.append(image)
- image = sprite_sheet.get_image(0, 69, 15, 23)
- self.walking_frames_u.append(image)
- image = sprite_sheet.get_image(16, 69, 15, 23)
- self.walking_frames_u.append(image)
- image = sprite_sheet.get_image(32, 69, 15, 23)
- self.walking_frames_u.append(image)
- image = sprite_sheet.get_image(48, 69, 15, 23)
- self.walking_frames_u.append(image)
- image = sprite_sheet.get_image(2, 38, 13, 22)
- self.walking_frames_r.append(image)
- image = sprite_sheet.get_image(18, 38, 13, 22)
- self.walking_frames_r.append(image)
- image = sprite_sheet.get_image(34, 38, 13, 22)
- self.walking_frames_r.append(image)
- image = sprite_sheet.get_image(50, 38, 13, 22)
- self.walking_frames_r.append(image)
- image = sprite_sheet.get_image(1, 102, 13, 22)
- self.walking_frames_l.append(image)
- image = sprite_sheet.get_image(17, 102, 13, 22)
- self.walking_frames_l.append(image)
- image = sprite_sheet.get_image(33, 102, 13, 22)
- self.walking_frames_l.append(image)
- image = sprite_sheet.get_image(49, 102, 13, 22)
- self.walking_frames_l.append(image)
- self.image = self.walking_frames_r[0]
- self.rect = self.image.get_rect()
- self.rect.y = y
- self.rect.x = x
- def move(self, walls):
- """ Moves player """
- # Move left/right
- self.change_x = 0
- pos = self.rect.x
- keystate = pygame.key.get_pressed()
- if keystate[pygame.K_a]:
- self.change_x = -3
- self.direction = "L"
- if keystate[pygame.K_d]:
- self.change_x = 3
- self.direction = "R"
- self.rect.x += self.change_x
- if self.direction == "R":
- frame = (pos // 30) % len(self.walking_frames_r)
- self.image = self.walking_frames_r[frame]
- else:
- frame = (pos // 30) % len(self.walking_frames_l)
- self.image = self.walking_frames_l[frame]
- # Did this update cause us to hit a wall?
- block_hit_list = pygame.sprite.spritecollide(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.change_y = 0
- pos = self.rect.y
- keystate = pygame.key.get_pressed()
- if keystate[pygame.K_w]:
- self.change_y = -3
- self.direction = "U"
- if keystate[pygame.K_s]:
- self.change_y = 3
- self.direction = "D"
- self.rect.y += self.change_y
- if self.direction == "D":
- frame = (pos // 30) % len(self.walking_frames_d)
- self.image = self.walking_frames_d[frame]
- else:
- frame = (pos // 30) % len(self.walking_frames_u)
- self.image = self.walking_frames_u[frame]
- # Check and see if we hit anything
- block_hit_list = pygame.sprite.spritecollide(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
- class Level(object):
- """ Base class for all levels. """
- # Each room has a list of walls, and of enemy sprites.
- wall_list = None
- enemy_list = None
- def __init__(self):
- """ Constructor, create our lists. """
- self.wall_list = pygame.sprite.Group()
- self.enemy_list = pygame.sprite.Group()
- def draw(self, screen):
- """ Draw everything on this level. """
- # Draw the background
- # We don't shift the background as much as the sprites are shifted
- # to give a feeling of depth.
- screen.fill(BLUE)
- screen.blit(self.background, (0, 0))
- class Level_01(Level):
- """This creates all the walls in room 1"""
- def __init__(self):
- super().__init__()
- # Make the walls. (x_pos, y_pos, width, height)
- self.background = pygame.image.load("gamemap.png").convert()
- # This is a list of walls. Each is in the form [x, y, width, height]
- walls = [[0, 32, 64, 800, GREEN],
- [0, 0, 384, 96, GREEN],
- [448, 0, 400, 96, GREEN],
- [736, 32, 64, 768, GREEN],
- [32, 704, 736, 96, GREEN],
- [320, 96, 32, 32, GREY],
- [480, 96, 32, 32, GREY]
- ]
- # Loop through the list. Create the wall, add it to the list
- for item in walls:
- wall = Wall(item[0], item[1], item[2], item[3], item[4])
- self.wall_list.add(wall)
- class Level_02(Level):
- """This creates all the walls in room 2"""
- def __init__(self):
- super().__init__()
- self.background = pygame.image.load("gamemap2.png").convert()
- walls = [[0, 0, 800, 96, GREY],
- [0, 32, 32, 736, GREY],
- [0, 736, 384, 64, GREY],
- [448, 736, 368, 64, GREY],
- [768, 32, 32, 736, GREY]
- ]
- for item in walls:
- wall = Wall(item[0], item[1], item[2], item[3], item[4])
- self.wall_list.add(wall)
- def main():
- """ Main Program """
- # Initializing Pygame
- pygame.init()
- # Window Size
- screen = pygame.display.set_mode([800, 800])
- # Game Name
- pygame.display.set_caption('Game')
- all_sprites_list = pygame.sprite.Group()
- # Creating Player
- player = Player(384, 384)
- player_sprite = pygame.sprite.Group()
- player_sprite.add(player)
- all_sprites_list.add(player)
- levels = []
- level = Level_01()
- levels.append(level)
- level = Level_02()
- levels.append(level)
- current_level_no = 0
- current_level = levels[current_level_no]
- clock = pygame.time.Clock()
- done = False
- while not done:
- # --- Event Processing ---
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- # --- Game Logic ---
- player.move(current_level.wall_list)
- if player.rect.y < 64:
- if current_level_no == 0:
- current_level_no = 1
- current_level = levels[current_level_no]
- player.rect.y = 790
- if player.rect.y > 800:
- if current_level_no == 1:
- current_level_no = 0
- current_level = levels[current_level_no]
- player.rect.y = 65
- # --- Drawing ---
- current_level.draw(screen)
- player_sprite.draw(screen)
- all_sprites_list.update()
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment