메뉴 건너뛰기

OBG

Programming

Python
2013.11.21 22:59

[첫게임 만들기] 8. Win or Lose

MoA
조회 수 421 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

지금까지 만든 걸 실행하면 에너지가 다 닳아도 게임이 끝나지 않는다. 게임을 끝내려면 언젠가는 while 루프를 빠져나와야 한다. 먼저 while 루프를 변경해보자.

 

# 4 - 무한 루프
while 1:
    badtimer-=1

이 부분을 아래와 같이 바꾼다.

 

# 4 - 무한 루프
running = 1 # 게임 진행 여부 체크
exitcode = 0 # 승리 / 패배 체크
while running:
    badtimer-=1

이렇게 하면 running이 0으로 바뀔 때 while 루프를 빠져나와 게임이 끝나게 된다.

하지만 게임이 그냥 끝나면 재미 없다. 게임에서 승리하였는지 패배하였는지 표시는 해야하지 않겠는가?

이를 위해 게임이 끝난 후 표시할 이미지를 불러온다.

이미지를 불러오는 3번 부분 아래에 아래 코드를 삽입한다.

 

gameover = pygame.image.load("resources/images/gameover.png")
youwin = pygame.image.load("resources/images/youwin.png")

그리고 while 루프의 맨 마지막에 승리 / 패배 체크 하는 부분을 삽입한다.

 

#10 - Win/Lose 체크
    if pygame.time.get_ticks()>=90000:
        running=0
        exitcode=1
    if healthvalue<=0:
        running=0
        exitcode=0
    if accuracy[1]!=0:
        accuracyPer=accuracy[0]/accuracy[1]*100
    else:
        accuracyPer=0

accuracyPer는 명중률을 %로 나타내기 위한 변수이다.

다음으로 승리 / 패배 화면을 표시해 보자. while 루프가 끝난 다음에 아래 코드를 삽입한다.

 

# 11 - Win/lose 표시       
if exitcode==0: # lose
    pygame.font.init()
    font = pygame.font.Font(None, 24)
    text = font.render("Accuracy: " + format(accuracyPer, ".2f") + "%", True, (255,0,0))
    textRect = text.get_rect()
    textRect.centerx = screen.get_rect().centerx
    textRect.centery = screen.get_rect().centery+24
    screen.blit(gameover, (0,0))
    screen.blit(text, textRect)
else: # win
    pygame.font.init()
    font = pygame.font.Font(None, 24)
    text = font.render("Accuracy: " + format(accuracyPer, ".2f") + "%", True, (0,255,0))
    textRect = text.get_rect()
    textRect.centerx = screen.get_rect().centerx
    textRect.centery = screen.get_rect().centery+24
    screen.blit(youwin, (0,0))
    screen.blit(text, textRect)
    
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
    pygame.display.flip()

앞의 강좌를 잘 따라왔다면 크게 어려운 부분은 없을 것이다.

 

게임이 끝나면 아래와 같은 화면이 뜰 것이다.

 

lose.png

 

엔딩 이미지가 게임 화면을 완전히 덮는게 아니라 투명하게 보인다. 이는 엔딩 이미지가 투명하기 때문이다.

?

  1. Programming 게시판 관련

    Date2014.11.01 CategoryTool/etc ByMoA Views1714
    read more
  2. CFormView

    Date2012.01.09 CategoryAPI/MFC By너울 Views418
    Read More
  3. Simple Add-On Wait Dialog in MFC

    Date2013.11.21 CategoryAPI/MFC ByMoA Views420
    Read More
  4. [첫게임 만들기] 8. Win or Lose

    Date2013.11.21 CategoryPython ByMoA Views421
    Read More
  5. [액션게임 만들기] 9. 캐릭터 액션 구현 2

    Date2014.05.07 CategoryPython ByMoA Views427
    Read More
  6. WaitForSingleObject와의 삽질..

    Date2013.07.28 CategoryAPI/MFC ByMoA Views429
    Read More
  7. GetLastInputInfo 함수

    Date2013.12.06 CategoryAPI/MFC ByMoA Views434
    Read More
  8. 시스템 분석을 위한 쉘 명령어

    Date2013.05.28 CategoryJAVA/Android ByMoA Views436
    Read More
  9. 태스크 대화상자 (Task Dialog)

    Date2013.10.22 CategoryAPI/MFC ByMoA Views436
    Read More
  10. PHP: 잘못된 디자인의 프랙탈

    Date2016.07.10 CategorySite ByMoA Views436
    Read More
  11. Google의 C++ 라이브러리

    Date2012.08.02 CategoryLibrary ByNaya Views439
    Read More
  12. 안드로이드 프로세스 확인

    Date2013.04.09 CategoryJAVA/Android ByMoA Views439
    Read More
  13. UpdateData(TRUE) or UpdateData(FALSE) 구분

    Date2013.07.28 CategoryAPI/MFC ByMoA Views440
    Read More
  14. Base64 decoder (binary file로 저장)

    Date2012.11.15 CategorySite ByNaya Views441
    Read More
  15. 윈도우 프로그램의 종료 메시지 순서

    Date2012.08.03 CategoryAPI/MFC ByNaya Views446
    Read More
  16. 작업자 스레드(Worker Thread) 와 사용자 인터페이스 스레드(User Interface Thread)

    Date2013.07.28 CategoryAPI/MFC ByMoA Views447
    Read More
  17. [첫게임 만들기] 9. 사운드

    Date2013.11.21 CategoryPython ByMoA Views449
    Read More
  18. [액션게임 만들기] 6. 게임 화면 출력

    Date2014.05.07 CategoryPython ByMoA Views450
    Read More
  19. [첫게임 만들기] 2. 배경 그리기, Bunny 움직이게 하기

    Date2013.11.21 CategoryPython ByMoA Views455
    Read More
  20. DLL 이란

    Date2013.07.28 CategoryAPI/MFC ByMoA Views457
    Read More
  21. 소스 공유 사이트

    Date2012.08.02 CategorySite ByNaya Views458
    Read More
Board Pagination Prev 1 ... 3 4 5 6 7 8 9 10 11 12 ... 15 Next
/ 15
위로