Created
April 20, 2022 13:45
-
-
Save elbakramer/eb205717086699e6072d71bdcad15f80 to your computer and use it in GitHub Desktop.
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 sys | |
import pygame | |
from enum import IntEnum | |
pygame.init() | |
size = width, height = 1000, 1000 | |
black = (0, 0, 0) | |
screen = pygame.display.set_mode(size) | |
class Facing(IntEnum): | |
DOWN = 0 | |
UP = 1 | |
LEFT = 2 | |
RIGHT = 3 | |
class Action(IntEnum): | |
IDLE = 0 | |
WALKING = 1 | |
class Player(pygame.sprite.Sprite): | |
def __init__(self): | |
super().__init__() | |
self._image_path = r"C:\Users\Yunseong\Documents\git\pygame\assets\Sprout Lands - Sprites - Basic pack\Characters\Basic Charakter Spritesheet.png" | |
self._full_image = pygame.image.load(self._image_path) | |
self._full_image = pygame.transform.rotozoom(self._full_image, 0, 4) | |
self._full_image_rect = self._full_image.get_rect() | |
self._position = [0, 0] | |
self._row_count = 4 | |
self._col_count = 4 | |
self._row_total_size = self._full_image_rect[2] | |
self._col_total_size = self._full_image_rect[3] | |
self._row_size = self._row_total_size / self._row_count | |
self._col_size = self._col_total_size / self._col_count | |
self._clock = pygame.time.Clock() | |
self._cycle_length = 2 | |
self._cycle_current_time = 0 | |
self._cycle_interval_msecs = 300 | |
self._current_facing = Facing.DOWN | |
self._current_action = Action.IDLE | |
self._current_cycle = 0 | |
self._state_changed = False | |
self._position_update_current_time = 0 | |
self._position_update_interval_msecs = 5 | |
self._image = self.get_image( | |
self._current_facing, self._current_action, self._current_cycle | |
) | |
def set_state(self, facing, action): | |
if facing is None: | |
facing = self._current_facing | |
if self._current_facing != facing or self._current_action != action: | |
self._state_changed = True | |
self._current_facing = facing | |
self._current_action = action | |
def get_image(self, facing, action, cycle): | |
row_index = facing | |
col_index = 2 * action + cycle | |
area = ( | |
col_index * self._col_size, | |
row_index * self._row_size, | |
self._col_size, | |
self._row_size, | |
) | |
return self._full_image.subsurface(area) | |
@property | |
def image(self): | |
return self._image | |
@property | |
def rect(self): | |
pos_x, pos_y, width_x, width_y = self._image.get_rect() | |
pos_x += self._position[0] | |
pos_y += self._position[1] | |
return (pos_x, pos_y, width_x, width_y) | |
def update(self): | |
self._clock.tick() | |
self._cycle_current_time += self._clock.get_time() | |
self._position_update_current_time += self._clock.get_time() | |
if self._state_changed or self._cycle_current_time > self._cycle_interval_msecs: | |
self._state_changed = False | |
self._cycle_current_time = 0 | |
self._current_cycle = (self._current_cycle + 1) % self._cycle_length | |
self._image = self.get_image( | |
self._current_facing, self._current_action, self._current_cycle | |
) | |
if ( | |
self._state_changed | |
or self._position_update_current_time > self._position_update_interval_msecs | |
): | |
self._state_changed = False | |
self._position_update_current_time = 0 | |
if self._current_action == Action.WALKING: | |
if self._current_facing == Facing.DOWN: | |
self._position[1] += 1 | |
elif self._current_facing == Facing.UP: | |
self._position[1] -= 1 | |
elif self._current_facing == Facing.LEFT: | |
self._position[0] -= 1 | |
elif self._current_facing == Facing.RIGHT: | |
self._position[0] += 1 | |
def main(): | |
player = Player() | |
all_sprites = pygame.sprite.Group(player) | |
game_is_running = True | |
keys_being_pressed = [] | |
key_to_facing = { | |
pygame.K_DOWN: Facing.DOWN, | |
pygame.K_UP: Facing.UP, | |
pygame.K_LEFT: Facing.LEFT, | |
pygame.K_RIGHT: Facing.RIGHT, | |
} | |
while game_is_running: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit() | |
elif event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_DOWN: | |
if event.key not in keys_being_pressed: | |
keys_being_pressed.append(event.key) | |
elif event.key == pygame.K_UP: | |
if event.key not in keys_being_pressed: | |
keys_being_pressed.append(event.key) | |
elif event.key == pygame.K_LEFT: | |
if event.key not in keys_being_pressed: | |
keys_being_pressed.append(event.key) | |
elif event.key == pygame.K_RIGHT: | |
if event.key not in keys_being_pressed: | |
keys_being_pressed.append(event.key) | |
elif event.type == pygame.KEYUP: | |
if event.key == pygame.K_DOWN: | |
if event.key in keys_being_pressed: | |
keys_being_pressed.remove(event.key) | |
elif event.key == pygame.K_UP: | |
if event.key in keys_being_pressed: | |
keys_being_pressed.remove(event.key) | |
elif event.key == pygame.K_LEFT: | |
if event.key in keys_being_pressed: | |
keys_being_pressed.remove(event.key) | |
elif event.key == pygame.K_RIGHT: | |
if event.key in keys_being_pressed: | |
keys_being_pressed.remove(event.key) | |
up_down = 0 | |
left_right = 0 | |
if pygame.K_DOWN in keys_being_pressed: | |
up_down += 1 | |
if pygame.K_UP in keys_being_pressed: | |
up_down -= 1 | |
if pygame.K_LEFT in keys_being_pressed: | |
left_right -= 1 | |
if pygame.K_RIGHT in keys_being_pressed: | |
left_right += 1 | |
if up_down == 0 and left_right == 0: | |
action = Action.IDLE | |
facing = None | |
elif up_down != 0 and left_right != 0: | |
action = Action.WALKING | |
facing = key_to_facing[keys_being_pressed[-1]] | |
else: | |
action = Action.WALKING | |
if up_down > 0: | |
facing = Facing.DOWN | |
elif up_down < 0: | |
facing = Facing.UP | |
elif left_right > 0: | |
facing = Facing.RIGHT | |
elif left_right < 0: | |
facing = Facing.LEFT | |
player.set_state(facing, action) | |
all_sprites.update() | |
screen.fill(black) | |
all_sprites.draw(screen) | |
pygame.display.update() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment