简体   繁体   English

如何在 pygame 中为全屏模式添加边界

[英]How can I add boundaries to fullscreen mode in pygame

This is my code:这是我的代码:

import pygame
    

pygame.init()


window_size=(800,600)
screen=pygame.display.set_mode(window_size)

player_X=200
player_Y=300
player_X_change=0
player_Y_change=0


running=True
while running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False


        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_LEFT:
                player_X_change=-1
            if event.key==pygame.K_RIGHT:
                player_X_change=1
            if event.key==pygame.K_UP:
                player_Y_change=-1
            if event.key==pygame.K_DOWN:
                player_Y_change=1
        if event.type==pygame.KEYUP:
            if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
                player_X_change=0
            if event.key==pygame.K_UP or event.key==pygame.K_DOWN:
                player_Y_change=0


    screen.fill((0,200,255))

    player_X+=player_X_change
    player_Y+=player_Y_change
    player = pygame.draw.rect(screen, (255, 0, 0), (player_X, player_Y, 40, 50))
    if player_X<=-1:
        player_X=0
    if player_X>=761:
        player_X=760
    if player_Y<=-1:
        player_Y=0
    if player_Y>=551:
        player_Y=550
    
    
        pygame.display.update()

Now I want to add the boundaries to fullscreen mode.现在我想将边界添加到全屏模式。 I know that I can do fullscreen by:我知道我可以通过以下方式进行全屏:

screen=pygame.display.set_mode(window_size,pygame.RESIZABLE,32).

Well, now I have two questions.好吧,现在我有两个问题。

  • First, and the main one, How can I make the boundaries while in the maximized form.首先,也是主要的,我怎样才能在最大化的形式下建立边界。
  • Second, what is the use of 32 (the 3rd argument I've given in the set_mode function)?其次,32(我在 set_mode 函数中给出的第三个参数)有什么用?

I used it as I saw it in a tutorial.我使用它,因为我在教程中看到它。 When I remove it, it doesn't do anything different but if it does then what is it/what it would be?当我删除它时,它没有做任何不同的事情,但如果它做了,那么它是什么/它会是什么? Well, back to the main question, when I try to go to the x axis more than 800 or the y axis more than 600 in the fullscreen mode,(which i got by resizing) i am not able to go further as the boundary ive added is for 800 by 600 window.好吧,回到主要问题,当我尝试在全屏模式下将 go 到 x 轴超过 800 或 y 轴超过 600 时(我通过调整大小得到),我无法将 go 进一步作为边界添加的是 800 x 600 window。

The screen is a pygame.Surface object. screenpygame.Surface 。表面是object。 Use get_size() to get the current size of the window:使用get_size()获取 window 的当前大小:

running=True
while running:
    # [...]

    player_X += player_X_change
    player_Y += player_Y_change
    
    width, height = screen.get_size()
    if player_X < 0:
        player_X = 0
    if player_X > width - 40:
        player_X = width - 40
    if player_Y < 0:
        player_Y = 0
    if player_Y > height - 50:
        player_Y = height - 50    

    player = pygame.draw.rect(screen, (255, 0, 0), (player_X, player_Y, 40, 50))

The 3rd argument of pygame.display.set_mode() is the number of bits to use for the color. pygame.display.set_mode()的第三个参数是用于颜色的位数。 You don't need to specify it at all.您根本不需要指定它。 See the documentation of pygame.display.set_mode() :请参阅pygame.display.set_mode()的文档:

It is usually best to not pass the depth argument.通常最好不要传递深度参数。 It will default to the best and fastest color depth for the system.它将默认为系统的最佳和最快的颜色深度。 If your game requires a specific color format you can control the depth with this argument.如果您的游戏需要特定的颜色格式,您可以使用此参数控制深度。 Pygame will emulate an unavailable color depth which can be slow. Pygame 将模拟可能很慢的不可用颜色深度。

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

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