HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🥋
Pygame Lecture
/
🐟
004 잡은 물고기와 경과시간을 텍스트로 추가하기
🐟

004 잡은 물고기와 경과시간을 텍스트로 추가하기

1. 실행 코드2. 상세 내용3. 실행 결과

1. 실행 코드

import pygame as pg pg.init() 화면가로길이 = 600 화면세로길이 = 800 화면 = pg.display.set_mode((화면가로길이, 화면세로길이)) pg.display.set_caption("생선잡기 게임") 배경이미지 = pg.image.load("img/배경.png") 배경이미지 = pg.transform.scale(배경이미지, (화면가로길이, 화면세로길이)) 화면.blit(배경이미지, (0, 0)) 물고기1 = pg.image.load("img/물고기1.png") 물고기1 = pg.transform.scale(물고기1, (64, 64)) 물고기2 = pg.image.load("img/물고기2.png") 물고기2 = pg.transform.scale(물고기2, (64, 64)) 스코어바 = pg.image.load("img/스코어바.png") 스코어바 = pg.transform.scale(스코어바, (250, 70)) 시간바 = pg.image.load("img/시간바.png") 시간바 = pg.transform.scale(시간바, (200, 55)) pg.display.update() 폰트 = pg.font.SysFont("hy얕은샘물m", 30, True) 시작시간 = pg.time.get_ticks() 잡은물고기 = 0 while True: 경과시간 = round((pg.time.get_ticks() - 시작시간) / 1000, 1) 화면.blit(스코어바, (350, 2)) 화면.blit(시간바, (0, 10)) 시간 = 폰트.render(f"{경과시간} 초", True, (0, 0, 0)) 화면.blit(시간, (60, 28)) 물고기점수 = 폰트.render(f"{잡은물고기} 마리", True, (0, 0, 0)) 화면.blit(물고기점수, (450, 28)) pg.display.update() for 이벤트 in pg.event.get(): if 이벤트.type == pg.QUIT: quit()

2. 상세 내용

폰트 = pg.font.SysFont("hy얕은샘물m", 30, True) 시작시간 = pg.time.get_ticks() 잡은물고기 = 0
  • 텍스트를 만들기위해 먼저 폰트를 설정해줍니다.pg.font.SysFont("폰트이름",크기,굵게)를 설정합니다.
    • 파이게임 폰트에 대한 공식 문서입니다.
    • pygame.font - pygame v2.0.0.dev15 documentation
      pygame module for loading and rendering fonts The font module allows for rendering TrueType fonts into a new Surface object. It accepts any UCS-2 character ('u0001' to 'uFFFF'). This module is optional and requires SDL_ttf as a dependency. You should test that is available and initialized before attempting to use the module.
      pygame.font - pygame v2.0.0.dev15 documentation
      https://www.pygame.org/docs/ref/font.html
      pygame.font - pygame v2.0.0.dev15 documentation
  • pg.time.get_ticks()으로 이 함수는 pg.init() 이후로 시간을 측정합니다. 게임시간을 측정하기 위해 이 값을 변수에 할당합니다.
  • 잡은물고기를 측정하기 위해 변수를 선언합니다.
while True: 경과시간 = round((pg.time.get_ticks() - 시작시간) / 1000, 1) 화면.blit(스코어바, (350, 2)) 화면.blit(시간바, (0, 10)) 시간 = 폰트.render(f"{경과시간} 초", True, (0, 0, 0)) 화면.blit(시간, (60, 28)) 물고기점수 = 폰트.render(f"{잡은물고기} 마리", True, (0, 0, 0)) 화면.blit(물고기점수, (450, 28)) pg.display.update()
  • While 문 내에서 경과시간, 스코어바, 물고기점수을 계속 업데이트 하는 코드입니다.
  • 경과시간을 계속적으로 업데이트하기 위해 [현재시간(pg.time.get_ticks())- 시작시간]의 결과값은 밀리초 이므로 이것을 1,000으로 나누어 초를 계산하고 Round함수로 소숫점이하 1의 자리까지 표시할 수 있도록 합니다.
  • 클릭횟수와 경과시간을 업데이트 하기전에 그 뒤에 보여줄 스코어바와 시간바를 지속적으로 추가합니다.
    • 🚫
      바들을 blit()하는 코드가 텍스트를 추가하는 코드보다 아래쪽에 있다면 결과는 바들만 보여주게됩니다.
  • 폰트.render("문자열",안티앨리어싱 여부,(R,G,B)) : 폰트를 기반으로 문자열을 Surface객체로 변환해줍니다.
    • 💡
      Tip: Python 3.6버전 이상 F-string기법을 사용할 수 있습니다.
      사용법 : f"문자열 {함수,또는 변수}"
      2. Lexical analysis - Python 3.9.0 documentation
      A Python program is divided into a number of logical lines. The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements).
      2. Lexical analysis - Python 3.9.0 documentation
      https://docs.python.org/3.9/reference/lexical_analysis.html#formatted-string-literals
  • 각 Surface객체들을 업데이트 합니다.
  • 결과적으로 화면에 스코어바,시간바→시간→물고기점수 순으로 계속 화면에 업데이트가 이루어집니다.

3. 실행 결과

notion image