Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
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
Tags
more
Archives
Today
Total
관리 메뉴

개발자입니다

[유튜브 소놀코딩] 4강.이미지 사용하기 / 5강.키보드+마우스+이미지 적용하기 본문

파이게임/파이썬 게임 만들기 강의(유튜브 소놀코딩)

[유튜브 소놀코딩] 4강.이미지 사용하기 / 5강.키보드+마우스+이미지 적용하기

끈기JK 2023. 8. 31. 19:24

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()