繁体   English   中英

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

[英]How do I restart my python game, or just reset where the player is?

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

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

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

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

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()

`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM