반응형

사운드를 로드하고 플레이 해 보자.

 

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
import pygame
 
pygame.init()
pygame.display.set_caption("Super fun game development")
screen = pygame.display.set_mode((640480))
clock = pygame.time.Clock()
running = True
 
player = pygame.image.load("player.png").convert()
player.set_colorkey(player.get_at((00)))
player_size = (player.get_width()*1.5, player.get_height()*1.5)
player = pygame.transform.scale(player, player_size)
player_pos = player.get_rect()
player_pos.center = (screen.get_width()/2, screen.get_height()/2)
 
sound = pygame.mixer.Sound("music.mp3")
# 사운드 리소스를 로드하고 오브젝트를 반환한다.
sound.play()
# 로드한 사운드 오브젝트를 플레이한다.
 
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
 
 
    screen.fill("black")
    screen.blit(player, player_pos)
    
    pygame.display.flip()
    clock.tick(60)
 
pygame.quit()
 

 

music.mp3
4.01MB

 

로드한 사운드가 플레이 된다.

 

반응형
Posted by J-sean
:
반응형

이미지를 로드하고 출력해 보자.

 

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
import pygame
 
pygame.init()
pygame.display.set_caption("Super fun game development")
screen = pygame.display.set_mode((640480))
clock = pygame.time.Clock()
running = True
 
player = pygame.image.load("player.png").convert()
# 플레이어 이미지를 로드하고 디스플레이와 일치하는 color format과 depth로 변환한다.
player.set_colorkey(player.get_at((00)))
# 이미지의 (0, 0) 픽셀을 colorkey로 사용한다. (0, 0) 픽셀과 같은 색상은 투명하게
# 표시된다.
player_size = (player.get_width()*1.5, player.get_height()*1.5)
player = pygame.transform.scale(player, player_size)
# 이미지를 1.5배 확대한다.
player_pos = player.get_rect()
player_pos.center = (screen.get_width()/2, screen.get_height()/2)
# 이미지의 위치를 화면 중앙으로 설정한다.
 
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
 
 
    screen.fill("black")
    screen.blit(player, player_pos)
    # 스크린에 이미지를 출력한다.
    
    pygame.display.flip()
    clock.tick(60)
 
pygame.quit()
 

 

 

player.png

 

colorkey가 적용되어 이미지가 깔끔하게 출력된다.

 

반응형
Posted by J-sean
:
반응형

Pygame으로 게임을 개발하기 위한 기본 코드를 작성해 보자. 

 

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
# Example file showing a basic pygame "game loop"
# pygame 기본 셋업
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'= '1'
# pygame 실행시 표시되는 메세지를 보이지 않게 한다.
# pygame을 import 하기 전에 설정해야 한다.
import pygame
 
# pygame setup
pygame.init()
pygame.display.set_caption("Super fun game development")
# 게임창 제목 표시
screen = pygame.display.set_mode((640480))
# 게임창 크기를 640X480으로 설정.
clock = pygame.time.Clock()
running = True
 
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
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
 
    # fill the screen with a color to wipe away anything from last frame
    screen.fill("black")
 
    # RENDER YOUR GAME HERE
    pygame.draw.circle(screen, "gray", screen.get_rect().center, 100)
    # 스크린 중앙에 회색원을 하나 그린다.
 
    # flip() the display to put your work on screen
    pygame.display.flip()
 
    clock.tick(60)  # limits FPS to 60
 
pygame.quit()
 

 

 

코드를 실행하면 검은 배경에 회색 원이 하나 표시된다.

 

PYGAME_HIDE_SUPPORT_PROMPT를 설정하지 않으면 위와 같은 메세지가 표시된다.

 

반응형
Posted by J-sean
: