JOOHUUN
파이썬 pygame 활용 미니 게임 만들기(mario_) 본문
github:https://github.com/joohuun/pygame.git
GitHub - joohuun/pygame
Contribute to joohuun/pygame development by creating an account on GitHub.
github.com
1. main_.py 파일의 pygame 환경 셋팅
2. settings_.py 파일에서 맵, 플레이어(P)의 시작 위치 설정
-----------------------------------------------------------------------------------------------------------------------------------
3. player_파일에서 Player 클래서 생성
플레이어 속성은 아래 self., 위치, 표면, 점프시 먼지입자 생성등
class Player(pygame.sprite.Sprite): # 타일 클래스(집단) 생성
def __init__(self, pos, surface,create_jump_particles): # 플레이어 위치 속성 설정
super().__init__() # 다른 클래스의 속성 불러옴
self.import_character_assets()
self.frame_index = 0
self.animation_speed = 0.15
self.image = self.animations['idle'][self.frame_index]
self.rect = self.image.get_rect(topleft=pos)
# self.image = pygame.Surface((32,64))
# self.image.fill('red')
# dust particles
self.import_dust_run_particles()
self.dust_frame_index = 0
self.dust_animation_speed = 0.15
self.display_surface = surface
self.create_jump_particles = create_jump_particles
# player movemenet
self.direction = pygame.math.Vector2(0.0)
self.speed = 8
self.gravity = 0.8
self.jump_speed = -16
# player status
self.status = 'idle'
self.facing_right = True # 오른쪽보기를 참으로 설정
self.on_ground = False
self.on_ceiling = False
self.on_left = False
self.on_right = False
player_.py) 중력 설정을 안하면 점프시 계속올라가는 버그 발생
def apply_gravity(self):
self.direction.y += self.gravity
self.rect.y += self.direction.y
player_.py) 키 입력 설정
def get_input(self): # 키 입력 설정
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
self.direction.x = 1
self.facing_right = True
elif keys[pygame.K_LEFT]:
self.direction.x = -1
self.facing_right = False
else:
self.direction.x = 0
if keys[pygame.K_SPACE] and self.on_ground: # 지면에 붙어있을때만 점프 출력
self.jump()
self.create_jump_particles(self.rect.midbottom)
palyer_.py) 캐릭터 상태를 idle, run, jump ,fall로 나누기 (파일 위치 등록)
def import_character_assets(self):
character_path = '../graphics/character/'
self.animations = {'idle': [], 'run': [], 'jump': [], 'fall': []}
for animation in self.animations.keys():
full_path = character_path + animation
self.animations[animation] = import_folder(full_path)
-----------------------------------------------------------------------------------------------------------------------------------
4. particle_.py 입자효과 생성
class ParticleEffect(pygame.sprite.Sprite):
def __init__(self, pos, type):
super().__init__()
self.frame_index = 0
self.animation_speed = 0.5
if type == 'jump':
self.frames = import_folder('../graphics/character/dust_particles/jump')
if type == 'land':
self.frames = import_folder('../graphics/character/dust_particles/land')
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect(center=pos)
particle_.py) 애니메이션 효과 설정
def animate(self):
self.frame_index += self.animation_speed
if self.frame_index >= len(self.frames):
self.kill()
else:
self.image = self.frames[int(self.frame_index)]
-----------------------------------------------------------------------------------------------------------------------------------
5. tile_.py 바닥 생성
import pygame
class Tile(pygame.sprite.Sprite): #타일 클래스(집단) 생성
def __init__(self,pos,size): # 각 타일(객체)의 위치, 크기 속성 설정
super().__init__() # 다른 클래스의 속성을 불러옴
self.image = pygame.Surface((size, size)) #셀프 이미지 설정 (x,y)사이즈
self.image.fill('grey') # 타일 색
self.rect = self.image.get_rect(topleft = pos) #셀프 사각형 위치 설정
def update(self, x_shift): # 축이동
self.rect.x += x_shift
6. level_.py 에서는 지금까지 만든 파일들을 불러와 합치고 발생하는 오류들을 수정함
ex) 벽 충돌시 생기는 버그, 캐릭터 이동에 따른 맵이동등 github 참조 바랍니다..
import pygame
from tiles_ import Tile
from settings_ import tile_size, screen_width
from player_ import Player
from particles_ import ParticleEffect
이렇게 player를 구성하였고 이어서 맵셋팅, enemy, coins을 추가