Menu
CoddyTech

アンカーポイント

CoddyのPygameジャーニー「ゲームのルール」セクションの一部。レッスン 7/30。

(10, 10)に描画されたスコアは左端から始まります。代わりに右端に配置するには、その幅が必要です。また、幅は数値が大きくなるにつれて変わります。Rectを使えば、計算を任せられます。

text.get_rect()には、center、topleft、topright、midtop、midbottomなど、任意の点をキーワード引数で指定できます。指定した点が指定した位置に来るようにRectが配置されます。

score_rect = score_text.get_rect(topright=(470, 10))
screen.blit(score_text, score_rect)

これで、スコアが0でも1000でも、右端は470の位置に保たれます。

背景がごちゃごちゃしている場合、プレートに載せるとテキストが読みやすくなります。inflate(12, 8)は、中心を同じ位置に保ちながら、幅を12ピクセル、高さを8ピクセル大きくしたRectのコピーを返します。まずプレートを描画し、その上にテキストを描画します。

pygame.draw.rect(screen, PLATE, score_rect.inflate(12, 8))

プレートの右端はテキストより6ピクセル先の476にあり、上端は4ピクセル上の6にあります。

challenge icon

チャレンジ

中級

スコアとタイトルを暗いプレートに載せましょう。

  1. PLATE = (60, 60, 90)を追加します。
  2. スコアをtopright=(470, 10)で配置し、その後ろにPLATEの長方形を描きます。Rectを(12, 8)だけ拡大します。
  3. メニューでは、Meteor Dodgeの後ろにPLATEの長方形を描きます。タイトルのRectを(240, 140)を中心に配置し、(24, 12)だけ拡大します。

「プレイ」を押すと、タイトルとスコアがプレートに載ります。その後、「確認」を押します。

自分で試してみよう

import random
import pygame

pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Meteor Dodge")
clock = pygame.time.Clock()
font = pygame.font.Font("assets/fonts/pixel.ttf", 24)
big_font = pygame.font.Font("assets/fonts/pixel.ttf", 48)
WHITE = (240, 240, 240)


def reset():
    global player, rock, score
    player = pygame.Rect(220, 300, 40, 40)
    rock = pygame.Rect(random.randint(0, 456), -24, 24, 24)
    score = 0


def draw_menu():
    screen.fill((30, 30, 46))
    title = big_font.render("Meteor Dodge", True, WHITE)
    screen.blit(title, title.get_rect(center=(240, 140)))
    hint = font.render("Press Enter to play", True, WHITE)
    screen.blit(hint, hint.get_rect(center=(240, 200)))


def draw_game():
    screen.fill((30, 30, 46))
    pygame.draw.rect(screen, (255, 196, 0), player)
    pygame.draw.rect(screen, (235, 80, 80), rock)
    score_text = font.render(f"Score: {score}", True, WHITE)
    screen.blit(score_text, (10, 10))


def play_frame():
    global score, state
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player.x -= 6
    if keys[pygame.K_RIGHT]:
        player.x += 6
    player.clamp_ip(screen.get_rect())

    rock.y += 4
    if rock.top > 360:
        rock.y = -24
        rock.x = random.randint(0, 456)
        score += 1
    if rock.colliderect(player):
        state = "over"
    draw_game()


def draw_over():
    draw_game()
    message = big_font.render("Game over", True, WHITE)
    screen.blit(message, message.get_rect(center=(240, 160)))
    hint = font.render("Press R to play again", True, WHITE)
    screen.blit(hint, hint.get_rect(center=(240, 200)))


reset()
state = "menu"

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN and state == "menu":
                state = "play"
            elif event.key == pygame.K_r and state == "over":
                reset()
                state = "play"

    if state == "menu":
        draw_menu()
    elif state == "play":
        play_frame()
    else:
        draw_over()
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

ゲームのルールのすべてのレッスン

自分で練習してみよう: Pygameプレイグラウンド