我正在做我的第一个项目,这是pygame中的一个骰子游戏,你按y键掷骰子,你和电脑对决,谁掷得高谁就赢。我把整个游戏都设置好了,但是我很难向谁赢的人显示文本,比如。。。 你赢了!对手掷3分。我可以在控制台中显示文本,但不能在pygame窗口中显示,并且不知道如何将其输入
参考代码
import pygame
import random
import os
import os.path
WIDTH = 750
HEIGHT = 750
FPS = 60
QUARTER_WIDTH = WIDTH // 4
MIDDLE_HEIGHT = HEIGHT // 2
white = (255, 255, 255)
pygame.init()
pygame.font.init()
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Dice Game")
# Fonts and Text
title_font = pygame.font.SysFont("comicsans", 70)
title_label = title_font.render("Would You like to roll? Y/N", 1, (255, 255, 255))
# Load images
dice1 = pygame.image.load(os.path.join("assets", "dice_1.png"))
dice2 = pygame.image.load(os.path.join("assets", "dice_2.png"))
dice3 = pygame.image.load(os.path.join("assets", "dice_3.png"))
dice4 = pygame.image.load(os.path.join("assets", "dice_4.png"))
dice5 = pygame.image.load(os.path.join("assets", "dice_5.png"))
dice6 = pygame.image.load(os.path.join("assets", "dice_6.png"))
# Indexed list to reference all the faces
all_dice = [None, dice1, dice2, dice3, dice4, dice5, dice6]
pygame.display.set_icon(dice6)
# Game Background
background = pygame.transform.scale(pygame.image.load(os.path.join("assets", "dice_board.png")), (WIDTH, HEIGHT))
### Function to perform the random parts of the game
def rollDice():
""" Generate the two random numbers, one for the Player and Opponent """
player_roll = random.randint(1, 6)
opponent_roll = random.randint(1, 6)
return player_roll, opponent_roll
### Main Loop
clock = pygame.time.Clock()
running = True
player_face = None # No dice before first roll
player_roll = 0
opponent_face = None # No dice before first roll
player_roll = 0
while running:
# handle user input
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
player_roll, opponent_roll = rollDice()
player_face = all_dice[player_roll]
opponent_face = all_dice[opponent_roll]
# Debug prints
font = pygame.font.Font("freesansbold.ttf", 32)
text1 = font.render(f'opponent won. They rolled a {opponent_roll}', 1, white)
text2 = font.render(f'You win! They rolled a {opponent_roll}', 1, white)
text3 = font.render('Tied!', 1, white)
textRect1 = text1.get_rect()
textRect2 = text2.get_rect()
textRect3 = text3.get_rect()
textRect1.center = (WIDTH // 2, HEIGHT // 2)
textRect2.center = (WIDTH // 2, HEIGHT // 2)
textRect3.center = (WIDTH // 2, HEIGHT // 2)
if opponent_roll > player_roll:
print(f"opponent won. They rolled a {opponent_roll}")
window.blit(text1, textRect1)
elif opponent_roll < player_roll:
print(f"You win! They rolled a {opponent_roll}")
window.blit(text2, textRect2)
elif opponent_roll == player_roll:
print("tied!")
window.blit(text3, textRect3)
# Reapint the screen
window.blit(background, (0, 0))
window.blit(title_label, (WIDTH // 2 - title_label.get_width() // 2, 250))
# Paint the dice faces
if (player_face != None):
window.blit(player_face, (QUARTER_WIDTH, MIDDLE_HEIGHT))
if (opponent_face != None):
window.blit(opponent_face, (3 * QUARTER_WIDTH, MIDDLE_HEIGHT))
# flush display changes
pygame.display.flip()
# Constrain FPS
clock.tick(FPS)
pygame.quit()
要呈现tetx,需要使用pygame.font
https://www.pygame.org/docs/ref/font.html
基本上,您必须创建位图字体,使用该字体将文本渲染到曲面,然后blit曲面
相关问题 更多 >
编程相关推荐