1

我想知道如何在 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)
4

4 回答 4

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
于 2015-07-23T22:36:46.893 回答
1

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

所以而不是

x_blocky = 50
y_blocky = 750

利用

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

现在您可以简单地使用

blocky_pos.move_ip(blocky_x_move, blocky_y_move)

移动的对象。

移动后,您可以简单地调用clamp/clamp_ip以确保blocky_pos Rect始终在屏幕内。

blocky_pos.clamp_ip(screen.get_rect())

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

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

于 2015-07-24T09:35:45.080 回答
0

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

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

ETC

于 2015-10-10T22:19:52.647 回答
0

min您可以使用和max函数设置边界。

这是概念:

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

这看起来很简单:

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

但这会导致一个错误 igchar.speed大于1. 就像当物体在 y 坐标5时,它的速度是10; 该条件仍然允许对象多走一步,导致对象5从 pygame 窗口中出来像素。我们想做的更像是:

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

对于左右移动,只需将上面两行中的 s 和(and )y替换为s 和( and )height HEIGHTxwidths 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()

在此处输入图像描述

祝你好运!

于 2020-11-22T20:22:12.423 回答