pygame.image.load() 함수로 이미지를 불러와 pygame.Surface.blit() 함수로 게임 화면에 그려본다.
본 글에서 사용하는 기능 및 참고되는 기능에 대한 정의는 다음과 같다.
pygame.image.load(fileobj, namehint="") -> Surface
pygame.Surface.blit(source, dest, area=None, special_flags=0) -> Rect
pygame.Rect.move(x, y) -> Rect
pygame.image.save(Surface, fileobj, namehint="") -> None
pygame.image.get_extended() -> bool
pygame.image.get_extended() 함수의 반환값이 True이면 아래의 이미지 포맷을 불러오는 것이 가능하다.
- JPG
- PNG
- GIF (non-animated)
- BMP
- PCX
- TGA (uncompressed)
- TIF
- LBM (and PBM)
- PBM (and PGM, PPM)
- XPM
이미지 저장 시 지원되는 포맷은 다음과 같다.
- BMP
- TGA
- PNG
- JPEG
예제 코드 1
<코드 1>
# python 3.9.6
# pygame 2.0.1
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
def main():
pygame.init()
screen_width, screen_height = (640, 480)
screen = pygame.display.set_mode((screen_width, screen_height))
screen_size = screen.get_size()
clock = pygame.time.Clock()
running = True
ball = pygame.image.load('image/intro_ball.gif')
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT\
or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
break
if not running:
continue
screen.fill(BLACK)
screen.blit(ball, [100, 100])
pygame.display.update()
clock.tick(60)
pygame.quit()
if __name__ == '__main__':
main()
<코드 1>에서 사용하는 공 이미지는 pygame.org 사이트의 Python Pygame Introduction 페이지에서 다운로드할 수 있다.
https://www.pygame.org/docs/tut/PygameIntro.html
<코드 1>을 실행하면 하나의 공이 게임 화면에 그려진다.
예제 코드 2
<코드 2>를 실행하면 게임 화면 안에서 공이 움직인다.
<코드 2>
# python 3.9.6
# pygame 2.0.1
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
def main():
pygame.init()
screen_width, screen_height = (640, 480)
screen = pygame.display.set_mode((screen_width, screen_height))
screen_size = screen.get_size()
clock = pygame.time.Clock()
running = True
ball = pygame.image.load('image/intro_ball.gif')
ball_rect = ball.get_rect()
speed = [2, 2]
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT\
or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
break
if not running:
continue
screen.fill(BLACK)
ball_rect = ball_rect.move(speed)
if ball_rect.left < 0 or ball_rect.right > screen_width:
speed[0] = -speed[0]
if ball_rect.top < 0 or ball_rect.bottom > screen_height:
speed[1] = -speed[1]
screen.blit(ball, ball_rect)
pygame.draw.rect(screen, RED, ball_rect, 1)
pygame.display.update()
clock.tick(60)
pygame.quit()
if __name__ == '__main__':
main()
예제 코드 3
<코드 3>을 실행한 후 키보드의 s 키를 누르면 게임 화면이 image 폴더 내 screenshot.png 이미지 파일로 저장된다.
# python 3.9.6
# pygame 2.0.1
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
def main():
pygame.init()
screen_width, screen_height = (640, 480)
screen = pygame.display.set_mode((screen_width, screen_height))
screen_size = screen.get_size()
clock = pygame.time.Clock()
running = True
ball = pygame.image.load('image/intro_ball.gif')
ball_rect = ball.get_rect()
speed = [2, 2]
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT\
or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
break
elif event.type == pygame.KEYDOWN and event.key == pygame.K_s:
pygame.image.save(screen, 'image/screenshot.png')
if not running:
continue
screen.fill(BLACK)
ball_rect = ball_rect.move(speed)
if ball_rect.left < 0 or ball_rect.right > screen_width:
speed[0] = -speed[0]
if ball_rect.top < 0 or ball_rect.bottom > screen_height:
speed[1] = -speed[1]
screen.blit(ball, ball_rect)
pygame.draw.rect(screen, RED, ball_rect, 1)
pygame.display.update()
clock.tick(60)
pygame.quit()
if __name__ == '__main__':
main()
<코드 3>에서 키보드의 s 키 입력 시 이미지를 저장하는 코드는 다음과 같다.
elif event.type == pygame.KEYDOWN and event.key == pygame.K_s:
pygame.image.save(screen, 'image/screenshot.png')
'코딩 > 파이썬 pygame' 카테고리의 다른 글
파이썬 게임 014. pygame에서 지원하는 색상 이름 (color name) (0) | 2021.10.09 |
---|---|
파이썬 게임 013. Pygame Documentation Summary (0) | 2021.10.07 |
파이썬 게임 011. pygame.draw 모듈로 그외 도형 그리기 (타원, 다각형, 타원호) (0) | 2021.10.03 |
파이썬 게임 010. pygame.draw.circle 함수로 원 그리기 (0) | 2021.10.01 |
파이썬 게임 009. pygame.draw.rect 함수로 사각형 그리기 (0) | 2021.09.29 |