我正在制作一个程序,其中3个方块从我画在屏幕上的几行反弹。当我跑动它时,方块会从线条上反弹几次,但最终它们只是在线条上犁过,然后它们排列在一起并从墙壁上反弹,而不是对线条进行修改。
有一次,当我编码时,蓝色的一个只是拒绝照顾并从边缘反弹,好像没有线条。我相信我已经正确设置了范围,但我不确定发生了什么。
import pygame, sys, time
from pygame.locals import *
pygame.init()
WINDOWWIDTH = 400
WINDOWHEIGHT = 400
window = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('potato')
DOWNLEFT = 1
DOWNRIGHT = 3
UPLEFT = 7
UPRIGHT = 9
MOVESPEED = 1
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
b1 = {'rect':pygame.Rect(0, 50, 25, 25), 'color':RED, 'dir':DOWNRIGHT}
b2 = {'rect':pygame.Rect(0, 100, 25, 25), 'color':GREEN, 'dir':DOWNRIGHT}
b3 = {'rect':pygame.Rect(0, 150, 25, 25), 'color':BLUE, 'dir':DOWNRIGHT}
blocks = [b1, b2, b3]
while True:
# check for the closing of the 'x' button
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
window.fill(BLACK)
pygame.draw.line(window,BLUE,(150,0),(150,130),5)
pygame.draw.line(window,BLUE,(150,300),(150,400),5)
pygame.draw.line(window,BLUE,(200,200),(200,300),5)
pygame.draw.line(window,BLUE,(300,400),(300,250),5)
for b in blocks:
#moves the blocks
if b['dir'] == DOWNLEFT:
b['rect'].left -= MOVESPEED
b['rect'].top += MOVESPEED
if b['dir'] == DOWNRIGHT:
b['rect'].left += MOVESPEED
b['rect'].top += MOVESPEED
if b['dir'] == UPLEFT:
b['rect'].left -= MOVESPEED
b['rect'].top -= MOVESPEED
if b['dir'] == UPRIGHT:
b['rect'].left += MOVESPEED
b['rect'].top -= MOVESPEED
# check if the block has move out of the window
if b['rect'].top < 0:
# block has moved past the top
if b['dir'] == UPLEFT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = DOWNRIGHT
if b['rect'].bottom > WINDOWHEIGHT:
# block has moved past the bottom
if b['dir'] == DOWNLEFT:
b['dir'] = UPLEFT
if b['dir'] == DOWNRIGHT:
b['dir'] = UPRIGHT
if b['rect'].left < 0:
# block has moved past the left side
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['rect'].right > WINDOWWIDTH:
# block has moved past the right side
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
##################CODE FOR THE BOX BOUNCING ON LINES IS BELOW#########
#Upper left
if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
if b['rect'].left == 150 and b['rect'].top > 0 and b['rect'].top < 130:
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
if b['rect'].right == 150 and b['rect'].top < 130 and b['rect'].top>0:
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
#Lower left line
if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
if b['rect'].left == 150 and b['rect'].top > 300 and b['rect'].top < 400:
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
if b['rect'].right == 150 and b['rect'].top > 300 and b['rect'].top < 400:
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
#middle line
if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
if b['rect'].left == 200 and b['rect'].top < 300 and b['rect'].top > 200:
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
if b['rect'].right == 200 and b['rect'].top <300 and b['rect'].top >200:
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
if b['rect'].left == 300 and b['rect'].top < 250 and b['rect'].top > 400:
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
if b['rect'].right == 300 and b['rect'].top < 400 and b['rect'].top >250:
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
pygame.draw.rect(window, b['color'], b['rect'])
pygame.display.update()
#change speed
time.sleep(0.004)
非常感谢帮助!
答案 0 :(得分:1)
问题解决了。
我忘了考虑线条的粗细,所以if语句的坐标如150和250,当它们应该是155和255或类似的东西时。这是:
import pygame, sys, time
from pygame.locals import *
#stuff happens then the bouncing code is below
#Upper
## pygame.draw.line(screen,BLUE,(150,0),(150,130),5)
if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
if (b['rect'].left == 155) and (b['rect'].top > 0) and (b['rect'].top < 130):
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
if (b['rect'].right == 150) and (b['rect'].top < 130) and (b['rect'].top>0):
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
#Lower
## pygame.draw.line(screen,BLUE,(150,300),(150,400),5)
if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
if (b['rect'].left == 155) and (b['rect'].bottom > 300) and (b['rect'].bottom < 400):
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
if (b['rect'].right == 150) and (b['rect'].bottom > 300) and (b['rect'].bottom < 400):
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
# the left line
## pygame.draw.line(screen,BLUE,(200,200),(200,300),5)
if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
if (b['rect'].left == 205) and (b['rect'].top < 300) and (b['rect'].bottom > 200):
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
if (b['rect'].right == 200) and (b['rect'].top <300) and (b['rect'].bottom >200):
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
# the right line
## pygame.draw.line(screen,BLUE,(300,400),(300,250),5)
if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
if (b['rect'].left == 305) and (b['rect'].bottom > 250) and (b['rect'].bottom < 400):
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
if (b['rect'].right == 300) and (b['rect'].bottom < 400) and (b['rect'].bottom > 250):
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
pygame.draw.rect(screen, b['color'], b['rect'])
pygame.display.update()
#change speed
time.sleep(0.05)
感谢@Stick的帮助!
答案 1 :(得分:0)
这方面的技巧是移动对象,然后检查它是否仍在入境中,如果不是,那么你需要通过向右推动并修改其方向来对其进行操作。
如果您打算使用自己的很多代码而不是借用pygame的现有类,那么我是否可以建议简化您首先移动对象的方式?
由于您只使用8个方向,因此您可以考虑将命名空间用作表示八个方向的一系列序列。
UP = [0, -1]
DOWN = [0, 1]
LEFT = [-1, 0]
RIGHT = [1, 0]
对角线有点不同,因为它们不是整数;如果你使用整数,你的对象看起来会移动得更快。如果你回想起毕达哥拉斯定理(或者如果你没有),或者如果你知道单位向量(或者......如果你不知道......)那么你就知道这些最好表示为一半的球场值。 2的平方根。为方便起见,我只是将此值赋给Q
,但这不是优化或任何东西;如果您将sqrt直接插入列表中,Python将无关紧,它们将在游戏开始之前进行整理。
Q = math.sqrt(2) / 2.0
UPLEFT = [-Q, -Q]
UPRIGHT = [Q, -Q]
DOWNLEFT = [-Q, Q]
DOWNRIGHT = [Q, Q]
现在,当您检查对象应该移动的方向时,您不需要进行所有if
次检查。请按以下步骤操作:
for obj in my_group:
# calculate the distance to move the object
delta_x, delta_y = [MOVESPEED * i for i in obj['dir']]
# use pygame.Rect's move_ip() method to move the rect
obj['rect'].move_ip(delta_x, delta_y)
因此,通过将元组中的值乘以对象的移动速度,您基本上可以确定移动对象的“空间”数量。然后,您只需使用rect
移动对象的rect.move_ip()
即可移动它。
移动对象后,您需要确保它是否在入站。你已经绘制了四条不同的线来代表这些;看起来这些也可以组合成另一个rect对象,这样可以更容易地检查对象的邻近度。我们在事件循环开始之前这样做 - 当然 -
Boundary = pygame.Rect(150, 150, 400, 400) # or whatever dimensions you like
现在,您可以在更改方向之前检查对象是否完全包含在边界内。
if not Boundary.contains(obj['rect']):
rect.contains
方法检查一个rect是否完全包含另一个rect。因此,只要移动对象的rect完全位于Boundary rect内,就不需要改变任何东西。但是,如果它走得太远,我们需要开始纠正它的行为。幸运的是,由于我们现在只是处理数字,这就像获得这些数字的负数一样容易。
# if a value is out of bounds, multiply it by -1
if not (Boundary.left < obj['rect'].left or
Boundary.right > obj['rect'].right):
obj['dir'][0] *= -1
if not (Boundary.top < obj['rect'].top or
Boundary.bottom > obj['rect'].bottom):
obj['dir'][1] *= -1
此时,对象仍然可以手动回到原位,但如果我们按照正确的顺序进行检查,我们可能不需要。通过这种方式,只需设置正确的方向即可自动处理重定向,然后允许事件循环回转并自然地将对象推入正确的位置。 (这并不总是有效,这取决于事情的发展方式,所以如果一个对象需要围绕这个if
语句略微改变,但现在它并不完全相关。)
此时,我们应该绘制整个系列的更新而不是一次一个,就像你的循环似乎那样。在pygame中分离绘制调用是导致性能不佳的好方法。既然你还没有使用Sprite Groups(你应该......:D),那么在完成所有操作之后你就会做类似下面的事情:
for obj in my_group:
pygame.draw.rect(window, obj['rect'], some_color)
pygame.display.flip()
最后 - 在完成所有这些之后,请查看另一个有用的对象来处理您的帧速率,而不是time.sleep
; pygame.time.Clock
对象。它在游戏开始前被实例化:
MyClock = pygame.time.Clock()
FPS = 30 # or whatever framerate you're going for
...在绘制调用之后,通常只需调用Clock.tick()
来正确提高帧速率。这个对象比普通调用time.sleep
更聪明,并且会尝试让你的游戏保持不变的FPS,所以使用它会很好。
MyClock.tick(FPS)
理想情况下,我们会做类似的事情:
for event in pygame.event.get():
process_event(event) # however it is you plan on handling events,
# that would go here
for obj in my_group:
# calculate the distance to move the object
delta_x, delta_y = [MOVESPEED * i for i in obj['dir']]
# use pygame.Rect's move_ip() method to move the rect
obj['rect'].move_ip(delta_x, delta_y)
# check if the object is outside the boundaries
if not Boundary.contains(obj['rect']):
# if a value is out of bounds, multiply it by -1
if not (Boundary.left < obj['rect'].left or
Boundary.right > obj['rect'].right):
obj['dir'][0] *= -1
if not (Boundary.top < obj['rect'].top or
Boundary.bottom > obj['rect'].bottom):
obj['dir'][1] *= -1
for obj in my_group:
pygame.draw.rect(window, obj['rect'], some_color)
pygame.display.flip()
MyClock.tick(FPS)