Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # date: 2026.09.05
- # [Staging Ground: Problem where programme cannot access items stored in lists made by a function - Stack Overflow](https://stackoverflow.com/staging-ground/80000706)
- import sys
- import pygame
- # from pygame.locals import * # PEP8: `import *` is not preferred
- # --- constants --- # PEP8: `UPPER_CASE_NAMES` for constants
- WINDOW_WIDTH = 1000
- WINDOW_HEIGHT = 800
- MUSKETEER_WIDTH = 50
- MUSKETEER_HEIGHT = 60
- PLAYER_X = 200
- PLAYER_Y = 90
- PLAYER_SPEED = 5
- ENEMY_X = 300
- ENEMY_Y = 90
- ENEMY_SPEED = 3
- BACKGROUND_COLOR = (100, 100, 255)
- UNIT_COLOR = (0, 100, 190)
- FPS = 30
- DIRECTION_RIGHT = 0
- DIRECTION_LEFT = 1
- # --- classes --- # PEP8: `CamelCaseNames` for classes
- # empty
- # --- functions --- # PEP8: `lower_case_names` for functions, (and `verbs` as names`)
- def create_musketeer_animation():
- filenames_right = [
- "musket.w.1.png",
- "musket.w.2.png",
- "musket.w.3.png",
- "musket.w.4.png",
- ]
- filenames_left = [
- "musket.w.1.png",
- "musket.w.2.png",
- "musket.w.3.png",
- "musket.w.4.png",
- ]
- walk_right = []
- for index, name in enumerate(filenames_right):
- # image = pygame.image.load(name)
- image = pygame.Surface((100, 100))
- image.fill("red")
- pygame.draw.rect(image, "white", (60, 20, 20, 20))
- pygame.draw.rect(image, "white", (60, 50, 40, 5))
- image = pygame.transform.scale(image, (MUSKETEER_WIDTH, MUSKETEER_HEIGHT))
- walk_right.append(image)
- walk_left = []
- for index, name in enumerate(filenames_left):
- # image = pygame.image.load(name)
- image = pygame.Surface((100, 100))
- image.fill("red")
- pygame.draw.rect(image, "white", (20, 20, 20, 20))
- pygame.draw.rect(image, "white", (0, 50, 40, 5))
- image = pygame.transform.scale(image, (MUSKETEER_WIDTH, MUSKETEER_HEIGHT))
- walk_left.append(image)
- return [walk_right, walk_left]
- def create_player_animation(color="green"):
- # player_image = pygame.image.load("musket.png")
- # - direction right -
- walk_right = []
- image = pygame.Surface((100, 100))
- image.fill(color)
- pygame.draw.rect(image, "white", (60, 20, 20, 20))
- pygame.draw.rect(image, "white", (60, 50, 40, 5))
- image = pygame.transform.scale(image, (MUSKETEER_WIDTH, MUSKETEER_HEIGHT))
- walk_right.append(image)
- # - direction left -
- walk_left = []
- image = pygame.Surface((100, 100))
- image.fill(color)
- pygame.draw.rect(image, "white", (20, 20, 20, 20))
- pygame.draw.rect(image, "white", (0, 50, 40, 5))
- image = pygame.transform.scale(image, (MUSKETEER_WIDTH, MUSKETEER_HEIGHT))
- walk_left.append(image)
- # - end -
- return [walk_right, walk_left]
- #
- # Guide for indexes of all units:
- # 1st index is unit class: musketeer = [0],
- # 2nd Index is action: Walk is [0], Running is [1]
- # 3rd Index is direction: Right is [0], Left is [1]
- # 4th Index is which part of the animation the units is in
- def add_unit(unit):
- if unit == "musketeer":
- enemy = {
- "unit": "musketeer",
- "animation": musketeer_animation,
- "rect": pygame.Rect(ENEMY_X, ENEMY_Y, MUSKETEER_WIDTH, MUSKETEER_HEIGHT),
- "direction": DIRECTION_RIGHT,
- "frame": 0,
- }
- all_enemies.append(enemy)
- def render_player():
- direction = player["direction"]
- frame = player["frame"]
- # print(f"{direction=} {frame=}")
- # print(f"{len(player['animation'])=}")
- # print(f"{len(player['animation'][direction])=}")
- image = player["animation"][direction][frame]
- rect = player["rect"]
- window_surface.blit(image, rect) # Draws it
- def render_enemies():
- for enemy in all_enemies:
- if enemy["unit"] == "musketeer": # see which unit it is
- direction = enemy["direction"]
- frame = enemy["frame"]
- # print(f"{direction=} {frame=}")
- # print(f"{len(enemy['animation'])=}")
- # print(f"{len(enemy['animation'][direction])=}")
- image = enemy["animation"][direction][frame]
- rect = enemy["rect"]
- window_surface.blit(image, rect) # Draws it
- # --- main ---
- pygame.init()
- window_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
- musketeer_animation = create_musketeer_animation()
- player_animation = create_player_animation()
- player = {
- "unit": "player",
- "animation": player_animation,
- "rect": pygame.Rect(PLAYER_X, PLAYER_Y, MUSKETEER_WIDTH, MUSKETEER_HEIGHT),
- "direction": 0,
- "frame": 0,
- }
- all_enemies = []
- # - main loop -
- clock = pygame.time.Clock()
- run = True
- while run:
- # - events -
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- run = False
- elif event.key == pygame.K_a:
- add_unit("musketeer")
- # - updates without draws -
- # update player position
- if player["rect"].bottom < 400: # use `.bottom` instead of `.y`
- player["rect"].y += ENEMY_SPEED
- key = pygame.key.get_pressed()
- if key[pygame.K_LEFT]:
- player["rect"].x -= PLAYER_SPEED
- player["direction"] = DIRECTION_LEFT
- if key[pygame.K_RIGHT]:
- player["rect"].x += PLAYER_SPEED
- player["direction"] = DIRECTION_RIGHT
- if key[pygame.K_UP]:
- player["rect"].y -= PLAYER_SPEED
- if player["rect"].left < 0:
- player["rect"].left = 0
- if player["rect"].right > WINDOW_WIDTH:
- player["rect"].right = WINDOW_WIDTH
- # update enemies position
- for enemy in all_enemies:
- if enemy["rect"].x < player["rect"].x:
- enemy["rect"].x += ENEMY_SPEED
- enemy["direction"] = DIRECTION_RIGHT # Means move right
- if enemy["rect"].x > player["rect"].x:
- enemy["rect"].x -= ENEMY_SPEED
- enemy["direction"] = DIRECTION_LEFT # Means move left
- if enemy["rect"].bottom < 400: # use `.bottom` instead of `.y`
- enemy["rect"].y += ENEMY_SPEED
- # update player animation
- player["frame"] += 1
- direction = player["direction"]
- if player["frame"] == len(player["animation"][direction]):
- player["frame"] = 0
- # update enemies animation
- for enemy in all_enemies:
- enemy["frame"] += 1
- direction = enemy["direction"]
- if enemy["frame"] == len(enemy["animation"][direction]):
- enemy["frame"] = 0
- # - draws without updates -
- window_surface.fill(BACKGROUND_COLOR)
- render_enemies()
- render_player()
- pygame.draw.rect(window_surface, "black", (0, 400, WINDOW_WIDTH, 50))
- pygame.display.flip()
- clock.tick(FPS)
- # - end -
- pygame.quit()
- sys.exit() # forgot `()`
Advertisement
Add Comment
Please, Sign In to add comment