Saya mencoba membuat penghitung pemain di mana jika pemain menekan panah atas pada keyboard saya ingin pemain naik satu dan menampilkan penghitung naik dan sama dengan turun. Tapi penghitungnya tidak muncul

import pygame
pygame.init()
pygame.display.set_mode()
pygame.font.init()
Red = (247, 12, 12)
White = (255,255,255)
Blue = (0, 157, 255)
Black=(0,0,0)
X = 1400
Y = 1000
players = 0
display_surface = pygame.display.set_mode((X, Y))
font = pygame.font.Font('freesansbold.ttf', 20)
text = font.render("Player one is:", True, Black, White)
textRect = text.get_rect()
textRect.center = (X // 4.4, Y // 3.6)
str(players)
while True:
    display_surface.fill(White)
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); #sys.exit() if sys is imported
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                print("Hey, you pressed the key, '0'!")
            if event.key == pygame.K_1:
                print("Doing whatever")
            if event.key == pygame.K_UP:
                int(players)
                players = players+1
                str(players)
                text1 = font.render(players, True, Black, White)
                text1Rect = text1.get_rect()
                text1Rect.center = (X // 4.4, Y // 3)
                display_surface.blit(text, textRect)
                int(players)
pygame.display.update()
1
Cameron Butcher 12 Mei 2021, 01:47

2 jawaban

Jawaban Terbaik

str(x) mengonversi x dan mengembalikan string:

str(players)

str(players)

Namun, Anda tidak membutuhkannya sama sekali. Anda harus merender teks setelah membersihkan tampilan dan sebelum memperbarui tampilan. Inisialisasi text1 sebelum loop aplikasi:

while True:
    # [...]

    # clear display
    display_surface.fill(White)
   
    # render secene
    display_surface.blit(text, textRect)
    display_surface.blit(text1, text1Rect)
    
    # [...]
    
    # update display
    pygame.display.update()

Ubah dan render ulang permukaan text1 saat UP ditekan:

if event.key == pygame.K_UP:
    players += 1
    text = font.render("Player one is: " + str(players), True, Black, White)
    textRect = text.get_rect(center = (X // 4.4, Y // 3))

Contoh lengkap:

import pygame
pygame.init()
pygame.font.init()
Red = (247, 12, 12)
White = (255,255,255)
Blue = (0, 157, 255)
Black=(0,0,0)
X = 1400
Y = 1000
players = 0
display_surface = pygame.display.set_mode((X, Y))
font = pygame.font.Font('freesansbold.ttf', 20)

text = font.render("Player one is:", True, Black, White)
textRect = text.get_rect(center = (X // 4.4, Y // 3.6))
text1 = font.render(str(players), True, Black, White)
text1Rect = text.get_rect(center = (X // 4.4, Y // 3))

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                print("Hey, you pressed the key, '0'!")
            if event.key == pygame.K_1:
                print("Doing whatever")
            if event.key == pygame.K_UP:
                players += 1
                text1 = font.render(str(players), True, Black, White)
                text1Rect = text.get_rect(center = (X // 4.4, Y // 3))

    display_surface.fill(White)
    display_surface.blit(text, textRect)
    display_surface.blit(text1, text1Rect)
    pygame.display.update()

pygame.quit()
exit()
0
Rabbid76 12 Mei 2021, 04:56

Tempatkan kode rendering Anda di loop while luar:

import pygame
pygame.init()
pygame.display.set_mode()
pygame.font.init()
Red = (247, 12, 12)
White = (255,255,255)
Blue = (0, 157, 255)
Black=(0,0,0)
X = 1400
Y = 1000
players = 0
display_surface = pygame.display.set_mode((X, Y))
font = pygame.font.Font('freesansbold.ttf', 20)
text = font.render("Player one is:", True, Black, White)
textRect = text.get_rect()
textRect.center = (X // 4.4, Y // 3.6)
str(players)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); #sys.exit() if sys is imported
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                print("Hey, you pressed the key, '0'!")
            if event.key == pygame.K_1:
                print("Doing whatever")
            if event.key == pygame.K_UP:
                int(players)
                players = players+1
                str(players)
                int(players)


    # Render here
    display_surface.fill(White)

    text1 = font.render(players, True, Black, White)
    text1Rect = text1.get_rect()
    text1Rect.center = (X // 4.4, Y // 3)
    display_surface.blit(text, textRect)

    pygame.display.update()
0
Sean Xie 12 Mei 2021, 03:20