import pygame # 1. pygame 선언
import random
import time
import data.colorList as dc
cc = 1
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
f = 0
clock = pygame.time.Clock()
def startGame():
global cc
global done
global f
while not done:
screen.fill(a[cc-1])
sysfont = pygame.font.SysFont(None, 64)
text = sysfont.render("Select Background Color", True, dc.WHITE)
screen.blit(text, (0, 0))
sysfont = pygame.font.SysFont(None, 32)
text = sysfont.render("Press Space to Select", True, dc.WHITE)
screen.blit(text, (0, 40))
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and f == 0:
if event.key == pygame.K_LEFT:
if cc == 0:
cc = 19
else:
cc -= 1
elif event.key == pygame.K_RIGHT:
if cc == 19:
cc = 0
else:
cc += 1
elif event.key == pygame.K_UP:
if cc < 4:
cc += 16
elif cc == 4:
cc = 16
else:
cc -= 4
elif event.key == pygame.K_DOWN:
if cc == 16:
cc = 4
elif cc > 16:
cc-= 16
else:
cc += 4
elif event.key == pygame.K_SPACE:
f = 1
sysfont = pygame.font.SysFont(None, 90)
text = sysfont.render("GAME START!", True, dc.WHITE)
screen.blit(text, (100, 400))
if event.type == pygame.MOUSEBUTTONDOWN and f == 1:
click_x, click_y = event.pos
if 100 <= click_x <= 525 and 350 <= click_y <= 450:
done = True
pygame.display.update()
def runGame():
global cc
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 = []
life_image = pygame.image.load('life.png')
life_image = pygame.transform.scale(life_image, (50, 50))
life.append(pygame.Rect(life_image.get_rect()))
life.append(pygame.Rect(life_image.get_rect()))
life.append(pygame.Rect(life_image.get_rect()))
starttime = int(time.time())
global done
while not done:
clock.tick(30)
screen.fill(a[cc-1])
timetime = int(time.time())
sysfont = pygame.font.SysFont(None, 72)
text = sysfont.render(str(timetime-starttime), True, dc.WHITE) # BLUE = (0, 0, 255)
screen.blit(text, (0, 0))
for i in range(l):
life[i].left=size[0] // 2 - (i+1)*life[i].width
life[i].top = 0
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 = -7.77
elif event.key == pygame.K_RIGHT:
person_dx = 7.77
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[i])
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=' ')
startGame()
done = False
runGame()
pygame.quit()
top of page

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