繁体   English   中英

如何停止pygame中的角色离开屏幕边缘?

[英]How can I stop a character in pygame from leaving the edge of the screen?

我在pygame中创建了一个小程序,玩家在其中控制着一个蓝色正方形在屏幕上移动,但是我想阻止玩家移过屏幕的边缘。 这是我到目前为止的代码,我该怎么做?

import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))
done = False
x = 30
y = 30

clock = pygame.time.Clock()

while not done:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True
                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                        is_blue = not is_blue


    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]: y -= 5
    if pressed[pygame.K_DOWN]: y += 5
    if pressed[pygame.K_LEFT]: x -= 5
    if pressed[pygame.K_RIGHT]: x += 5

    screen.fill((0, 0, 0))
    color = (0, 128, 255)
    pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))

    pygame.display.flip()
    clock.tick(60)

pygame.Rect ■找一个clamp (和clamp_ip ,你可以用它来限制移动区域)的方法。 因此,创建一个具有屏幕大小的rect(在此称为screen_rect )和一个供玩家使用的rect( player_rect ),并在每次移动后调用clamp_ip方法以将其保持在屏幕区域内。

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
BG_COLOR = pg.Color(30, 30, 50)


def main():
    clock = pg.time.Clock()
    image = pg.Surface((50, 30))
    image.fill(pg.Color('dodgerblue'))
    pg.draw.rect(image, pg.Color(40, 220, 190), (0, 0, 49, 29), 2)
    player_rect = image.get_rect(topleft=(200, 200))
    # This pygame.Rect has the dimensions of the screen and
    # is used to clamp the player_rect to this area.
    screen_rect = screen.get_rect()
    speed = 5

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                return

        pressed = pg.key.get_pressed()
        if pressed[pg.K_UP]:
            player_rect.y -= speed
        if pressed[pg.K_DOWN]:
            player_rect.y += speed
        if pressed[pg.K_LEFT]:
            player_rect.x -= speed
        if pressed[pg.K_RIGHT]:
            player_rect.x += speed
        # Clamp the rect to the dimensions of the screen_rect.
        player_rect.clamp_ip(screen_rect)

        screen.fill(BG_COLOR)
        screen.blit(image, player_rect)

        pg.display.flip()
        clock.tick(60)


if __name__ == '__main__':
    main()
    pg.quit()

这应该工作

if pressed[pygame.K_UP] and y > 0: y -= 5
if pressed[pygame.K_DOWN] and y < 600 - 60: y += 5
if pressed[pygame.K_LEFT] and x > 0: x -= 5
if pressed[pygame.K_RIGHT] and x < 800 - 60: x += 5

屏幕尺寸为600和800,矩形尺寸为60

就像是:

if pressed[pygame.K_UP]:
    if not (y > maxwidth or y < 0):
        y += 5

以此类推。 maxwidth在您的代码中看起来像600,但是我将其放在代码的顶部,因此您不必继续在其他位置进行更改。

您尝试做的事情称为边缘检测,就像检测您是否在边缘一样,在这种情况下,您希望边缘成为屏幕的边缘。 您应该做的是检查xy是否在边缘,如果这样,就不要再走了。

if pressed[pygame.K_UP]: 
    if 0 < y-5 < 600:  #0 and 600 being the edges of your screen, you can use a variable to change it dynamically later one  
        y -= 5

请注意,这只会检测左上角的正方形是否超出范围,因为xy是矩形的上下坐标,这意味着正方形的右下角仍将超出范围,

如果要检查整个平方,则必须在if语句中进行调整计算,或者将xy放在中心(您仍然必须将if语句修改为如下所示。) m根据您当前的代码更改( xy位于左上方)。

if pressed[pygame.K_UP]: 
    if (0 < y-5 < 600) or (0< y+60-5 <600)  #0 and 600 being the edges of your screen, you can use a variable to change it dynamically later one  
        y -= 5

这将检查正方形的另一侧。 注意x您将检查水平极限,在这种情况下为800 另外,我们正在检查是否包含-5因为我们想知道我们要去的地方,而不是我们要去的地方。

暂无
暂无

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

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