gpt4 book ai didi

python - 我一直在 pygame 中从事一个项目,但即使没有出现错误,玩家移动似乎也不起作用

转载 作者:行者123 更新时间:2023-12-04 17:24:04 24 4
gpt4 key购买 nike

我需要准备好玩家的 Action ,我已经尝试从其他脚本中获取所需的代码但它似乎没有工作,即使没有出现错误,代码在几次检查后似乎没问题。我使用的库是 sys、pygame 和 glob。我正在遵循本指南,但也改变了它:https://pythonprogramming.altervista.org/platform-game-in-detail-part-1/?doing_wp_cron=1603309265.4902870655059814453125

代码:

from pygame.locals import *
import pygame
import sys
import glob
map1 = """wwwwwwwwwwwwwwwwwwwwwwwwwwwww
w w
w w
w w
w w
w
w d
w p
w
w w
w w
w w
w w
w w
w w
wwwwwwwwwwwwwwwwwwwwwwwwwwwww"""

#-----------------------------

door = pygame.image.load("C:/Users/cuerv/Downloads/Door.png")
door_rect = door.get_rect(center=(100, 250))

tile = pygame.image.load("C:/Users/cuerv/Downloads/Wall.png")
tile_rect = tile.get_rect(center=(100, 256))

player = pygame.image.load("C:/Users/cuerv/Downloads/Player.png")
player_rect = player.get_rect(center=(100, 256))



def init_display():
global screen, tile, door, player
screen = pygame.display.set_mode((480, 250))


def tiles(map1):
global tile, door, player
for y, line in enumerate(map1):
#counts lines
for x, c in enumerate(line):
#counts caracters
if c == "w":
#caracter is w
screen.blit(tile, (x * 16.18, y * 15))
if c == "d":
screen.blit(door, (x * 16.2, y * 15))
if c == "p":
screen.blit(player, (x * 16.2, y * 15))







playerx=200
playery=130




map1 = map1.splitlines()
pygame.init()
init_display()
clock = pygame.time.Clock()
while True:
tiles(map1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

DISPLAYSURF.blit(sprite, (playerx, playery))

if event.type == KEYDOWN:
if (event.key == K_LEFT):
playerx -= 5
elif (event.key == K_RIGHT):
playerx += 5
elif (event.key == K_UP):
playery -= 5
elif (event.key == K_DOWN):
playery += 5






for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()




pygame.display.update()
clock.tick(120)

最佳答案

只是你必须添加一个缩进 block :

while True:
tiles(map1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()

DISPLAYSURF.blit(sprite, (playerx, playery))

if event.type == KEYDOWN:
if (event.key == K_LEFT):
playerx -= 5
elif (event.key == K_RIGHT):
playerx += 5
elif (event.key == K_UP):
playery -= 5
elif (event.key == K_DOWN):
playery += 5

关于python - 我一直在 pygame 中从事一个项目,但即使没有出现错误,玩家移动似乎也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64543413/

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