pygame[์‘์šฉ]

Pygame ์‘์šฉ

๋ณธ ํฌ์ŠคํŒ…์€ ํ•ด๋‹น ์˜์ƒ์„ ์ ๊ทน์ฐธ๊ณ  ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ

  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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import os
import pygame

##############################################################################
# ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค)

pygame.init() # ์ดˆ๊ธฐํ™”

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 640 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 480 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption('Jiwoo Pang')

# FPS
clock = pygame.time.Clock()
##############################################################################

# 1. ์‚ฌ์šฉ์ž ๊ฒŒ์ž„ ์ดˆ๊ธฐํ™” (๋ฐฐ๊ฒฝํ™”๋ฉด, ๊ฒŒ์ž„ ์ด๋ฏธ์ง€, ์ขŒํ‘œ, ์†๋„, ํฐํŠธ ๋“ฑ)
current_path = os.path.dirname(__file__) # ํ˜„์žฌ ํŒŒ์ผ์˜ ์œ„์น˜ ๋ฐ˜ํ™˜
image_path = os.path.join(current_path, 'images') # images ํด๋” ์œ„์น˜ ๋ฐ˜ํ™˜

# ๋ฐฐ๊ฒฝ ๋งŒ๋“ค๊ธฐ
background = pygame.image.load(os.path.join(image_path, 'game_background.jpg'))

# ์Šคํ…Œ์ด์ง€ ๋งŒ๋“ค๊ธฐ
stage = pygame.image.load(os.path.join(image_path, 'stage.png'))
stage_size = stage.get_rect().size
stage_height = stage_size[1] # ์Šคํ…Œ์ด์ง€ ๋†’์ด ์œ„์— ์บ๋ฆญํ„ฐ๋ฅผ ๋‘๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

# ์บ๋ฆญํ„ฐ ๋งŒ๋“ค๊ธฐ
character = pygame.image.load(os.path.join(image_path, 'cat.png'))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = (screen_width/2) - (character_width/2)
character_y_pos = screen_height - character_height - stage_height

# ์บ๋ฆญํ„ฐ ์ด๋™ ๋ฐฉํ–ฅ
character_to_x = 0

# ์บ๋ฆญํ„ฐ์ด๋™ ์†๋„
character_speed = 5

# ๋ฌด๊ธฐ ๋งŒ๋“ค๊ธฐ 
weapon = pygame.image.load(os.path.join(image_path, 'weapon.png'))
weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]

# ๋ฌด๊ธฐ๋Š” ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ๋ฐœ ๋ฐœ ์‚ฌ ๊ฐ€๋Šฅ
weapons = []

# ๋ฌด๊ธฐ ์ด๋™ ์†๋„
weapon_speed = 10

# ๊ณต ๋งŒ๋“ค๊ธฐ (4๊ฐœ ํฌ๊ธฐ์— ๋Œ€ํ•ด ๋”ฐ๋กœ ์ฒ˜๋ฆฌ)
ball_images = [
    pygame.image.load(os.path.join(image_path, 'balloon1.png')),
    pygame.image.load(os.path.join(image_path, 'balloon2.png')),
    pygame.image.load(os.path.join(image_path, 'balloon3.png')),
    pygame.image.load(os.path.join(image_path, 'balloon4.png'))]

# ๊ณต ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ์ตœ์ดˆ ์Šคํ”ผ๋“œ
ball_speed_y = [-18, -15, -12, -9]

# ๊ณต๋“ค 
balls = []

# ์ตœ์ดˆ ๋ฐœ์ƒํ•˜๋Š” ํฐ ๊ณต ์ถ”๊ฐ€
balls.append({
    'pos_x' : 50, # ๊ณต์˜ x์ขŒํ‘œ
    'pos_y' : 50, # ๊ณต์˜ y์ขŒํ‘œ
    'img_idx' : 0, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
    'to_x' : 3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ 
    'to_y' : -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ
    'init_spd_y' : ball_speed_y[0]}) # y ์ตœ์ดˆ ์†๋„

# ์‚ฌ๋ผ์งˆ ๋ฌด๊ธฐ, ๊ณต ์ •๋ณด ์ €์žฅ ๋ณ€์ˆ˜
weapon_to_remove = -1
ball_to_remove = -1

# ํฐํŠธ ์ •์˜
game_font = pygame.font.Font(None, 40)
total_time = 100
start_ticks = pygame.time.get_ticks() # ์‹œ์ž‘ ์‹œ๊ฐ„ ์ •์˜

# ๊ฒŒ์ž„ ์ข…๋ฃŒ ๋ฉ”์„ธ์ง€
game_result = 'Game Over'

