import pygame # 1. pygame 선언
import random
import os
pygame.init() # 2. pygame 초기화
# 3. pygame에 사용되는 전역변수 선언
BLACK = (0, 0, 0)
size = [600, 800]
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
def runGame():
bomb_image = pygame.image.load('bomb.png')
bomb_image = pygame.transform.scale(bomb_image, (50, 50))
bombs = []
life = 7
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('person.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)
screen.fill(BLACK)
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.left = random.randint(0, size[0])
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.width:
person.left = size[0] - person.width
screen.blit(person_image, person)
for bomb in bombs:
if bomb['rect'].colliderect(person):
bomb['rect'].left = random.randint(0, size[0])
life = life - 1
if life == 0:
done = True
screen.blit(bomb_image, bomb['rect'])
pygame.display.update()
runGame()
pygame.quit()top of page

실제 작동 상태를 확인하려면 라이브 사이트로 이동하세요.
2025.09.14
2025.09.14
댓글 1개
좋아요
댓글(1)
더 이상 게시물에 대한 댓글 기능이 지원되지 않습니다. 자세한 사항은 사이트 소유자에게 문의하세요.
bottom of page



mport pygame # 1. pygame 선언 import random import os pygame.init() # 2. pygame 초기화 # 3. pygame에 사용되는 전역변수 선언 MINT = (0, 222, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) GREEN = (0, 255, 0) YELLOW = (255, 255, 0) ORANGE = (255, 128, 0) PURPLE = (255, 0, 255) GRAY = (128, 128, 128) size = [600, 800] screen = pygame.display.set_mode(size) done = False clock = pygame.time.Clock() def runGame(): bomb_image = pygame.image.load('bomb.png') bomb_image = pygame.transform.scale(bomb_image, (50, 50)) bombs = [] life = 7 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('person.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) screen.fill(GRAY) 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.left = random.randint(0, size[0]) 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.width: person.left = size[0] - person.width screen.blit(person_image, person) for bomb in bombs: if bomb['rect'].colliderect(person): bomb['rect'].top -= 700 bomb['rect'].left = random.randint(0, size[0]) life = life - 1 if life == 0: done = True screen.blit(bomb_image, bomb['rect']) pygame.display.update() runGame() pygame.quit()