-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstage3.py
196 lines (160 loc) · 5.43 KB
/
stage3.py
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
import pygame
import sys
import importlib
def run_stage():
pygame.init()
# 화면 크기 설정
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("점프 점프")
# 색깔 정의
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
FLOOR_COLOR = (144, 228, 144)
# 캐릭터 속성 설정
character_width, character_height = 20, 20
initial_character_x, initial_character_y = 50, 50
character_x, character_y = initial_character_x, initial_character_y
character_speed = 6
jump_speed = 16
gravity = 1
# 바닥 속성 설정
floor_height = 22
floor_y = SCREEN_HEIGHT - floor_height
# 발판 속성 설정
platform_width, platform_height = 100, 20
platform_color = BLUE
# 블록 좌표 설정
blocks_positions = [
(0, 300),
(100, 300),
(200, 300),
(300, 300),
(400, 300),
(500, 300),
(600, 300),
(700, 300)
]
# 블록 클래스 정의
class Block:
def __init__(self, x, y):
self.x = x
self.y = y
# 블록 리스트 초기화
blocks = [Block(x, y) for x, y in blocks_positions]
# 포탈 클래스 정의
class Portal:
def __init__(self, x, y, width, height, target_stage):
self.rect = pygame.Rect(x, y, width, height)
self.target_stage = target_stage
# 포탈 리스트 초기화
portal_width, portal_height = 40, 40
portals = [Portal(700, 275 - portal_height - 10, portal_width, portal_height, 'stage4')]
clock = pygame.time.Clock()
# 가시 클래스 정의
class Spike:
def __init__(self, x, y, width, height):
self.rect = pygame.Rect(x, y, width, height)
spike_width, spike_height = 20, 20
spikes = [
Spike(150, 250, spike_width, spike_height),
Spike(225, 150, spike_width, spike_height),
Spike(300, 250, spike_width, spike_height),
Spike(375, 150, spike_width, spike_height),
Spike(450, 250, spike_width, spike_height),
Spike(525, 150, spike_width, spike_height),
Spike(600, 250, spike_width, spike_height),
Spike(675, 150, spike_width, spike_height)
]
# 충돌 감지
def check_collision(character, blocks):
for block in blocks:
if character.colliderect(pygame.Rect(block.x, block.y, platform_width, platform_height)):
return block
return None
# 포탈 충돌 감지
def check_portal_collision(character, portals):
for portal in portals:
if character.colliderect(portal.rect):
return portal
return None
# 가시 충돌 감지
def check_spike_collision(character, spikes):
for spike in spikes:
if character.colliderect(spike.rect):
return spike
return None
# 게임 루프
running = True
vertical_momentum = 0
is_on_ground = True
space_pressed = False
while running:
screen.fill(WHITE)
character_rect = pygame.Rect(character_x, character_y, character_width, character_height)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
space_pressed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
space_pressed = False
if space_pressed and is_on_ground:
vertical_momentum = -jump_speed
is_on_ground = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
character_x -= character_speed
if keys[pygame.K_RIGHT]:
character_x += character_speed
# 화면 범위 제한 및 바닥 충돌 처리
character_x = max(0, min(SCREEN_WIDTH - character_width, character_x))
vertical_momentum += gravity
character_y += vertical_momentum
character_y = min(character_y, floor_y - character_height)
# 바닥 그리기
pygame.draw.rect(screen, FLOOR_COLOR, (0, floor_y, SCREEN_WIDTH, floor_height))
# 충돌 검사 및 처리
block_collided = check_collision(character_rect, blocks)
if block_collided:
if vertical_momentum > 0:
character_y = block_collided.y - character_height
vertical_momentum = 0
is_on_ground = True
elif character_y >= floor_y - character_height:
character_y = floor_y - character_height
vertical_momentum = 0
is_on_ground = True
else:
is_on_ground = False
# 포탈 충돌 검사 및 처리
portal_collided = check_portal_collision(character_rect, portals)
if portal_collided:
stage_module = importlib.import_module(portal_collided.target_stage)
stage_module.run_stage()
running = False
# 가시 충돌 검사 및 처리
spike_collided = check_spike_collision(character_rect, spikes)
if spike_collided:
print("Character hit a spike! Respawning...")
character_x, character_y = initial_character_x, initial_character_y
vertical_momentum = 0
is_on_ground = True
# 발판 그리기
for block in blocks:
pygame.draw.rect(screen, platform_color, (block.x, block.y, platform_width, platform_height))
# 포탈 그리기
for portal in portals:
pygame.draw.rect(screen, (255, 0, 255), portal.rect) # 포탈 색상은 보라색으로 설정
# 가시 그리기
for spike in spikes:
pygame.draw.rect(screen, (0, 0, 0), spike.rect) # 가시 색상은 검정색으로 설정
# 캐릭터 생성
pygame.draw.rect(screen, RED, character_rect)
pygame.display.update()
clock.tick(60)
pygame.quit()
sys.exit()