我想知道如何在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)
答案 0 :(得分: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)
到move您的对象。
移动后,您只需拨打clamp
/clamp_ip
即可确保blocky_pos
Rect
始终位于屏幕内。
blocky_pos.clamp_ip(screen.get_rect())
此外,您不需要自己定义基本颜色,例如,您只需使用pygame.color.Color('Red')
。
我还建议您使用pygame.key.get_pressed()
获取所有按下的键,以查看如何移动对象,而不是创建1000行事件处理代码。
答案 1 :(得分:0)
好吧,如果您检测到用户对象位于边界附近或边界处,则根本不要再增加移动变量。或者反转移动方向,具体取决于您的一般意图。
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 :(得分: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
等
答案 3 :(得分:0)
这里是概念:
我们有一个pygame对象,它可以在所有四个方向上移动;假设用户按住向左箭头键,则对象到达屏幕顶部。屏幕顶部的y坐标将始终为0
,因此我们希望对象在y坐标0
处停下来。
这似乎很简单:
if char.rect.y > 0:
char.rect.y -= char.speed
但是这会导致错误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])
要左右移动,只需将上面两行中的y
和height
(和HEIGHT
)替换为x
和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()
祝你好运!