1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import pygame
######################
# 기본 초기화 (반드시 해야 하는 것들)
pygame.init() #초기화
# 화면 크기 설정
screen_width = 480 #가로 크기
screen_height = 640 #세로 크기
screen = pygame.display.set_mode((screen_width, screen_height))
# 화면 타이틀 설정
pygame.display.set_caption("게임 이름") #게임 이름
#FPS
clock = pygame.time.Clock()
######################
#1. 사용자 게임 초기화 (배경 화면,게임 이미지,좌표,속도,폰트 등)
# 이벤트 루프
running = True #게임이 진행중인가?
while running:
dt = clock.tick(30) #게임화면 초당 프레임수
#2. 이벤트 처리(키보드,마우스)
for event in pygame.event.get(): #어떤 이벤트가 발생하였는가?
if event.type == pygame.QUIT: #창이 닫히는 이벤트가 발생하였는가?
running = False # 게임이 진행중이 아님
# 3. 게임 케릭터 위치 정의
# 4. 충돌 처리
# 5. 화면에 그리기
pygame.display.update() #게임화면을 다시 그리기
# pygame 종료
pygame.quit()
|
cs |
# 기본 초기화 (반드시 해야 하는 것들)
먼저 import pygame을 통해 pygame을 불러오고 시작합니다
(만약 설치 되있지 않다면 터미널에 C:/>pip install pygame 입력)
초기화
pygame.init()
화면 크기 설정
screen_width = #가로 크기
screen_height = #세로 크기
screen = pygame.display.set_mode((screen_width, screen_height))
화면 타이틀(게임이름)
pygame.display.set_caption("게임 이름") #게임 이름
FPS(Frames Per Second)
clock = pygame.time.Clock()
#1. 사용자 게임 초기화
배경 이미지
background = pygame.image.load("배경 이미지 경로")
캐릭터(적 캐릭터 등도 같은 방법)
character = pygame.image.load("캐릭터 이미지 경로")
character_size = character.get_rect().size #이미지의 크기를 구해옴
character_width = character_size[0] #캐릭터의 가로 크기
character_height = character_size[1] #캐릭터의 세로 크기
character_x_pos = #캐릭터 위치
character_y_pos = #캐릭터 위치
좌표 (움직일때 필요한 좌표값)
to_x = 0
to_y = 0
속도(캐릭터 이동속도)
character_speed = 0.6
폰트(폰트,크기)
game_font = pygame.font.Font(None,40)
시간
총시간
total_time =
시작 시간
start_ticks = pygame.time.get_ticks() # 시작 tick 을 받아옴
이벤트 루프
running = True 게임의 진행중 여부
while running:
dt = clock.tick(60) #게임화면 초당 프레임수
#2. 이벤트 처리(키보드, 마우스)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
for event in pygame.event.get():
if event.type == pygame.QUIT: #창닫기를 누를 경우
running = False # 게임이 진행중이 아님
if event.type == pygame.KEYDOWN: #키가 눌러졌는지 확인
if event.key == pygame.K_LEFT:
to_x -= character_speed
elif event.key == pygame.K_RIGHT:
to_x += character_speed
elif event.key == pygame.K_UP:
to_y -= character_speed
elif event.key == pygame.K_DOWN:
to_y += character_speed
if event.type == pygame.KEYUP: #키에서 손땔때
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
to_x = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
to_y = 0
character_x_pos += to_x *dt #프레임이 달라져도 속도를 같게 하기 위함
character_y_pos += to_y *dt
|
cs |
위와 같이 어떤 이벤트가 발생하는지(key up,down and click) 설정합니다
#3. 게임 케릭터 위치 정의
1
2
3
4
5
6
7
8
9
10
11
|
#가로 경계값 처리
if character_x_pos <0:
character_x_pos = 0
elif character_x_pos > screen_width - character_width:
character_x_pos = screen_width - character_width
#세로 경계값 처리
if character_y_pos <0:
character_y_pos = 0
elif character_y_pos > screen_height - character_height:
character_y_pos = screen_height - character_height
|
cs |
캐릭터가 화면 밖으로 나가지 않게 하기위해 위와 같이 설정합니다
#4.충돌처리
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 충돌 처리를 위한 rect 정보 업데이트
character_rect = character.get_rect()
character_rect.left = character_x_pos
character_rect.top = character_y_pos
enemy_rect = enemy.get_rect()
enemy_rect.left = enemy_x_pos
enemy_rect.top = enemy_y_pos
#충돌 체크
if character_rect.colliderect(enemy_rect):
print("충돌함")
running = False
|
cs |
위의 예시와 같이 충돌을 체크하고 처리합니다
#5. 화면에 그리기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
screen.blit(background,(0,0)) #배경 그리기
screen.blit(character,(character_x_pos,character_y_pos))
screen.blit(enemy,(enemy_x_pos,enemy_y_pos)) #적 그리기
#타이머 집어 넣기
#경과 시간 계산
elapsed_time = (pygame.time.get_ticks() - start_ticks) /1000
# 경과 시간을 1000으로 나눠서 초 단위로 표시
timer = game_font.render(str(int(total_time- elapsed_time)),True,(255,255,255))
#출력할 글자, True, 글자 색상
screen.blit(timer, (10,10))
# 시간이 0 이하면 게임 종료
if total_time- elapsed_time <= 0:
print("Time Out")
running = False
pygame.display.update() #게임화면을 다시 그리기
|
cs |
위와 같은 방법으로 배경 이미지,캐릭터,시간 등을 화면에 표시합니다
'python > pygame' 카테고리의 다른 글
pygame 활용한 게임 만들기 2 (오락실 게임 pang) (0) | 2021.02.21 |
---|