running = True
while running:
    dt = clock.tick(30)

    # 2. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ (ํ‚ค๋ณด๋“œ. ๋งˆ์šฐ์Šค ๋“ฑ)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT:
            running = False       

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                character_to_x -= character_speed
            elif event.key == pygame.K_RIGHT:
                character_to_x += character_speed
            elif event.key == pygame.K_SPACE: # ๋ฌด๊ธฐ ๋ฐœ์‚ฌ
                weapon_x_pos = character_x_pos + (character_width / 2) - (weapon_width / 2)
                weapon_y_pos = character_y_pos
                weapons.append([weapon_x_pos, weapon_y_pos])

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                character_to_x = 0

    # 3. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ์œ„์น˜ ์ •์˜
    character_x_pos += character_to_x

    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # ๋ฌด๊ธฐ ์œ„์น˜ ์กฐ์ •
    weapons = [ [w[0], w[1] - weapon_speed] for w in weapons] # ๋ฌด๊ธฐ ์œ„์น˜๋ฅผ ์œ„๋กœ

    # ์ฒœ์žฅ์— ๋‹ฟ์€ ๋ฌด๊ธฐ ์—†์• ๊ธฐ
    weapons = [ [w[0], w[1]] for w in weapons if w[1] > 0]

    # ๊ณต ์œ„์น˜ ์ •์˜
    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val['pos_x']
        ball_pos_y = ball_val['pos_y']
        ball_img_idx = ball_val['img_idx']
        
        ball_size = ball_images[ball_img_idx].get_rect().size
        ball_width = ball_size[0]
        ball_height = ball_size[1]

        # ๊ฐ€๋กœ๋ฒฝ์— ๋‹ฟ์•˜์„ ๋•Œ ๊ณต ์ด๋™ ์œ„์น˜ ๋ณ€๊ฒฝ (ํŠ•๊ฒจ๋‚˜์˜ค๋Š” ํšจ๊ณผ)
        if ball_pos_x < 0 or ball_pos_x > screen_width - ball_width:
            ball_val['to_x'] = ball_val['to_x'] * (-1)

        # ์„ธ๋กœ ์œ„์น˜
        # ์Šคํ…Œ์ด์ง€์— ํŠ•๊ฒจ์„œ ์˜ฌ๋ผ๊ฐ€๋Š” ์ฒ˜๋ฆฌ
        if ball_pos_y >= screen_height - stage_height - ball_height:
            ball_val['to_y'] = ball_val['init_spd_y']
        else: # ๊ทธ์™ธ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์—๋Š” ์†๋„๋ฅผ ์ฆ๊ฐ€
            ball_val['to_y'] += 0.5

        ball_val['pos_x'] += ball_val['to_x']
        ball_val['pos_y'] += ball_val['to_y']

    # 4. ์ถฉ๋Œ ์ฒ˜๋ฆฌ

    # ์บ๋ฆญํ„ฐ rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.top = character_y_pos

    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val['pos_x']
        ball_pos_y = ball_val['pos_y']
        ball_img_idx = ball_val['img_idx']

        # ๊ณต rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
        ball_rect =  ball_images[ball_img_idx].get_rect()
        ball_rect.left = ball_pos_x
        ball_rect.top = ball_pos_y

        # ๊ณต๊ณผ ์บ๋ฆญํ„ฐ ์ถฉ๋Œ ์ฒ˜๋ฆฌ
        if character_rect.colliderect(ball_rect):
            running = False
            break

        # ๊ณต๊ณผ ๋ฌด๊ธฐ๋“ค ์ถฉ๋Œ ์ฒ˜๋ฆฌ
        for weapon_idx, weapon_val in enumerate(weapons):
            weapon_pos_x = weapon_val[0]
            weapon_pos_y = weapon_val[1]

            # ๋ฌด๊ธฐ rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
            weapon_rect = weapon.get_rect()
            weapon_rect.left = weapon_pos_x
            weapon_rect.top = weapon_pos_y

            # ์ถฉ๋Œ ์ฒดํฌ
            if weapon_rect.colliderect(ball_rect):
                weapons_to_remove = weapon_idx # ํ•ด๋‹น ๋ฌด๊ธฐ ์—†์• ๊ธฐ ์œ„ํ•œ ๊ฐ’ ์„ค์ •
                ball_to_remove = ball_idx

                if ball_img_idx < 3:
                    # ํ˜„์žฌ ๊ณต ํฌ๊ธฐ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ด
                    ball_width = ball_rect.size[0]
                    ball_height = ball_rect.size[1]

                    # ๋‚˜๋ˆ ์ง„ ๊ณต ์ •๋ณด
                    small_ball_rect = ball_images[ball_img_idx + 1].get_rect()
                    small_ball_width = small_ball_rect.size[0]
                    small_ball_height = small_ball_rect.size[1]

                    # ์™ผ์ชฝ์œผ๋กœ ํŠ•๊ฒจ๋‚˜๊ฐ€๋Š” ์ž‘์€ ๊ณต
                    balls.append({
                        'pos_x' : ball_pos_x + (ball_width / 2) - (small_ball_width / 2), # ๊ณต์˜ x์ขŒํ‘œ
                        'pos_y' : ball_pos_y + (ball_height / 2) - (small_ball_height / 2), # ๊ณต์˜ y์ขŒํ‘œ
                        'img_idx' : ball_img_idx + 1, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
                        'to_x' : -3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ 
                        'to_y' : -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ
                        'init_spd_y' : ball_speed_y[ball_img_idx + 1]}) # y ์ตœ์ดˆ ์†๋„

                    # ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํŠ•๊ฒจ๋‚˜๊ฐ€๋Š” ์ž‘์€ ๊ณต
                    balls.append({
                        'pos_x' : ball_pos_x + (ball_width / 2) - (small_ball_width / 2), # ๊ณต์˜ x์ขŒํ‘œ
                        'pos_y' : ball_pos_y + (ball_height / 2) - (small_ball_height / 2), # ๊ณต์˜ y์ขŒํ‘œ
                        'img_idx' : ball_img_idx + 1, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
                        'to_x' : 3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ 
                        'to_y' : -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ
                        'init_spd_y' : ball_speed_y[ball_img_idx + 1]}) # y ์ตœ์ดˆ ์†๋„    
                break
            
        else: # ๊ณ„์† ๊ฒŒ์ž„์„ ์ง„ํ–‰
            continue # ์•ˆ์ชฝ for๋ฌธ ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์œผ๋ฉด continue. ๋ฐ”๊นฅ for๋ฌธ ๊ณ„์† ์ˆ˜ํ–‰
        break # ์•ˆ์ชฝ for๋ฌธ์—์„œ break๋ฅผ ๋งŒ๋‚˜๋ฉด ์—ฌ๊ธฐ๋กœ ์ง„์ž… ๊ฐ€๋Šฅ. 2์ค‘ for๋ฌธ์„ ํ•œ๋ฒˆ์— ํ†ต๊ณผํ•˜๋Š” ํŠธ๋ฆญ!
    
        # for ๋ฐ”๊นฅ ์กฐ๊ฑด:
        #     ๋ฐ”๊นฅ ๋™์ž‘
        #     for ์•ˆ์ชฝ ์กฐ๊ฑด:
        #         ์•ˆ์ชฝ ๋™์ž‘
        #         if ์ถฉ๋Œํ•˜๋ฉด:
        #             break
        #     else:
        #         continue
        #     break


    # ์ถฉ๋Œ๋œ ๊ณต or ๋ฌด๊ธฐ ์—†์• ๊ธฐ
    if ball_to_remove > -1:
        del balls[ball_to_remove]
        ball_to_remove = -1
    if weapon_to_remove > -1:
        del weapons[weapon_to_remove]
        weapon_to_remove = -1
    
    # ๋ชจ๋“  ๊ณต์„ ์—†์•ค ๊ฒฝ์šฐ ๊ฒŒ์ž„ ์ข…๋ฃŒ(์„ฑ๊ณต)
    if len(balls) == 0:
        game_result = 'Mission Complete'
        running = False

    # 5. ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(background, (0, 0))
    for weapon_x_pos, weapon_y_pos in weapons:
        screen.blit(weapon, (weapon_x_pos, weapon_y_pos))
    for idx, val in enumerate(balls):
        ball_pos_x = val['pos_x']
        ball_pos_y = val['pos_y']
        ball_img_dx = val['img_idx']
        screen.blit(ball_images[ball_img_idx], (ball_pos_x, ball_pos_y))

    screen.blit(stage, (0, screen_height - stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))
    
    # ๊ฒฝ๊ณผ ์‹œ๊ฐ„ ๊ณ„์‚ฐ
    elapsed_time = (pygame.time.get_ticks() - start_ticks)/ 1000
    timer = game_font.render('Time: {}'.format(int(total_time - elapsed_time)), True, (255, 255, 255))
    screen.blit(timer, (10, 10))

    # ์‹œ๊ฐ„ ์ดˆ๊ณผํ–ˆ๋‹ค๋ฉด
    if total_time - elapsed_time <= 0:
        game_result = 'Time Over'
        running = False

    pygame.display.update() # ๊ฒŒ์ž„ํ™”๋ฉด์„ ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ธฐ

# ๊ฒŒ์ž„ ์˜ค๋ฒ„ ๋ฉ”์„ธ์ง€
msg = game_font.render(game_result, True, (255, 0, 0))
msg_rect = msg.get_rect(center=(int(screen_width/2), int(screen_height/2)))
screen.blit(msg, msg_rect)
pygame.display.update()

# ์ž ์‹œ ๋Œ€๊ธฐ
pygame.time.delay(2000) # 2์ดˆ ์ •๋„ ๋Œ€๊ธฐ(ms ๋‹จ์œ„)
 
# pygame ์ข…๋ฃŒ
pygame.quit()

๊ฒฐ๊ณผ๋ฌผ

pygame