import pygame
import random
import os
pygame.init()
Black = (0,0,0)
size = [600,600]
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
def runGame() :
bomb_image = pygame.image.load('nuclear-bomb.png')
bomb_image = pygame.transform.scale(bomb_image, (50,50))
bombs = []
for i in range(5) :
rect = pygame.Rect(bomb_image.get_rect())
rect.left = random.randint(0, size[0])
rect.top = -100
dy = random.randint(3, 9)
bombs.append({'rect':rect, 'dy':dy})
person_image = pygame.image.load('standing-up-man-.png')
person_image = pygame.transform.scale(person_image, (100, 100))
person = pygame.Rect(person_image.get_rect())
person.left = size[0] // 2 - person.width // 2
person.top = size[1] - person.height
person_dx = 0
person_dy = 0
global done
while not done:
clock.tick(30)
for event in pygame.event.get() :
if event.type == pygame.QUIT :
done = True
break
elif event.type == pygame.KEYDOWN : # 키보드가 눌리는 이벤트 발생시
if event.key == pygame.K_LEFT : # 왼쪽방향키가 눌렸다면,
person_dx = -5
elif event.key == pygame.K_RIGHT :
person_dx = 5
elif event.type == pygame.KEYUP : # 키보드가 떼지는 이벤트 발생시
if event.key == pygame.K_LEFT :
person_dx = 0
elif event.key == pygame.K_RIGHT :
person_dx = 0
for bomb in bombs:
bomb['rect'].top + bomb['dy']
if bomb['rect'].top > size[1] :
bombs.remove(bomb)
rect = pygame.Rect(bomb_image.get_rect())
rect.top = -100
dy = random.randint(3, 9)
bombs.append({'rect':rect, 'dy':dy})
person.left = person.left + person_dx
if person.left < 0 :
person.left = 0
elif person.left > size[0] :
person.left = size[0]
screen.blit(person_image, person)
for bomb in bombs :
if bomb['rect'].colliderect(person) : done = True
screen.blit(bomb_image, bomb['rect'])
pygame.display.update()
screen.fill(Black)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.display.update()
runGame()
pygame.quit()import pygame
import sys
import random
# 초기 설정
pygame.init()
WIDTH, HEIGHT = 800, 600
FPS = 60
# 색상 정의
WHITE = (255, 255, 255)
BLUE = (0, 100, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
GOLD = (255, 215, 0)
BLACK = (0, 0, 0)
# 화면 생성
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("점프 스테이지 게임")
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 36)
# 플레이어 설정
player_width, player_height = 50, 50
player = pygame.Rect(WIDTH // 2, HEIGHT - 100, player_width, player_height)
player_vel_y = 0
jump_power = -15
gravity = 1
on_ground = False
# 게임 상태
stage = 1
lava_timer = 0
lava_height = HEIGHT
lava_speed = 0
lava_started = False
# 플랫폼 설정
platforms = []
goal_platform = None
def create_stage(stage):
global platforms, goal_platform, lava_timer, lava_height, lava_speed, lava_started
platforms = []
platform_count = max(4, 8 - stage) # 스테이지가 올라갈수록 발판 개수 감소
step_y = HEIGHT // (platform_count + 1)
for i in range(platform_count):
plat_width = random.randint(100, 200)
plat_x = random.randint(0, WIDTH - plat_width)
plat_y = HEIGHT - (i + 1) * step_y
platforms.append(pygame.Rect(plat_x, plat_y, plat_width, 20))
# 목표 발판 (맨 위)
goal_platform = pygame.Rect(WIDTH // 2 - 60, 50, 120, 20)
platforms.append(goal_platform)
# 플레이어 위치 초기화
player.x = WIDTH // 2
player.y = HEIGHT - 100
global player_vel_y
player_vel_y = 0
# 용암 초기화
lava_timer = 0
lava_height = HEIGHT
lava_speed = 0.5 + stage * 0.2 # 스테이지 올라갈수록 더 빨라짐
lava_started = False
create_stage(stage)
# 게임 루프
running = True
while running:
dt = clock.tick(FPS) / 1000 # 초 단위 시간
lava_timer += dt
# 이벤트 처리
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 키 입력
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.x -= 5
if keys[pygame.K_RIGHT]:
player.x += 5
if keys[pygame.K_SPACE] and on_ground:
player_vel_y = jump_power
on_ground = False
# 중력 및 이동
player_vel_y += gravity
player.y += player_vel_y
# 충돌 처리
on_ground = False
for platform in platforms:
if player.colliderect(platform) and player_vel_y > 0:
if player.bottom <= platform.bottom:
player.bottom = platform.top
player_vel_y = 0
on_ground = True
# 목표 발판 도달 → 다음 스테이지
if player.colliderect(goal_platform):
stage += 1
create_stage(stage)
# 화면 밖 제한
if player.left < 0:
player.left = 0
if player.right > WIDTH:
player.right = WIDTH
# 용암 시작 (4초 후)
if not lava_started and lava_timer >= 4:
lava_started = True
if lava_started:
lava_height -= lava_speed
# 용암에 닿으면 리셋
if player.bottom > lava_height:
stage = 1
create_stage(stage)
# 화면 그리기
screen.fill(WHITE)
# 플랫폼 그리기
for plat in platforms:
color = GOLD if plat == goal_platform else GREEN
pygame.draw.rect(screen, color, plat)
# 플레이어 그리기
pygame.draw.rect(screen, BLUE, player)
# 용암 그리기
if lava_started:
pygame.draw.rect(screen, RED, (0, lava_height, WIDTH, HEIGHT - lava_height))
# 스테이지 표시
text = font.render(f"Stage: {stage}", True, BLACK)
screen.blit(text, (10, 10))
# 화면 업데이트
pygame.display.flip()
pygame.quit()
sys.exit()3
import pygame # 1. pygame 선언
import random
pygame.init() # 2. pygame 초기화
# 3. pygame에 사용되는 전역변수 선언
WHITE = (255, 255, 255)
size = [400, 400]
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
# 4. pygame 무한루프
def runGame():
global done
while not done:
clock.tick(10)
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
**
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[pygame.draw.rect(screen,(색깔),(좌표,가로 세로))] ----사각형색깔 칠해진것
ex)pygame.draw.rect(screen, (0,255,255), (0,0,200,400)
[pygame.draw.rect(screen, (0, 0, 255), (0, 0, 200, 100), 1)] ---- 사각형 선만
[pygame.draw.rect(screen, (0, 0, 255), (0, 0, 200, 100), 1)]
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[pygame.draw.rect(screen, (0,0,255), (100, 200), 30)] ---- 원
반지름
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[pygame.draw.line(screen, 255, 0, 0), (0,0), (200, 0),5)]
where, color, start, end, thin
**
pygame.display.update()
runGame()
pygame.quit()


