Last active
June 18, 2024 15:17
-
-
Save horstjens/3b288323f6d953184beb0787aff565fd to your computer and use it in GitHub Desktop.
pygame platformer for Linus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import os | |
# Example file showing a circle moving on screen | |
W,H = 1280, 720 | |
FPS = 60 | |
APS = 4 # animations per second | |
# pygame setup | |
pygame.init() | |
screen = pygame.display.set_mode((W, H)) | |
clock = pygame.time.Clock() | |
running = True | |
dt = 0 | |
i = 0 | |
a = 0 | |
all_images = os.listdir("data") | |
all_images = [name for name in all_images if name.startswith("character_robot_") and name.endswith(".png")] | |
pics = {} | |
for name in all_images: | |
middle = name[16:-4] | |
while middle[-1].isnumeric(): | |
middle = middle[:-1] | |
print(middle) | |
pics[middle] = [] | |
print(pics) | |
all_states = list(pics.keys()) | |
print(all_states) | |
#pics = {"wide":[], | |
# "behindBack":[], | |
# "show":[], | |
# "walk":[], # 8 | |
# "climb":[], # 2 | |
# } | |
for state in pics: | |
images_found = [name for name in all_images if name.startswith(f"character_robot_{state}") and name.endswith(".png")] | |
for image_name in images_found: | |
p = pygame.image.load(os.path.join("data",image_name)) | |
pics[state].append(p.convert_alpha()) | |
player_pos = pygame.Vector2(W / 2, H / 2) | |
player_state = "wide" | |
print("ps=", player_state) | |
def next_state(player_state): | |
#if player_state in ("walk","climb"): | |
# return player_state | |
#if player_state == "wide": | |
# new_state = "behindBack" | |
#elif player_state == "behindBack": | |
# new_state = "show" | |
#elif player_state == "show": | |
# new_state = "wide" | |
#return new_state | |
derzeit = all_states.index(player_state) | |
if derzeit == len(all_states)-1: | |
player_state = all_states[0] | |
else: | |
player_state = all_states[derzeit+1] | |
return player_state | |
while running: | |
# poll for events | |
# pygame.QUIT event means the user clicked X to close your window | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_SPACE: | |
player_state = next_state(player_state) | |
# fill the screen with a color to wipe away anything from last frame | |
screen.fill("purple") | |
#pygame.draw.circle(screen, "red", player_pos, 40) | |
try: | |
screen.blit(pics[player_state][i], player_pos) | |
except IndexError: | |
screen.blit(pics[player_state][0], player_pos) | |
keys = pygame.key.get_pressed() | |
moved = False | |
if keys[pygame.K_b]: # duck | |
player_state = "duck" | |
else: | |
player_state = "idle" | |
if keys[pygame.K_w]: | |
moved = True | |
player_pos.y -= 300 * dt | |
player_state = "climb" | |
elif keys[pygame.K_s]: | |
moved = True | |
player_pos.y += 300 * dt | |
player_state = "climb" | |
elif keys[pygame.K_a]: | |
moved = True | |
if keys[pygame.K_b]: # duck | |
player_pos.x -= 30 * dt | |
player_state = "duck" | |
else: | |
player_pos.x -= 300 * dt | |
player_state = "walk" | |
elif keys[pygame.K_d]: | |
moved = True | |
if keys[pygame.K_b]: # duck | |
player_pos.x += 30 * dt | |
player_state = "duck" | |
else: | |
player_pos.x += 300 * dt | |
player_state = "walk" | |
#if not moved and player_state in ("walk","climb"): | |
# player_state = "behindBack" | |
# animation | |
if len(pics[player_state]) > 1: | |
a += dt | |
if a > 1 / APS: | |
a = 0 | |
i += 1 | |
if i >= len(pics[player_state]): | |
i = 0 | |
# flip() the display to put your work on screen | |
pygame.display.flip() | |
# limits FPS to 60 | |
# dt is delta time in seconds since last frame, used for framerate- | |
# independent physics. | |
dt = clock.tick(FPS) / 1000 | |
pygame.display.set_caption(f"fps: {clock.get_fps():.2f} res:{W}x{H} {player_state} {i}") | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment