import pygame #게임시작
import random
import time
pygame.init()
BLACK = (0,0,0)
size = [600,800] #필드생성
screen = pygame.display.set_mode(size)
done = False
clock = pygame.time.Clock()
def runGame() :
survive = 1
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('plane.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
global done #done을 전역변수로
while not done:
clock.tick(30)
screen.fill(BLACK) #화면을 까맣게
for event in pygame.event.get() :
if event.type == pygame.QUIT : #done이 True라면 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으로 바꿔서 계속 움직일수있게
person_dx = 0
elif event.key == pygame.K_RIGHT : #46번 참고
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) #가로 3~9에 랜덤 사이를 폭탄에 가로
bombs.append({'rect': rect, 'dy': dy}) #폭탄을 리스트에 추가
person.left = person.left + person_dx #플레이어 위치는 지금 위치에서 - 거나 + 함(+0.1=-0.01),(+0.1)
if person.left < 0 : #플레이어가 screen 을 넘으려 한다면?
person.left = 0 #플레이어를 잡아둠
elif person.left > size[0] - person.width :
person.left = size[0] - person.height
screen.blit(person_image, person) #screen에 플레이어을 보여줌
for bomb in bombs :
if bomb['rect'].colliderect(person) : #만약 폭탄이 플레이어에 다으면?
if survive == 0 :
e=pygame.image.load('photo-1631015248879-8896f6389b2b.jpg')
e=pygame.transform.scale(e, (500,500))
time.sleep(3)
done = True
else :
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) # 가로 3~9에 랜덤 사이를 폭탄에 가로
bombs.append({'rect': rect, 'dy': dy})
survive = survive - 1
screen.blit(bomb_image, bomb['rect']) #screen에 폭탄을 보여줌
pygame.display.update() #업데이트(위치,Quit(종료),폭탄,플레이어)
runGame() #게임 끋
pygame.quit() top of page

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


