为什么我的变量在 while 循环中不起作用
Why aren't my variables working in the while loop
出于某种原因,当我 blit 我的图像并尝试移动我的立方体时,它不起作用,我的文本也不起作用。
import pygame
from random import randint
pygame.init()
pygame.font.init()
HEIGHT = 600
WIDTH = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Cube game")
clock = pygame.time.Clock()
TP_usage = 0
FPS = 60
Cube_Vel = 4
HP = 3
CubeX = 400
CubeY = 400
TP_staff = pygame.image.load("Game imag/TP staff.png")
TP_staff_rect = TP_staff.get_rect(center=(randint(10, 500), (randint(10, 770))))
No_tp_text = pygame.font.Font(None, 50)
No_tp_Warning = No_tp_text.render("NO TP USAGE", False, "white")
TP_usage_Stat_Font = pygame.font.Font(None, 30)
TP_usage_Stat = TP_usage_Stat_Font.render(f"TP usages: {TP_usage}", False, "white")
Cube_player = pygame.image.load("Game imag/Cube.png")
Cube_player_rect = Cube_player.get_rect(center=(CubeX, CubeY))
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#part that is not updating
screen.fill((0, 0, 0))
screen.blit(TP_usage_Stat, (500, 600))
screen.blit(Cube_player, Cube_player_rect)
screen.blit(TP_staff, TP_staff_rect)
if Cube_player_rect.colliderect(TP_staff_rect):
TP_usage += 1
print(TP_usage)
if TP_usage > 1:
TP_usage = 1
print(TP_usage)
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and CubeX - Cube_Vel > 0:
CubeX -= Cube_Vel
if keys[pygame.K_d] and CubeX + Cube_Vel < WIDTH:
CubeX += Cube_Vel
if keys[pygame.K_w] and CubeY - Cube_Vel > 0:
CubeY -= Cube_Vel
if keys[pygame.K_s] and CubeY + Cube_Vel < HEIGHT:
CubeY += Cube_Vel
pressed_keys = pygame.mouse.get_pressed()
if (pressed_keys[0]) and TP_usage > 0:
TP_usage -= 1
print(TP_usage)
mx, my = pygame.mouse.get_pos()
CubeX = mx
CubeY = my
if (pressed_keys[0]) and TP_usage < 0:
screen.blit(No_tp_Warning, (400, 500))
pygame.display.update()
之前我有这样的东西
import pygame
from random import randint
pygame.init()
pygame.font.init()
HEIGHT = 600
WIDTH = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Cube game")
clock = pygame.time.Clock()
TP_usage = 0
PhoX = 100
PhoY = -200
FPS = 60
Cube_Vel = 4
HP = 3
CubeX = 400
CubeY = 400
TP_staff = pygame.image.load("Game imag/TP staff.png")
TP_usage_Stat_Font = pygame.font.Font(None, 30)
TP_usage_Stat = TP_usage_Stat_Font.render(f"TP usages: {TP_usage}", False, "white")
Cube_player = pygame.image.load("Game imag/Cube.png")
# Timer
obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer, 5000)
TP_staff_rect = TP_staff.get_rect(center=(randint(10, 500), (randint(10, 770))))
game_on = True
while True:
No_tp_text = pygame.font.Font(None, 50)
No_tp_Warning = No_tp_text.render("NO TP USAGE", False, "white")
Cube_player_rect = Cube_player.get_rect(center=(CubeX, CubeY))
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.fill((0, 0, 0))
screen.blit(TP_usage_Stat, (500, 600))
screen.blit(Cube_player, Cube_player_rect)
screen.blit(TP_staff, TP_staff_rect)
if event.type == obstacle_timer:
TP_staff_rect = TP_staff.get_rect(center=(randint(10, 500), (randint(10, 770))))
if Cube_player_rect.colliderect(TP_staff_rect):
TP_usage += 1
print(TP_usage)
if TP_usage > 1:
TP_usage = 1
print(TP_usage)
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and CubeX - Cube_Vel > 0:
CubeX -= Cube_Vel
if keys[pygame.K_d] and CubeX + Cube_Vel < WIDTH:
CubeX += Cube_Vel
if keys[pygame.K_w] and CubeY - Cube_Vel > 0:
CubeY -= Cube_Vel
if keys[pygame.K_s] and CubeY + Cube_Vel < HEIGHT:
CubeY += Cube_Vel
pressed_keys = pygame.mouse.get_pressed()
if (pressed_keys[0]) and TP_usage > 0:
TP_usage -= 1
print(TP_usage)
mx, my = pygame.mouse.get_pos()
CubeX = mx
CubeY = my
if (pressed_keys[0]) and TP_usage < 0:
screen.blit(No_tp_Warning, (400, 500))
pygame.display.update()
问题是它每秒到处跑 60 次,所以我试图将工作人员 rect 移出 while 循环,然后一切正常,但现在没有像正常情况那样更新
您的游戏更新正常。问题是你用 Cube_player 移动并且你还绘制了 Cube_player_rect。解决方法如下:
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and Cube_player_rect.x - Cube_Vel > 0:
Cube_player_rect.x -= Cube_Vel
if keys[pygame.K_d] and Cube_player_rect.x + Cube_Vel < WIDTH - Cube_player_rect.w:
Cube_player_rect.x += Cube_Vel
if keys[pygame.K_w] and Cube_player_rect.y - Cube_Vel > 0:
Cube_player_rect.y -= Cube_Vel
if keys[pygame.K_s] and Cube_player_rect.y + Cube_Vel < HEIGHT - Cube_player_rect.h:
Cube_player_rect.y += Cube_Vel
pressed_keys = pygame.mouse.get_pressed()
if (pressed_keys[0]) and TP_usage > 0:
TP_usage -= 1
print(TP_usage)
mx, my = pygame.mouse.get_pos()
Cube_player_rect.x = mx
Cube_player_rect.y = my
if (pressed_keys[0]) and TP_usage < 0:
screen.blit(No_tp_Warning, (400, 400))
您看不到该字体,因为它的 y 坐标在图像之外。 TP_usage 未更新,因为您绘制了一个旧变量。试试我的绘制文本的功能:
def draw_text(text, color, size, x, y):
font = pygame.font.Font(None, size)
text = font.render(text, False, color)
text_rect = text.get_rect(center=(x, y))
screen.blit(text, text_rect)
这是你在主循环中的使用方式:
draw_text(f"TP usages: {TP_usage}", "white", 30, 500, 500)
希望对你有所帮助,亚当
出于某种原因,当我 blit 我的图像并尝试移动我的立方体时,它不起作用,我的文本也不起作用。
import pygame
from random import randint
pygame.init()
pygame.font.init()
HEIGHT = 600
WIDTH = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Cube game")
clock = pygame.time.Clock()
TP_usage = 0
FPS = 60
Cube_Vel = 4
HP = 3
CubeX = 400
CubeY = 400
TP_staff = pygame.image.load("Game imag/TP staff.png")
TP_staff_rect = TP_staff.get_rect(center=(randint(10, 500), (randint(10, 770))))
No_tp_text = pygame.font.Font(None, 50)
No_tp_Warning = No_tp_text.render("NO TP USAGE", False, "white")
TP_usage_Stat_Font = pygame.font.Font(None, 30)
TP_usage_Stat = TP_usage_Stat_Font.render(f"TP usages: {TP_usage}", False, "white")
Cube_player = pygame.image.load("Game imag/Cube.png")
Cube_player_rect = Cube_player.get_rect(center=(CubeX, CubeY))
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#part that is not updating
screen.fill((0, 0, 0))
screen.blit(TP_usage_Stat, (500, 600))
screen.blit(Cube_player, Cube_player_rect)
screen.blit(TP_staff, TP_staff_rect)
if Cube_player_rect.colliderect(TP_staff_rect):
TP_usage += 1
print(TP_usage)
if TP_usage > 1:
TP_usage = 1
print(TP_usage)
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and CubeX - Cube_Vel > 0:
CubeX -= Cube_Vel
if keys[pygame.K_d] and CubeX + Cube_Vel < WIDTH:
CubeX += Cube_Vel
if keys[pygame.K_w] and CubeY - Cube_Vel > 0:
CubeY -= Cube_Vel
if keys[pygame.K_s] and CubeY + Cube_Vel < HEIGHT:
CubeY += Cube_Vel
pressed_keys = pygame.mouse.get_pressed()
if (pressed_keys[0]) and TP_usage > 0:
TP_usage -= 1
print(TP_usage)
mx, my = pygame.mouse.get_pos()
CubeX = mx
CubeY = my
if (pressed_keys[0]) and TP_usage < 0:
screen.blit(No_tp_Warning, (400, 500))
pygame.display.update()
之前我有这样的东西
import pygame
from random import randint
pygame.init()
pygame.font.init()
HEIGHT = 600
WIDTH = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Cube game")
clock = pygame.time.Clock()
TP_usage = 0
PhoX = 100
PhoY = -200
FPS = 60
Cube_Vel = 4
HP = 3
CubeX = 400
CubeY = 400
TP_staff = pygame.image.load("Game imag/TP staff.png")
TP_usage_Stat_Font = pygame.font.Font(None, 30)
TP_usage_Stat = TP_usage_Stat_Font.render(f"TP usages: {TP_usage}", False, "white")
Cube_player = pygame.image.load("Game imag/Cube.png")
# Timer
obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer, 5000)
TP_staff_rect = TP_staff.get_rect(center=(randint(10, 500), (randint(10, 770))))
game_on = True
while True:
No_tp_text = pygame.font.Font(None, 50)
No_tp_Warning = No_tp_text.render("NO TP USAGE", False, "white")
Cube_player_rect = Cube_player.get_rect(center=(CubeX, CubeY))
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.fill((0, 0, 0))
screen.blit(TP_usage_Stat, (500, 600))
screen.blit(Cube_player, Cube_player_rect)
screen.blit(TP_staff, TP_staff_rect)
if event.type == obstacle_timer:
TP_staff_rect = TP_staff.get_rect(center=(randint(10, 500), (randint(10, 770))))
if Cube_player_rect.colliderect(TP_staff_rect):
TP_usage += 1
print(TP_usage)
if TP_usage > 1:
TP_usage = 1
print(TP_usage)
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and CubeX - Cube_Vel > 0:
CubeX -= Cube_Vel
if keys[pygame.K_d] and CubeX + Cube_Vel < WIDTH:
CubeX += Cube_Vel
if keys[pygame.K_w] and CubeY - Cube_Vel > 0:
CubeY -= Cube_Vel
if keys[pygame.K_s] and CubeY + Cube_Vel < HEIGHT:
CubeY += Cube_Vel
pressed_keys = pygame.mouse.get_pressed()
if (pressed_keys[0]) and TP_usage > 0:
TP_usage -= 1
print(TP_usage)
mx, my = pygame.mouse.get_pos()
CubeX = mx
CubeY = my
if (pressed_keys[0]) and TP_usage < 0:
screen.blit(No_tp_Warning, (400, 500))
pygame.display.update()
问题是它每秒到处跑 60 次,所以我试图将工作人员 rect 移出 while 循环,然后一切正常,但现在没有像正常情况那样更新
您的游戏更新正常。问题是你用 Cube_player 移动并且你还绘制了 Cube_player_rect。解决方法如下:
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and Cube_player_rect.x - Cube_Vel > 0:
Cube_player_rect.x -= Cube_Vel
if keys[pygame.K_d] and Cube_player_rect.x + Cube_Vel < WIDTH - Cube_player_rect.w:
Cube_player_rect.x += Cube_Vel
if keys[pygame.K_w] and Cube_player_rect.y - Cube_Vel > 0:
Cube_player_rect.y -= Cube_Vel
if keys[pygame.K_s] and Cube_player_rect.y + Cube_Vel < HEIGHT - Cube_player_rect.h:
Cube_player_rect.y += Cube_Vel
pressed_keys = pygame.mouse.get_pressed()
if (pressed_keys[0]) and TP_usage > 0:
TP_usage -= 1
print(TP_usage)
mx, my = pygame.mouse.get_pos()
Cube_player_rect.x = mx
Cube_player_rect.y = my
if (pressed_keys[0]) and TP_usage < 0:
screen.blit(No_tp_Warning, (400, 400))
您看不到该字体,因为它的 y 坐标在图像之外。 TP_usage 未更新,因为您绘制了一个旧变量。试试我的绘制文本的功能:
def draw_text(text, color, size, x, y):
font = pygame.font.Font(None, size)
text = font.render(text, False, color)
text_rect = text.get_rect(center=(x, y))
screen.blit(text, text_rect)
这是你在主循环中的使用方式:
draw_text(f"TP usages: {TP_usage}", "white", 30, 500, 500)
希望对你有所帮助,亚当