로그인

검색

Python
2013.11.21 22:59

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

MoA
조회 수 3205 추천 수 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

 

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

?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
공지 Tool/etc Programming 게시판 관련 2 MoA 2014.11.01 15452
138 C/C++ memset vs for 초기화. 속도 차이가 얼마나 날까? 2 MoA 2013.07.28 3054
137 Library Math Library Naya 2012.08.02 3016
136 Tool/etc Machine Learning for Video Games MoA 2015.07.27 3415
135 Deeplearning LSTM-AE를 이용한 시퀀스 데이터 이상 탐지 OBG 2023.08.14 2240
134 JAVA/Android logcat 사용법 MoA 2013.05.28 5785
133 LLM LLM 출력 속도 24배 높여주는 라이브러리 등장했다 OBG 2023.06.30 2551
132 LLM llama3 implemented from scratch OBG 2024.05.24 1616
131 Deeplearning LLaMA: INT8 edition OBG 2023.03.09 2641
130 API/MFC Legacy MFC 어플리케이션을 MFC feature pack으로 포팅 MoA 2013.07.30 2672
129 Deeplearning Keras를 활용한 주식 가격 예측 OBG 2022.09.02 2458
128 Site IT 세미나 유튜브 동영상 Naya 2012.09.10 2688
127 API/MFC InvalidateRect 함수, flag성 메세지 MoA 2013.07.25 2615
126 API/MFC HWND와 HINSTANCE MoA 2013.07.28 3598
125 Tool/etc HuggingFace 공동창업자가 추천하는 AI 분야 입문 서적 OBG 2024.05.24 2605
124 Tool/etc How to stop programmers to copy the code from GitHub when they leave the company? OBG 2024.01.02 2785
123 Tool/etc How To Set Up Multi-Factor Authentication for SSH on Ubuntu 20.04 OBG 2023.01.17 2635
122 Web How to send dynamic charts with a Slack bot OBG 2022.05.31 2255
121 LLM How LLMs Work ? Explained in 9 Steps — Transformer Architecture OBG 2024.04.11 2458
120 Library High-speed Charting Control MoA 2013.07.28 2830
119 Site Great summary cheat sheet (OpenCV) MoA 2013.01.04 3048
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 ... 15 Next
/ 15