-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstage1.py
161 lines (133 loc) · 4.47 KB
/
stage1.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
import pygame
import sys
import importlib
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
character_x, character_y = 50, 50
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
# 블록 클래스 정의
class Block:
def __init__(self, x, y):
self.x = x
self.y = y
# 움직이는 블록 클래스 정의 (Block 클래스 상속)
class MovingBlock(Block):
def __init__(self, x, y, move_range=100, speed=2):
super().__init__(x, y)
self.move_range = move_range
self.speed = speed
self.initial_x = x
self.direction = 1
def move(self):
self.x += self.speed * self.direction
if self.x > self.initial_x + self.move_range or self.x < self.initial_x - self.move_range:
self.direction *= -1
# 블록 리스트 초기화
blocks = [
Block(100, 500),
Block(300, 400),
Block(500, 300),
Block(700, 200)
]
# 포탈 클래스 정의
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(745, 150, portal_width, portal_height, 'stage2'),
clock = pygame.time.Clock()
# 충돌 감지
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
# 게임 루프
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
# 발판 그리기
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) # 포탈 색상은 보라색으로 설정
# 캐릭터 생성
pygame.draw.rect(screen, RED, character_rect)
pygame.display.update()
clock.tick(60)
pygame.quit()
sys.exit()