본문 바로가기
파이썬/파이게임(Pygame)

[파이게임.005] 이미지 불러오기/움직이기

by 긱펀 2022. 6. 13.
반응형

 

 

🚩 이미지 불러오기

[1] 코드

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
40
41
42
43
44
45
46
47
48
import pygame
 
# pygame window setup
pygame.init()
background = pygame.display.set_mode((480,360))
pygame.display.set_caption("image")
 
# image load
image_bg = pygame.image.load("Blue Sky.svg"# 이미지 불러오기
image_ball = pygame.image.load("ball.png")
image_ball = pygame.transform.scale(image_ball,(48,48)) # 이미지 크기조절
image_dog = pygame.image.load("dog.png")
image_dog = pygame.transform.scale(image_dog, (80,80))
 
# variables about sizes of image files.
size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]
 
size_ball_width = image_ball.get_rect().size[0]
size_ball_height = image_ball.get_rect().size[1]
 
x_pos_ball = size_bg_width/2 - size_ball_width/2
y_pos_ball = 0
 
size_dog_width = image_dog.get_rect().size[0]
size_dog_height = image_dog.get_rect().size[1]
 
x_pos_dog = size_bg_width/2 - size_dog_width/2
y_pos_dog = size_bg_height - size_dog_height
 
# make a window with ALL
play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
    
    # end of for
 
    # blit(이미지 객체, 이미지 좌표): 이미지를 화면에 나타내기
    background.blit(image_bg, (0,0))
    #background.blit(image_dog, (0,0))
    background.blit(image_dog, (x_pos_dog, y_pos_dog)) # dog 이미지 나타내기
    background.blit(image_ball, (x_pos_ball, y_pos_ball)) # ball 이미지 나타내기
    pygame.display.update()
 
pygame.quit()
 
cs

 

-이미지 파일 다운로드 ⬇

images.zip
0.05MB

 

[2] 실행결과

이미지 불러오기

 

 

🚩 이미지 움직이기(by 키보드)

[1] 코드

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import pygame
 
# pygame window setup
pygame.init()
background = pygame.display.set_mode((480,360))
pygame.display.set_caption("image")
 
# image load
image_bg = pygame.image.load("Blue Sky.svg"# 이미지 불러오기
image_ball = pygame.image.load("ball.png")
image_ball = pygame.transform.scale(image_ball,(48,48)) # 이미지 크기조절
image_dog = pygame.image.load("dog.png")
image_dog = pygame.transform.scale(image_dog, (80,80))
 
# some variables
size_bg_width = background.get_size()[0]
size_bg_height = background.get_size()[1]
 
size_ball_width = image_ball.get_rect().size[0]
size_ball_height = image_ball.get_rect().size[1]
 
x_pos_ball = size_bg_width/2 - size_ball_width/2
y_pos_ball = 0
 
size_dog_width = image_dog.get_rect().size[0]
size_dog_height = image_dog.get_rect().size[1]
 
x_pos_dog = size_bg_width/2 - size_dog_width/2
y_pos_dog = size_bg_height - size_dog_height
 
###### 추가(1) ####### dog를 방향키로 움직이기 위한 추가
to_x = 0
to_y = 0
dog_speed = 1
######################
 
###### 추가 (4) ###### ball이 자동으로 움직이기 위한 추가
x_speed_ball = 1
y_speed_ball = 1
#####################
 
 
# make a window with ALL
play = True
while play:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
        ####### 추가 (2) #########
        if event.type == pygame.KEYDOWN: #키보드를 눌렀다면
            # if event.key == pygame.K_UP: #위쪽 화살표키라면
            #     to_y = -dog_speed
            # elif event.key == pygame.K_DOWN: #아래쪽 화살표키 라면
            #     to_y = dog_speed
 
            # 좌우로만 dog를 움직이기
            if event.key == pygame.K_RIGHT: #오른쪽 화살표키 라면
                to_x = dog_speed
            elif event.key == pygame.K_LEFT: #왼쪽 화살표키 라면
                to_x = -dog_speed
        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
        #########################
    # end of for
 
    ##### 추가 (8) dog가 좌우 벽에서 팅기기 #####
    if x_pos_dog < 0:
        x_pos_dog = 0
    elif x_pos_dog > size_bg_width - size_dog_width:
        x_pos_dog = size_bg_width - size_dog_width
    else:
        x_pos_dog += to_x
    ## 추가(8)코딩한 뒤, 추가 (3)은 주석처리하기
 
    ###### 추가 (3) #######
    #x_pos_dog += to_x
    #y_pos_dog += to_y
    ####################### dog를 방향키로 움직여보기
 
    ###### 추가 (5) #######
    x_pos_ball += x_speed_ball
    y_pos_ball += y_speed_ball
    # 추가 (5)를 중간실행해 보면, 공이 쭉 떨어지는걸 확인할수 있음
    
    ###### 추가 (6) 공이 테두리에서 튕기게 하기 #######
    if x_pos_ball <= 0:
        x_pos_ball = 0
        x_speed_ball = -x_speed_ball # 추가(7) 4면에서 튕기게하기
    elif x_pos_ball >= size_bg_width - size_ball_width:
        x_pos_ball = size_bg_width - size_ball_width
        x_speed_ball = -x_speed_ball # 추가(7)
    
    if y_pos_ball <= 0:
        y_pos_ball = 0
        y_speed_ball = -y_speed_ball # 추가(7)
    elif y_pos_ball >= size_bg_height - size_ball_height:
        y_pos_ball = size_bg_height - size_ball_height
        y_speed_ball = -y_speed_ball # 추가(7)
    # 추가 (6)을 중간실행해 보면, 공이 벽에 부딪힌 뒤 쭉 미끌어짐
 
    # blit(이미지 객체, 이미지 좌표): 이미지를 화면에 나타내기
    background.blit(image_bg, (0,0))
    #background.blit(image_dog, (0,0))
    background.blit(image_dog, (x_pos_dog, y_pos_dog))
    background.blit(image_ball, (x_pos_ball, y_pos_ball))
    pygame.display.update()
 
pygame.quit()
 
cs

 

[2] 실행결과

이미지 움직이기(by 키보드)

 

728x90
반응형

댓글