如何重新启动python游戏,或者只是重置玩家所在的位置?

如何重新启动python游戏,或者只是重置玩家所在的位置?,python,pygame,Python,Pygame,我想在玩家触碰墙壁时重新启动玩家所在的位置,但在此之前,我想给他们一个警告,然后重置位置。不过,我也做不到。我最近问了一个类似的问题,虽然我得到了一个非常好和有用的答案,但它不起作用。无论如何谢谢你,拉比。这是我的代码: def touch_walls(player_pos): if x-20 < 0: #give warning not to touch sides #reset player position elif x+20 > 800:

我想在玩家触碰墙壁时重新启动玩家所在的位置,但在此之前,我想给他们一个警告,然后重置位置。不过,我也做不到。我最近问了一个类似的问题,虽然我得到了一个非常好和有用的答案,但它不起作用。无论如何谢谢你,拉比。这是我的代码:

def touch_walls(player_pos):

  if x-20 < 0:
    #give warning not to touch sides
    #reset player position

  elif x+20 > 800:
    #give warning not to touch sides
    #reset player position
def触摸墙(播放器位置):
如果x-20<0:
#警告不要触摸侧面
#重置球员位置
elif x+20>800:
#警告不要触摸侧面
#重置球员位置

如果有人找到解决方案,请告诉我。

您可以调用函数来绘制“请勿触摸墙壁”文本。 `

def触摸墙(播放器位置):
全球玩家,blit_font_时间
x=玩家位置[0]
如果x-20<0或x+20>800:
#使文本可变
#重置球员位置
playerPos=(50,0)
blit_font_time=60#将此值设置为您希望的长度
def blit_font():
text=pygame.font.SysFont('Arial',50)
警告\u label=text.render('Don't Touch Walls',True,(255,255,255))#您可以根据需要修改文本或颜色
中心=(screen.get_width()/2-警告标签。get_width()/2,screen.get_height()/2-警告标签。get_height()/2)
screen.blit(警告标签,中间)#blit屏幕中央的文本
`

整个事情看起来是这样的: `

导入pygame
pygame.init()
playerPos=(-60,50)#假设playerPos是一个包含玩家位置的变量
screen=pygame.display.set_模式((500500))
clock=pygame.time.clock()
blit\u font\u time=0
def触摸屏(播放器位置):
全球玩家,blit_font_时间
x=玩家位置[0]
如果x-20<0或x+20>800:
#使文本可变
#重置球员位置
playerPos=(50,0)
blit_font_time=60#将此值设置为您希望的长度
def blit_font():
text=pygame.font.SysFont('Arial',50)
警告\u label=text.render('Don't Touch Walls',True,(255,255,255))#您可以根据需要修改文本或颜色
中心=(screen.get_width()/2-警告标签。get_width()/2,screen.get_height()/2-警告标签。get_height()/2)
screen.blit(警告标签,中间)#blit屏幕中央的文本
运行=真
运行时:
时钟滴答(60)
对于pygame.event.get()中的e:
如果e.type==pygame.QUIT:
运行=错误
屏幕填充((20,20,20))
触摸墙(播放者)
如果blit\u font\u time>0:
blit\u font\u time-=1
blit_font()
pygame.display.update()
pygame.quit()

`

您在寻找哪种错误?它是控制台输出还是其他什么?我不知道,我可以打印一些东西,我更感兴趣的是如何让代码工作。看起来你想让我们为你写一些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只在海报已经试图自己解决问题时才提供帮助。演示这项工作的一个好方法是包括您迄今为止编写的代码、示例输入(如果有)、预期输出和实际获得的输出(控制台输出、回溯等)。你提供的细节越多,你可能收到的答案就越多。你需要重置每一个游戏状态。没有神奇的召唤来重新启动游戏。编写一个初始化游戏状态的函数。在启动时和需要重新启动游戏时调用该函数。
def touch_walls(player_pos):

    global playerPos, blit_font_time

    x = player_pos[0]

    if x-20 < 0 or x+20 > 800:
        #make a text varible
        #reset player position
        playerPos = (50, 0)
        blit_font_time = 60 # set this to as long as you wish

def blit_font():
    text = pygame.font.SysFont('Arial', 50)
    warning_label = text.render('Don\'t Touch Walls', True, (255, 255, 255)) #You can modify the text or the color anyway you want
    center = (screen.get_width()/2 - warning_label.get_width()/2, screen.get_height()/2 - warning_label.get_height()/2)
    screen.blit(warning_label, center) #Blit the text at the center of the screen
import pygame

pygame.init()

playerPos = (-60, 50) # Assume playerPos is a varible that contains the player's position

screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

blit_font_time = 0

def touch_walls(player_pos):

    global playerPos, blit_font_time

    x = player_pos[0]

    if x-20 < 0 or x+20 > 800:
        #make a text varible
        #reset player position
        playerPos = (50, 0)
        blit_font_time = 60 # set this to as long as you wish

def blit_font():
    text = pygame.font.SysFont('Arial', 50)
    warning_label = text.render('Don\'t Touch Walls', True, (255, 255, 255)) #You can modify the text or the color anyway you want
    center = (screen.get_width()/2 - warning_label.get_width()/2, screen.get_height()/2 - warning_label.get_height()/2)
    screen.blit(warning_label, center) #Blit the text at the center of the screen

run = True

while run:
    clock.tick(60)
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            run = False

    screen.fill((20, 20, 20))

    touch_walls(playerPos)

    if blit_font_time > 0:
        blit_font_time -= 1
        blit_font()
    pygame.display.update()

pygame.quit()