공 부수기 게임 만들기

사용한 이미지

그림판에서 크기만 조절하고 페인트칠해서 사용했다. 이 게임의 이미지만 바꿔줘도 옛날 고전게임의 느낌을 낼 수 있을것같다.

background 백그라운드(배경)

ball 볼 크기 1 ball2 볼 크기 2 ball3 볼 크기 3

bullet 총알

character 캐릭터

weapon 무기

소스 코드

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
import pygame, sys
from pygame.locals import *

pygame.init() #pygame 초기화
pygame.display.set_caption("공 부수기 게임") #제목 설정

#색깔 (R, G, B)
Red = (255, 0, 0)
Blue = (0, 0, 255)
Green = (0, 255, 0)
Black = (0, 0, 0)
White = (255, 255, 255)

#이미지, 폰트 로드

##메인 스크린
ScrWidth = 640
ScrHeight = 480
Screen = pygame.display.set_mode((ScrWidth, ScrHeight))
background = pygame.image.load("C:/Users/Seounb/Desktop/Workspace/PythonWorkspace/pygame/arcade_game/images/background.png")

##메인 폰트
FontType = pygame.font.SysFont("NanumGothic", 32)
mainFont = FontType.render("GAME OVER", True, Red, None)
mainFont_Rect = mainFont.get_rect()
mainFont_Rect.center = (ScrWidth/2, ScrHeight/2)

##캐릭터 이미지
Character = pygame.image.load("C:/Users/Seounb/Desktop/Workspace/PythonWorkspace/pygame/arcade_game/images/character.png")
Character_size = Character.get_rect().size
Character_width = Character_size[0]
Character_height = Character_size[1]
Character_xpos = ScrWidth/2 - Character_width
Character_ypos = ScrHeight - Character_height
Character_tox = 0
Character_toy = 0


##공 이미지
ball_images = [
     pygame.image.load("PythonWorkspace/pygame/arcade_game/images/Ball.png"),
     pygame.image.load("PythonWorkspace/pygame/arcade_game/images/Ball2.png"),
     pygame.image.load("PythonWorkspace/pygame/arcade_game/images/Ball3.png")
]

ball_speed_y = [-18, -15, -12, -9]

balls = []

#최초 발생 공
balls.append({
    "pos_x" : 50,
    "pos_y" : 50,
    "img_idx" : 0,
    "to_x" : 3,
    "to_y" : -6,
    "init_spd_y" : ball_speed_y[0]
})

#총알 사라짐, 공 정보 저장
bullet_to_remove = -1
ball_to_remove = -1

##무기 이미지
Weapon = pygame.image.load("PythonWorkspace/pygame/arcade_game/images/weapon.png")
bullet = pygame.image.load("PythonWorkspace/pygame/arcade_game/images/bullet.png")
bullet.get_rect().size
bullet_width = bullet
bullets = []
bullet_speed = 10

#FPS
clock = pygame.time.Clock()

#시간
total_time = 30
start_time = pygame.time.get_ticks()

#무한 루프문
running = True
while running:
    game_frame = clock.tick(60)

    Screen.blit(background, (0 ,0))

    for event in pygame.event.get():
        if event.type == QUIT:
            running = False
        #캐릭터 위치
        elif event.type == KEYDOWN:
            if event.key == K_RIGHT:
                Character_tox += 5
            elif event.key == K_LEFT:
                Character_tox -= 5
            #총알 발사
            elif event.key == K_SPACE:
                bullet_xpos = Weapon_xpos + (Character_width/7)
                bullet_ypos = Weapon_ypos
                bullets.append([bullet_xpos, bullet_ypos])

        elif event.type == KEYUP:
            if event.key == K_RIGHT:
                Character_tox = 0
            elif event.key == K_LEFT:
                Character_tox = 0



    # 경계값 제한
    if Character_xpos > ScrWidth - Character_width:
        Character_xpos = ScrWidth-Character_width
    elif Character_xpos < 0:
        Character_xpos = 0
    
    #총알 위치 조정
    bullets = [ [b[0], b[1] - bullet_speed] for b in bullets]
    
    #공 튕기기
    for ball_idx, ball_val in enumerate(balls): #enumerate 를 사용하면
    #인덱스값과 그에 해당하는 밸류값을 같이 가져와서 저장함
        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 > ScrWidth-ball_width:
            ball_val["to_x"] = ball_val["to_x"] * -1
        #점프 효과
        if ball_pos_y >= ScrHeight - 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"]

    #충돌 처리
    CharacterRect = Character.get_rect()
    CharacterRect.left = Character_xpos
    CharacterRect.top = Character_ypos
    Character_xpos = Character_xpos + Character_tox

    #공, 캐릭터 충돌
    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_rect = ball_images[ball_img_idx].get_rect()
        ball_rect.left = ball_pos_x
        ball_rect.top = ball_pos_y

        if CharacterRect.colliderect(ball_rect):
            running = False
            break
    
    #공, 총알 충돌
    for bullet_idx, bullet_val in enumerate(bullets):
        bullet_xpos = bullet_val[0]
        bullet_ypos = bullet_val[1]

        bullet_rect = bullet.get_rect()
        bullet_rect.left = bullet_xpos
        bullet_rect.top = bullet_ypos

        if bullet_rect.colliderect(ball_rect):
            bullet_to_remove = bullet_idx
            ball_to_remove = ball_idx

            if ball_img_idx < 2:
                ball_with = 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),
                "pos_y" : ball_pos_y + (ball_height/2) - (small_ball_height/2),
                "img_idx" : ball_img_idx + 1,
                "to_x" : -3,
                "to_y" : -6,
                "init_spd_y" : ball_speed_y[ball_img_idx + 1]
                })

                balls.append({
                "pos_x" : ball_pos_x + (ball_width/2) - (small_ball_width/2),
                "pos_y" : ball_pos_y + (ball_height/2) - (small_ball_height/2),
                "img_idx" : ball_img_idx + 1,
                "to_x" : 3,
                "to_y" : -6,
                "init_spd_y" : ball_speed_y[ball_img_idx + 1]
                })
            break
        else:
            continue
        break
    
    if bullet_to_remove > -1:
        del bullets[bullet_to_remove]
        bullet_to_remove = -1
    
    if ball_to_remove > -1:
        del balls[ball_to_remove]
        ball_to_remove = -1

    #TIMER
    elapsed_time = int((pygame.time.get_ticks() - start_time) / 1000)
    rendtimer = FontType.render("{}".format(total_time-elapsed_time), True, Red)

    if total_time - elapsed_time <= 0:
        running = False
    
    #BLIT
    for bullet_xpos, bullet_ypos in bullets:
        Screen.blit(bullet, (bullet_xpos, bullet_ypos))

    for idx, val in enumerate(balls):
        ball_pos_x = val["pos_x"]
        ball_pos_y = val["pos_y"]
        ball_img_idx = val["img_idx"]
        Screen.blit(ball_images[ball_img_idx], (ball_pos_x, ball_pos_y))

    Weapon_xpos = Character_xpos + (Character_width/2)
    Weapon_ypos = Character_ypos - (Character_height/2)

    Screen.blit(Character, (Character_xpos, Character_ypos))
    Screen.blit(Weapon, (Weapon_xpos, Weapon_ypos))
    Screen.blit(rendtimer, (10, 10))

    pygame.display.update()

Screen.blit(mainFont, mainFont_Rect)
pygame.display.update()
pygame.time.delay(2000)

댓글남기기