개발자입니다
[유튜브 소놀코딩] 4강.이미지 사용하기 / 5강.키보드+마우스+이미지 적용하기 본문
pygame11.py
이미지 로드 후 불러오기
- 이미지를 로드하고 크기를 객체에 저장한다.
- while 문 안에 background.blit() 으로 이미지를 그린다.
import pygame
pygame.init()
background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")
image_bg = pygame.image.load("image/image3.jpg")
image_banana = pygame.image.load("image/image2.png")
image_monkey = pygame.image.load("image/image1.png")
size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]
size_banana_width = image_banana.get_rect().size[0]
size_banana_height = image_banana.get_rect().size[1]
x_pos_banana = size_bg_width/2 - size_banana_width/2
y_pos_banana = 0
size_monkey_width = image_banana.get_rect().size[0]
size_monkey_height = image_banana.get_rect().size[1]
x_pos_monkey = size_bg_width/2 - size_monkey_width/2
y_pos_monkey = size_bg_height - size_monkey_height
play = True
while play:
for event in pygame.event.get():
if event.type == pygame.QUIT:
play = False
background.blit(image_bg, (0, 0))
background.blit(image_monkey, (x_pos_monkey, y_pos_monkey))
background.blit(image_banana, (x_pos_banana, y_pos_banana))
pygame.display.update()
pygame.quit()
pygame12.py
원숭이 키보드로 조종하기
pygame04.py 에 있는 코드 가져왔다.
- 이미지를 그리고 키보드 화살표가 입력되면 원숭이를 이동한다
import pygame
pygame.init()
background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")
fps = pygame.time.Clock()
image_bg = pygame.image.load("image/image3.jpg")
image_monkey = pygame.image.load("image/image1.png")
size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]
size_monkey_width = image_monkey.get_rect().size[0]
size_monkey_height = image_monkey.get_rect().size[1]
x_pos_monkey = size_bg_width/2 - size_monkey_width/2
y_pos_monkey = size_bg_height - size_monkey_height
to_x = 0
to_y = 0
play = True
while play:
deltaTime = fps.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
play = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
to_y = -2
elif event.key == pygame.K_DOWN:
to_y = 2
elif event.key == pygame.K_RIGHT:
to_x = 2
elif event.key == pygame.K_LEFT:
to_x = -2
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
to_y = 0
elif event.key == pygame.K_DOWN:
to_y = 0
elif event.key == pygame.K_RIGHT:
to_x = 0
elif event.key == pygame.K_LEFT:
to_x = 0
x_pos_monkey += to_x
y_pos_monkey += to_y
background.blit(image_bg, (0, 0))
background.blit(image_monkey, (x_pos_monkey, y_pos_monkey))
pygame.display.update()
pygame.quit()
pygame13.py
마우스 따라다니는 바나나
pygame08.py 에서 코드 가져왔다.
- 바나나를 그리고 마우스 이동하면 따라다니게 한다.
import pygame
pygame.init()
background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")
fps = pygame.time.Clock()
image_bg = pygame.image.load("image/image3.jpg")
image_banana = pygame.image.load("image/image2.png")
size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]
size_banana_width = image_banana.get_rect().size[0]
size_banana_height = image_banana.get_rect().size[1]
x_pos_banana = size_bg_width/2 - size_banana_width/2
y_pos_banana = 0
play = True
while play:
for event in pygame.event.get():
if event.type == pygame.QUIT:
play = False
if event.type == pygame.MOUSEMOTION:
x_pos_mouse, y_pos_mouse = pygame.mouse.get_pos()
x_pos_banana = x_pos_mouse-size_banana_width/2
y_pos_banana = y_pos_mouse-size_banana_height/2
background.blit(image_bg, (0, 0))
background.blit(image_banana, (x_pos_banana, y_pos_banana))
pygame.display.update()
pygame.quit()
'파이게임 > 파이썬 게임 만들기 강의(유튜브 소놀코딩)' 카테고리의 다른 글
[유튜브 소놀코딩] 8강.캐릭터 충돌하기 / 9강.점수 만들기 / 10강.게임 종료하기 (0) | 2023.08.31 |
---|---|
[유튜브 소놀코딩] 6강.벽에 닿았을 때 튕기기 / 7강.캐릭터 추가하기 (0) | 2023.08.31 |
[유튜브 소놀코딩] 3강.선, 도형 그리기 (0) | 2023.08.30 |
[유튜브 소놀코딩] 2강.마우스로 조종하기 (0) | 2023.08.30 |
[유튜브 소놀코딩] 1강.키보드로 조종하기 (0) | 2023.08.29 |