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
관리 메뉴

개발자입니다

[유튜브 소놀코딩] 1강.키보드로 조종하기 본문

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

[유튜브 소놀코딩] 1강.키보드로 조종하기

끈기JK 2023. 8. 29. 22:08

pygame01.py

파이게임 화면을 계속 띄우기 위한 기본코드
import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False

pygame.quit()

 

pygame.K_0 은 0 키를 눌렀을때 값인데 누르면 48로 나온다. 이런 숫자들이 pygame 에 다 저장되어 있다.

print(pygame.K_0)  # 48

 

 

 

pygame02.py

키보드 화살표 키 입력 처리

0~9, a~z 는 ASCII 코드값이다.

import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                print('UP')
            elif event.key == pygame.K_DOWN:
                print('DOWN')
            elif event.key == pygame.K_RIGHT:
                print('RIGHT')
            elif event.key == pygame.K_LEFT:
                print('LEFT')

pygame.quit()

 

 

 

pygame03.py

background.get_size() 를 print 하면 화면 크기가 튜플 형태로 나타난다.

background.fill() 로 배경 색을 지정한다.

pygame.draw.circle() 에서 원을 그린다.

pygame.display.update() 를 해야 그림이 업데이트 되며 그려진다.

import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

x_pos = background.get_size()[0]//2 #240
y_pos = background.get_size()[1]//2 #180

play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                print('UP')
                y_pos = y_pos - 10
            elif event.key == pygame.K_DOWN:
                print('DOWN')
                y_pos = y_pos + 10
            elif event.key == pygame.K_RIGHT:
                print('RIGHT')
                x_pos = x_pos + 10
            elif event.key == pygame.K_LEFT:
                print('LEFT')
                x_pos = x_pos - 10
            
    background.fill((255,255,255))
    pygame.draw.circle(background, (0,0,255), (x_pos,y_pos), 5)
    #pygame.draw.circle(surface, color, center, radius)
    pygame.display.update()

pygame.quit()

 

 

 

pygame04.py

fps 변수로 게임 속도를 조절한다. while 내에서 fps.tick(60) 을 하면 1초에 while 문을 60번만 반복한다.

to_x, to_y 로 값을 받아서 더해준다. 키를 누르면 계속 더해서 움직이다가 떼면 멈춘다.

import pygame

pygame.init()

background = pygame.display.set_mode((480, 360))
pygame.display.set_caption("SONOL")

fps = pygame.time.Clock()

x_pos = background.get_size()[0]//2 #240
y_pos = background.get_size()[1]//2 #180

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 = -1
            elif event.key == pygame.K_DOWN:
                to_y = 1
            elif event.key == pygame.K_RIGHT:
                to_x = 1
            elif event.key == pygame.K_LEFT:
                to_x = -1
        if event.type == pygame.KEYUP:
            # to_x = 0
            # to_y = 0
            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 += to_x
    y_pos += to_y            
            
    background.fill((255,255,255))
    pygame.draw.circle(background, (0,0,255), (x_pos,y_pos), 5)  #pygame.draw.circle(surface, color, center, radius)
    pygame.display.update()

pygame.quit()