print("select background color")
print("1.BLACK 2.MINT 3.RED 4.GREEN 5.BLUE 6.YELLOW 7.ORANGE 8.PURPLE 9.PINK 10.LIME 11.BROWN 12.GRAY 13.CORAL 14.PIG 15.OLIVE")
cc = int(input())-1
import pygame # 1. pygame 선언
import random
import time
import data.colorList as dc
pygame.init() # 2. pygame 초기화
# 3. pygame에 사용되는 전역변수 선언
a = [dc.BLACK, dc.MINT, dc.RED, dc.GREEN, dc.BLUE, dc.YELLOW, dc.ORANGE, dc.PURPLE, dc.PINK, dc.LIME, dc.BROWN, dc.GRAY, dc.CORAL, dc.PIG, dc.OLIVE]
size = [600, 800]
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
def runGame():
starttime = int(time.time())
bomb_image = pygame.image.load('bomb.png')
bomb_image = pygame.transform.scale(bomb_image, (50, 50))
bombs = []
life = 3
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(a[cc])
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()
endtime = int(time.time())
score = endtime - starttime
print('score :', score, 'sec', sep=' ')
runGame()
pygame.quit()
# 파이게임 텍스트 출력 명령어 검색 -> 스코어 나타내기
# 남은 목숨 나타내기
# 색깔 추가
# 아래 파일 .py로 바꾸고 이용top of page

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



print("select background color") print("1.BLACK 2.RED 3.GREEN 4.BLUE 5.MINT 6.YELLOW 7.ORANGE\n8.PURPLE 9.PINK 10.LIME 11.BROWN 12.GRAY 13.CORAL 14.PIG\n15.OLIVE 16.WINE 17.PEACH 18.IVORY 19.BEIGE") cc = int(input())-1 import pygame # 1. pygame 선언 import random import time import data.colorList as dc pygame.init() # 2. pygame 초기화 # 3. pygame에 사용되는 전역변수 선언 a = [dc.BLACK, dc.RED, dc.GREEN, dc.BLUE, dc.MINT, dc.YELLOW, dc.ORANGE, dc.PURPLE, dc.PINK, dc.LIME, dc.BROWN, dc.GRAY, dc.CORAL, dc.PIG, dc.OLIVE, dc.WINE, dc.PEACH, dc.IVORY, dc.BEIGE] size = [600, 800] screen = pygame.display.set_mode(size) done = False clock = pygame.time.Clock() def runGame(): starttime = int(time.time()) bomb_image = pygame.image.load('bomb.png') bomb_image = pygame.transform.scale(bomb_image, (50, 50)) bombs = [] l = 3 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 life_image = pygame.image.load('life.png') life_image = pygame.transform.scale(life_image, (50, 50)) life = pygame.Rect(life_image.get_rect()) global done while not done: clock.tick(30) screen.fill(a[cc]) for i in range(l): life.left=size[0] // 2 - (i+1)*life.width // 2 life.top = life.height 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 i in range(l): screen.blit(life_image, life) for bomb in bombs: if bomb['rect'].colliderect(person): bomb['rect'].top -= 777 bomb['rect'].left = random.randint(0, size[0]) l = l - 1 if l == 0: done = True screen.blit(bomb_image, bomb['rect']) pygame.display.update() endtime = int(time.time()) score = endtime - starttime print('score :', score, 'sec', sep=' ') runGame() pygame.quit() # life 사진이 여러개가 안뜸 # 시간초 어디에서 셀지 명령문도 모르겠음