【问题标题】:How to create a border in Pygame如何在 Pygame 中创建边框
【发布时间】:2015-10-14 10:55:34
【问题描述】:

我想知道如何在 Pygame 中创建一个边框来阻止用户控制的对象退出屏幕。现在,我只有它,所以当用户控制的对象靠近 4 个边之一时,python 会打印一些文本。

这是我目前的代码。

import pygame
from pygame.locals import *
pygame.init()

#Display Stuff 
screenx = 1000
screeny = 900
screen = pygame.display.set_mode((screenx,screeny))
pygame.display.set_caption('Block Runner')
clock = pygame.time.Clock()
image = pygame.image.load('square.png')




#Color Stuff
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
black = (0,0,0)


#Variables
x_blocky = 50
y_blocky = 750
blocky_y_move = 0
blocky_x_move = 0



#Animations
def Blocky(x_blocky, y_blocky, image):
screen.blit(image,(x_blocky,y_blocky))

#Game Loop

game_over = False
while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                blocky_y_move = -3
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                blocky_y_move = 0

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                blocky_y_move = 3
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN:
                blocky_y_move = 0

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                blocky_x_move = 3
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                blocky_x_move = 0

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                blocky_x_move = -3
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                blocky_x_move = 0

        if x_blocky > 870 or x_blocky < 0:
            print(' X Border')


        if y_blocky > 750 or y_blocky < 2:
            print(' Y Border')



    y_blocky += blocky_y_move
    x_blocky += blocky_x_move


    screen.fill(white)
    Blocky(x_blocky, y_blocky, image)
    pygame.display.update()
clock.tick(60)

【问题讨论】:

    标签: python pygame boundary boundaries


    【解决方案1】:

    好吧,如果您检测到用户对象靠近或位于边界处,就不要进一步增加您的移动变量。或者反转移动方向,这取决于您的总体意图。

        if x_blocky > 870 or x_blocky < 0:
            print(' X Border')
            blocky_x_move = 0
    
    
        if y_blocky > 750 or y_blocky < 2:
            print(' Y Border')
            blocky_y_move = 0
    

    【讨论】:

    • 我正在寻找一个真正的边界,我最终可以让东西从墙上反弹。
    • 那么你的问题是绘图还是碰撞检测?
    【解决方案2】:

    不要使用整数来存储您的位置。使用Rect

    所以不是

    x_blocky = 50
    y_blocky = 750
    

    使用

    blocky_pos = pygame.rect.Rect(50, 750)
    

    现在你可以简单地使用

    blocky_pos.move_ip(blocky_x_move, blocky_y_move)
    

    move你的对象。

    移动后,您只需调用clamp/clamp_ip 即可确保blocky_pos Rect 始终在屏幕内。

    blocky_pos.clamp_ip(screen.get_rect())
    

    另外,您不需要自己定义基本颜色,例如可以简单地使用pygame.color.Color('Red')

    我还建议您使用pygame.key.get_pressed() 获取所有按下的键以查看如何移动您的对象,而不是创建 1000 行事件处理代码。

    【讨论】:

      【解决方案3】:

      此外,您的键盘移动还有一些冗余代码。而不是写

      if event.type == KEYDOWN:
      

      一遍又一遍,将 KEYUP if 语句和 KEYDOWN if 语句分组。

      if event.type == pygame.KEYDOWN:
          if event.key == pygame.K_UP:
              blocky_y_move = -3
          elif event.key == pygame.K_DOWN:
              blocky_y_move = +3
      

      等等,并且:

      if event.type == pygame.KEYUP:
          if event.key == pygame.K_UP:
              blocky_y_move = 0
          elif event.type == pygame.K_DOWN:
              blocky_y_move = 0
      

      【讨论】:

        【解决方案4】:

        您可以使用minmax 函数设置边界。

        这是概念:

        我们有一个 pygame 对象,它可以在所有四个方向上移动;假设用户按住左箭头键,使对象到达屏幕顶部。屏幕顶部的 y 坐标将始终为 0,因此我们希望对象在 y 坐标 0 处停止。

        这看起来很简单:

        if char.rect.y > 0:
            char.rect.y -= char.speed
        

        但这会导致错误 ig char.speed 大于 1。就像对象在 y 坐标5 时一样, 它的速度是10;条件仍然允许对象多一步,导致对象 来自 pygame 窗口的 5 像素。我们想做的更像是:

        if char.rect.y > 0:
            char.rect.y -= char.speed
        if char.rect.y < 0:
            char.rect.y = 0
        

        将对象推回边界。上面的代码块可以用max函数简化:

        self.rect.y = max([self.rect.y - self.speed, 0])
        

        对于向下移动的对象:

        if char.rect.y < HEIGHT - char.height:
            char.rect.y += char.speed
        if char.rect.y > HEIGHT - char.height:
            char.rect.y = HEIGHT - char.height
        

        或者,更有效和更干净的方法:

        self.rect.y = min([self.rect.y + self.speed, HEIGHT - self.height])
        

        要左右移动,只需将上面两行中的ys 和height (和HEIGHT 替换为xs 和widths (和WIDTH)

        大家一起:

        import pygame
        
        pygame.init()
        
        WIDTH = 600
        HEIGHT = 600
        wn = pygame.display.set_mode((WIDTH, HEIGHT))
        
        class Player:
            def __init__(self):
                self.speed = 1
                self.width = 20
                self.height = 20
                self.color = (255, 255, 0)
                self.rect = pygame.Rect((WIDTH - self.width) / 2, (HEIGHT - self.height) / 2, 20, 20)
        
            def up(self):
                self.rect.y = max([self.rect.y - self.speed, 0])
                
            def down(self):
                self.rect.y = min([self.rect.y + self.speed, HEIGHT - self.height])
                
            def left(self):
                self.rect.x = max([self.rect.x - self.speed, 0])
                
            def right(self):
                self.rect.x = min([self.rect.x + self.speed, WIDTH - self.width])
                
            def draw(self):
                pygame.draw.rect(wn, self.color, self.rect)
        
        char = Player()
        
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
            keys = pygame.key.get_pressed()
            if keys[pygame.K_UP]:
                char.up()
            if keys[pygame.K_DOWN]:
                char.down()
            if keys[pygame.K_LEFT]:
                char.left()
            if keys[pygame.K_RIGHT]:
                char.right()
            wn.fill((0, 0, 0))
            char.draw()
            pygame.display.update()
        

        祝你好运!

        【讨论】:

          猜你喜欢
          • 2022-10-25
          • 1970-01-01
          • 2018-06-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多