top of page

소스 코드 제출

공개·회원 52명

숙제

import pygame #게임시작

import random

import time


pygame.init()

he = 1

random1=random.randint(0,256)

G = random.randint(0, 256)

a = random.randint(0, 256)

BLACK = (random1,G,a)

size = [600,800] #필드생성

screen = pygame.display.set_mode(size)


done = False

clock = pygame.time.Clock()


def runGame() :

ail = pygame.image.load('frankenstein.png') #괴물

sky = pygame.image.load('heart.png') #하트 1

hart = pygame.image.load('heart.png') #하트 2

d = pygame.image.load('heart.png') #하츠 3

survive = 2

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

ail = pygame.transform.scale(ail, (100, 100)) # sky를 (400,300)크기로 설정


global done #done을 전역변수로

global he

while not done:

clock.tick(30)

screen.fill(BLACK)

sysfont = pygame.font.SysFont(None, 40)

text = sysfont.render('if danger press space', True, (0,0,0))

screen.blit(text, (0, 0))

screen.blit(ail, (rect.left, -20)) # 폭탄 생성 위치에 ail 두기


if survive == 1 :

sky = pygame.transform.scale(sky, (50, 50)) # 하트 1를 (50,50)크기로 설정

screen.blit(sky, (550, 0)) # 하트 1를 좌표 550,0에다 두기.

hart = pygame.transform.scale(hart, (50, 50)) # 하트 2를 (50,50)크기로 설정

screen.blit(hart, (500, 0)) # 하트 2를 좌표 500,0에다 두기.


elif survive == 2 :

d = pygame.transform.scale(d, (50, 50)) # 하트 3를 (50,50)크기로 설정

screen.blit(d, (450, 0)) # 하트 3를 좌표 450,0에다 두기.

sky = pygame.transform.scale(sky, (50, 50)) # 하트 1를 (50,50)크기로 설정

screen.blit(sky, (550, 0)) # 하트 1를 좌표 550,0에다 두기.

hart = pygame.transform.scale(hart, (50, 50)) # sky를 (50,50)크기로 설정

screen.blit(hart, (500, 0)) # 하트 3를 좌표 500,0에다 두기.


else :


sky = pygame.transform.scale(sky, (50, 50)) # 하트 1를 (50,50)크기로 설정

screen.blit(sky, (550, 0)) # 하트 1를 좌표 550,0에다 두기.

screen.blit(person_image, person)

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.key == pygame.K_SPACE :

if he == 1 :

survive += 1

he = 0



elif event.type == pygame.KEYUP : # 키보드가 떼지는 이벤트 발생시

if event.key == pygame.K_LEFT : #person_dx를 0으로 바꿔서 계속 움직일수있게

person_dx = 0

elif event.key == pygame.K_RIGHT : #79줄 참고

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, 20) #스피드

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 :

sky = pygame.image.load('photo-1631015248879-8896f6389b2b.jpg')


sky = pygame.transform.scale(sky, (600, 800)) # sky를 (400,300)크기로 설정

screen.blit(sky, (0, 0)) # sky를 좌표0,0에다 두기.

pygame.display.update()

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, 11) # 스피드

bombs.append({'rect': rect, 'dy': dy})

survive = survive - 1

screen.blit(bomb_image, bomb['rect']) #screen에 폭탄을 보여줌

pygame.display.update() #업데이트(위치,Quit(종료),폭탄,플레이어)

runGame()

pygame.quit()

5회 조회
주소 : 경기도 용인시 광교중앙로 302 블루 스퀘어 602호
연락처 : 031) 216 - 1546 ,     031) 215 - 1546
사업자등록번호 : 465-92-00916
​학원 등록 제 4603호
bottom of page