로그인

검색

Python
2013.11.21 22:59

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

MoA
조회 수 3209 추천 수 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 Views15455
    read more
  2. [첫게임 만들기] 8. Win or Lose

    Date2013.11.21 CategoryPython ByMoA Views3209
    Read More
  3. [첫게임 만들기] 9. 사운드

    Date2013.11.21 CategoryPython ByMoA Views3543
    Read More
  4. [한빛미디어] 머신러닝·딥러닝 도서 선택 가이드

    Date2023.06.11 CategoryDeeplearning ByOBG Views2652
    Read More
  5. "Node.js를 떠나며" - express를 만든 TJ의 글

    Date2022.06.23 CategoryTool/etc ByOBG Views2400
    Read More
  6. #pragma

    Date2013.07.28 CategoryAPI/MFC ByMoA Views2618
    Read More
  7. 10 Useful/Fun/Weird Github Repos You Have to Play Around With

    Date2023.12.28 CategorySite ByOBG Views2802
    Read More
  8. 2016년에 자바스크립트를 배우는 기분

    Date2016.12.27 CategoryTool/etc ByMoA Views2612
    Read More
  9. 2024년 가장 조회수 높은 소프트웨어 엔지니어링 발표들

    Date2025.02.18 CategorySite ByOBG Views1406
    Read More
  10. 2048 Game in Python

    Date2022.09.22 CategoryPython ByOBG Views2939
    Read More
  11. 2048게임 높은 점수 얻기 위한 알고리즘

    Date2014.03.29 CategoryAlgorithm ByMoA Views3159
    Read More
  12. A Beginner's Guide to Prompt Engineering with GitHub Copilot

    Date2024.04.04 CategoryLLM ByOBG Views1785
    Read More
  13. Address Bar Install for Progressive Web Apps on the Desktop

    Date2021.12.15 CategoryWeb ByOBG Views2462
    Read More
  14. AI-hub 공공데이터를 활용하여 한국어-영어 번역 LLM 만들기

    Date2025.01.14 CategoryLLM ByOBG Views1570
    Read More
  15. Anthropic, LLM의 내부를 이해하는데 있어 상당한 진전을 보임

    Date2024.06.03 CategoryLLM ByOBG Views2263
    Read More
  16. AS3 Code Library

    Date2013.10.11 CategoryLibrary ByMoA Views6818
    Read More
  17. ASCII 3D 렌더러 만들기

    Date2024.06.03 CategoryGraphic ByOBG Views2283
    Read More
  18. AWS VPC 피어링

    Date2022.11.03 CategoryTool/etc ByOBG Views3048
    Read More
  19. AWS 망 분리하기

    Date2022.09.06 CategoryTool/etc ByOBG Views2801
    Read More
  20. Base64 decoder (binary file로 저장)

    Date2012.11.15 CategorySite ByNaya Views2982
    Read More
  21. Binary 데이터 저장 by Google

    Date2013.07.28 CategoryC/C++ ByMoA Views3118
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 15 Next
/ 15