Menu
CoddyTech

The Night

Lesson 4 of 20 in Coddy's Pygame Project: Hold the Keep course.

The game will soon hold a lot: the knight, monsters, bolts, gems, the time. Keep all of it in one object, night, made from a Night class. Starting over later is then one line, night = Night().

class Night:
    def __init__(self):
        self.knight = Knight()
        self.time = 0

    def update(self, keys):
        self.time += 1
        self.knight.move(keys)

update runs one frame of the night. At 60 frames a second, a four minute night is NIGHT = 60 * 60 * 4 frames.

To show the time left, turn frames into minutes and seconds with // (whole division) and % (what is left over):

def clock_text(frames):
    seconds = frames // 60
    return f"{seconds // 60}:{seconds % 60:02d}"

:02d writes a number with at least two digits, so 65 seconds reads 1:05, not 1:5.

challenge icon

Challenge

Easy
  1. Add NIGHT = 60 * 60 * 4, WHITE = (240, 240, 240) and font = pygame.font.Font("assets/fonts/pixel.ttf", 24).
  2. Write the Night class above and clock_text(frames).
  3. Replace knight = Knight() with night = Night(). In the loop call night.update(pygame.key.get_pressed()), and draw night.knight (knight = night.knight before drawing is fine).
  4. Draw the time left in white, centred at (360, 24): clock_text(max(0, NIGHT - night.time)) + " until dawn".

Try it yourself

import pygame

WIDTH, HEIGHT = 720, 480
TILE = 48

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Hold the Keep")
clock = pygame.time.Clock()


def load(name):
    return pygame.image.load("assets/images/" + name).convert_alpha()


floor_img = load("floor.png")
wall_img = load("wall.png")
knight_img = load("knight.png")

courtyard = pygame.Rect(TILE, TILE, WIDTH - 2 * TILE, HEIGHT - 2 * TILE)
background = pygame.Surface((WIDTH, HEIGHT))
for row in range(HEIGHT // TILE):
    for col in range(WIDTH // TILE):
        x = col * TILE
        y = row * TILE
        if courtyard.collidepoint(x, y):
            background.blit(floor_img, (x, y))
        else:
            background.blit(wall_img, (x, y))


def draw(image, center):
    screen.blit(image, image.get_rect(center=center))


class Knight(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = knight_img
        self.rect = pygame.Rect(0, 0, 24, 24)
        self.rect.center = courtyard.center
        self.speed = 3

    def move(self, keys):
        if keys[pygame.K_LEFT]:
            self.rect.x -= self.speed
        if keys[pygame.K_RIGHT]:
            self.rect.x += self.speed
        if keys[pygame.K_UP]:
            self.rect.y -= self.speed
        if keys[pygame.K_DOWN]:
            self.rect.y += self.speed
        self.rect.clamp_ip(courtyard)


knight = Knight()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    knight.move(pygame.key.get_pressed())

    screen.blit(background, (0, 0))
    draw(knight.image, knight.rect.center)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

All lessons in Pygame Project: Hold the Keep

Practice on your own: Pygame playground