furas

Python - PyGame - Musketeer (Stackoverflow)

Sep 6th, 2026 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.97 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # date: 2026.09.05
  4. # [Staging Ground: Problem where programme cannot access items stored in lists made by a function - Stack Overflow](https://stackoverflow.com/staging-ground/80000706)
  5.  
  6. import sys
  7. import pygame
  8.  
  9. # from pygame.locals import *  # PEP8: `import *` is not preferred
  10.  
  11. # --- constants ---  # PEP8: `UPPER_CASE_NAMES` for constants
  12.  
  13. WINDOW_WIDTH = 1000
  14. WINDOW_HEIGHT = 800
  15.  
  16. MUSKETEER_WIDTH = 50
  17. MUSKETEER_HEIGHT = 60
  18.  
  19. PLAYER_X = 200
  20. PLAYER_Y = 90
  21. PLAYER_SPEED = 5
  22.  
  23. ENEMY_X = 300
  24. ENEMY_Y = 90
  25. ENEMY_SPEED = 3
  26.  
  27. BACKGROUND_COLOR = (100, 100, 255)
  28. UNIT_COLOR = (0, 100, 190)
  29.  
  30. FPS = 30
  31.  
  32. DIRECTION_RIGHT = 0
  33. DIRECTION_LEFT = 1
  34.  
  35. # --- classes ---  # PEP8: `CamelCaseNames` for classes
  36.  
  37. # empty
  38.  
  39. # --- functions ---  # PEP8: `lower_case_names` for functions, (and `verbs` as names`)
  40.  
  41.  
  42. def create_musketeer_animation():
  43.     filenames_right = [
  44.         "musket.w.1.png",
  45.         "musket.w.2.png",
  46.         "musket.w.3.png",
  47.         "musket.w.4.png",
  48.     ]
  49.     filenames_left = [
  50.         "musket.w.1.png",
  51.         "musket.w.2.png",
  52.         "musket.w.3.png",
  53.         "musket.w.4.png",
  54.     ]
  55.  
  56.     walk_right = []
  57.     for index, name in enumerate(filenames_right):
  58.         # image = pygame.image.load(name)
  59.         image = pygame.Surface((100, 100))
  60.         image.fill("red")
  61.         pygame.draw.rect(image, "white", (60, 20, 20, 20))
  62.         pygame.draw.rect(image, "white", (60, 50, 40, 5))
  63.  
  64.         image = pygame.transform.scale(image, (MUSKETEER_WIDTH, MUSKETEER_HEIGHT))
  65.         walk_right.append(image)
  66.  
  67.     walk_left = []
  68.     for index, name in enumerate(filenames_left):
  69.         # image = pygame.image.load(name)
  70.         image = pygame.Surface((100, 100))
  71.         image.fill("red")
  72.         pygame.draw.rect(image, "white", (20, 20, 20, 20))
  73.         pygame.draw.rect(image, "white", (0, 50, 40, 5))
  74.  
  75.         image = pygame.transform.scale(image, (MUSKETEER_WIDTH, MUSKETEER_HEIGHT))
  76.         walk_left.append(image)
  77.  
  78.     return [walk_right, walk_left]
  79.  
  80.  
  81. def create_player_animation(color="green"):
  82.     # player_image = pygame.image.load("musket.png")
  83.  
  84.     # - direction right -
  85.  
  86.     walk_right = []
  87.  
  88.     image = pygame.Surface((100, 100))
  89.     image.fill(color)
  90.  
  91.     pygame.draw.rect(image, "white", (60, 20, 20, 20))
  92.     pygame.draw.rect(image, "white", (60, 50, 40, 5))
  93.  
  94.     image = pygame.transform.scale(image, (MUSKETEER_WIDTH, MUSKETEER_HEIGHT))
  95.     walk_right.append(image)
  96.  
  97.     # - direction left -
  98.  
  99.     walk_left = []
  100.  
  101.     image = pygame.Surface((100, 100))
  102.     image.fill(color)
  103.     pygame.draw.rect(image, "white", (20, 20, 20, 20))
  104.     pygame.draw.rect(image, "white", (0, 50, 40, 5))
  105.  
  106.     image = pygame.transform.scale(image, (MUSKETEER_WIDTH, MUSKETEER_HEIGHT))
  107.     walk_left.append(image)
  108.  
  109.     # - end -
  110.  
  111.     return [walk_right, walk_left]
  112.  
  113.  
  114. #
  115.  
  116. # Guide for indexes of all units:
  117. # 1st index is unit class: musketeer = [0],
  118. # 2nd Index is action: Walk is [0], Running is [1]
  119. # 3rd Index is direction: Right is [0], Left is [1]
  120. # 4th Index is which part of the animation the units is in
  121.  
  122.  
  123. def add_unit(unit):
  124.     if unit == "musketeer":
  125.         enemy = {
  126.             "unit": "musketeer",
  127.             "animation": musketeer_animation,
  128.             "rect": pygame.Rect(ENEMY_X, ENEMY_Y, MUSKETEER_WIDTH, MUSKETEER_HEIGHT),
  129.             "direction": DIRECTION_RIGHT,
  130.             "frame": 0,
  131.         }
  132.         all_enemies.append(enemy)
  133.  
  134.  
  135. def render_player():
  136.     direction = player["direction"]
  137.     frame = player["frame"]
  138.  
  139.     # print(f"{direction=} {frame=}")
  140.     # print(f"{len(player['animation'])=}")
  141.     # print(f"{len(player['animation'][direction])=}")
  142.  
  143.     image = player["animation"][direction][frame]
  144.     rect = player["rect"]
  145.     window_surface.blit(image, rect)  # Draws it
  146.  
  147.  
  148. def render_enemies():
  149.     for enemy in all_enemies:
  150.         if enemy["unit"] == "musketeer":  # see which unit it is
  151.             direction = enemy["direction"]
  152.             frame = enemy["frame"]
  153.  
  154.             # print(f"{direction=} {frame=}")
  155.             # print(f"{len(enemy['animation'])=}")
  156.             # print(f"{len(enemy['animation'][direction])=}")
  157.  
  158.             image = enemy["animation"][direction][frame]
  159.             rect = enemy["rect"]
  160.             window_surface.blit(image, rect)  # Draws it
  161.  
  162.  
  163. # --- main ---
  164.  
  165.  
  166. pygame.init()
  167. window_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
  168.  
  169. musketeer_animation = create_musketeer_animation()
  170. player_animation = create_player_animation()
  171.  
  172. player = {
  173.     "unit": "player",
  174.     "animation": player_animation,
  175.     "rect": pygame.Rect(PLAYER_X, PLAYER_Y, MUSKETEER_WIDTH, MUSKETEER_HEIGHT),
  176.     "direction": 0,
  177.     "frame": 0,
  178. }
  179.  
  180. all_enemies = []
  181.  
  182. # - main loop -
  183.  
  184. clock = pygame.time.Clock()
  185. run = True
  186.  
  187. while run:
  188.     # - events -
  189.  
  190.     for event in pygame.event.get():
  191.         if event.type == pygame.QUIT:
  192.             run = False
  193.         if event.type == pygame.KEYDOWN:
  194.             if event.key == pygame.K_ESCAPE:
  195.                 run = False
  196.             elif event.key == pygame.K_a:
  197.                 add_unit("musketeer")
  198.  
  199.     # - updates without draws -
  200.  
  201.     # update player position
  202.  
  203.     if player["rect"].bottom < 400:  # use `.bottom` instead of `.y`
  204.         player["rect"].y += ENEMY_SPEED
  205.  
  206.     key = pygame.key.get_pressed()
  207.     if key[pygame.K_LEFT]:
  208.         player["rect"].x -= PLAYER_SPEED
  209.         player["direction"] = DIRECTION_LEFT
  210.     if key[pygame.K_RIGHT]:
  211.         player["rect"].x += PLAYER_SPEED
  212.         player["direction"] = DIRECTION_RIGHT
  213.     if key[pygame.K_UP]:
  214.         player["rect"].y -= PLAYER_SPEED
  215.  
  216.     if player["rect"].left < 0:
  217.         player["rect"].left = 0
  218.     if player["rect"].right > WINDOW_WIDTH:
  219.         player["rect"].right = WINDOW_WIDTH
  220.  
  221.     # update enemies position
  222.  
  223.     for enemy in all_enemies:
  224.         if enemy["rect"].x < player["rect"].x:
  225.             enemy["rect"].x += ENEMY_SPEED
  226.             enemy["direction"] = DIRECTION_RIGHT  # Means move right
  227.  
  228.         if enemy["rect"].x > player["rect"].x:
  229.             enemy["rect"].x -= ENEMY_SPEED
  230.             enemy["direction"] = DIRECTION_LEFT  # Means move left
  231.  
  232.         if enemy["rect"].bottom < 400:  # use `.bottom` instead of `.y`
  233.             enemy["rect"].y += ENEMY_SPEED
  234.  
  235.     # update player animation
  236.  
  237.     player["frame"] += 1
  238.     direction = player["direction"]
  239.     if player["frame"] == len(player["animation"][direction]):
  240.         player["frame"] = 0
  241.  
  242.     # update enemies animation
  243.  
  244.     for enemy in all_enemies:
  245.         enemy["frame"] += 1
  246.         direction = enemy["direction"]
  247.         if enemy["frame"] == len(enemy["animation"][direction]):
  248.             enemy["frame"] = 0
  249.  
  250.     # - draws without updates -
  251.  
  252.     window_surface.fill(BACKGROUND_COLOR)
  253.  
  254.     render_enemies()
  255.     render_player()
  256.  
  257.     pygame.draw.rect(window_surface, "black", (0, 400, WINDOW_WIDTH, 50))
  258.  
  259.     pygame.display.flip()
  260.  
  261.     clock.tick(FPS)
  262.  
  263. # - end -
  264.  
  265. pygame.quit()
  266. sys.exit()  # forgot `()`
Advertisement
Add Comment
Please, Sign In to add comment