gpt4 book ai didi

python - 如何在pygame中移动ASCII字符?

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:06 24 4
gpt4 key购买 nike

我正在开发一款类似 rogue-like 的 ASCII 游戏。我想打印 @ 作为播放器并用箭头移动它。我尝试的一切都不起作用。

import sys, pygame
pygame.init()
pygame.font.init()

white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
black = (0, 0, 0)

playerposx = 356
playerposy = 254
playerpos = (playerposx, playerposy)
playervel = 16

win = pygame.display.set_mode((720,516))

pygame.display.flip()
pygame.display.set_caption('Tiles')
font = pygame.font.SysFont("timesnewroman", 16)
text = font.render('@', True, white, blue)
pygame.display.flip()


run = True
while run:
pygame.time.delay(100)

win.fill((0,0,0))
win.blit(text, playerpos)
pygame.display.update()

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT]:
playerposx -= playervel
print("LEFT")

if keys[pygame.K_RIGHT]:
playerposx += playervel
print("RIGHT")

if keys[pygame.K_UP]:
playerposy -= playervel
print("UP")

if keys[pygame.K_DOWN]:
playerposy += playervel
print("DOWN")

pygame.display.update()

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False


pygame.quit()

最佳答案

你很接近,但你错过了更新 playerpos:

playerpos = (playerposx, playerposy)
win.blit(text, playerpos)

请注意,当按下按钮时,playerposxplayerposy 会发生变化,但 playerpos 永远不会更新。

进一步注意,1 pygame.display.update()足够了。移动延迟,因为 pygame.key.get_pressed()pygame.event.get() 之前调用. pygame.key.get_pressed() 返回的状态在 pygame.event.get() 处理事件时进行评估,因此您可以从前一帧。

像这样改变主循环:

run = True
while run:

pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT]:
playerposx -= playervel
print("LEFT")
if keys[pygame.K_RIGHT]:
playerposx += playervel
print("RIGHT")
if keys[pygame.K_UP]:
playerposy -= playervel
print("UP")
if keys[pygame.K_DOWN]:
playerposy += playervel
print("DOWN")

playerpos = (playerposx, playerposy)

win.fill((0,0,0))
win.blit(text, playerpos)
pygame.display.update()

关于python - 如何在pygame中移动ASCII字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57324880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com