我有一个程序它弹跳球我正在使用pygame。我有一个矩形,我可以使用鼠标移动它唯一的问题是当我试图使球弹跳的那个矩形不反弹。它只是停留在那里,球继续循环。我也不能退出那里。我正在使用python 2.7.9。这是我的代码。
import pygame,sys
class MovingBall:
def __init__(self,color,x,y,r,speed):
self.color=color
self.x=x
self.y=y
self.r=r
self.speed=speed
self.dx=1
self.dy=1
def move(self,w,h):
while True:
nx = self.x+self.dx*self.speed; #new x position of the center of the circle
ny = self.y+self.dy*self.speed; #new y position of the center of the circle
if (nx < self.r or nx > w-self.r):
self.dx *= -1
if (ny < self.r or ny > h-self.r):
self.dy *= -1
for event in pygame.event.get():
if event.type == pygame.QUIT:
endProgram = True
break;
#if ( new circle is completely inside the screen )
if ( ( nx >= self.r and nx <= w - self.r ) and ( ny >= self.r and ny <= h-self.r ) ):
self.x=nx; self.y=ny;
break;
print self.dx, self.dy, h-self.r, nx, ny, self.x, self.y
BLACK = ( 0, 0, 0)
BLUE = ( 0, 0, 255)
WHITE = ( 255, 255, 255)
w=800; h=500 #width and height of the screen
pygame.init()
size = (w,h)
screen = pygame.display.set_mode ( size )
all_balls=[\
MovingBall((0, 0, 255), 150, 150, 45, 4), \
# MovingBall((0, 255, 0), 250, 250, 45, 6), \
# MovingBall((255, 255, 0), 250, 350, 45, 5), \
# MovingBall((255, 0, 211), 250, 350, 45, 6), \
# MovingBall((255, 0, 255), 250, 350, 25, 3), \
# MovingBall((255, 255, 0), 250, 350, 40, 4), \
# MovingBall((0,172 ,255), 250, 350, 30, 5), \
# MovingBall((255, 0, 255), 250, 350, 35, 6), \
# MovingBall((255, 0, 0), 250, 350, 45, 7), \
# MovingBall((0, 0, 255), 150, 150, 45, 2), \
# MovingBall((0, 255, 0), 250, 250, 45, 6), \
# MovingBall((255, 255, 0), 250, 350, 45, 7), \
# MovingBall((255, 0, 211), 250, 350, 45, 10), \
# MovingBall((255, 201, 255), 250, 350, 25, 11), \
# MovingBall((255, 255, 0), 250, 350, 40, 8), \
# MovingBall((0,172 ,255), 250, 350, 30, 7), \
# MovingBall((255, 62, 255), 250, 350, 35, 6), \
# MovingBall((255, 35 ,90), 250, 350, 45, 8), \
]
x=0
y=480
#program speed controller
clock = pygame.time.Clock()
endProgram = False
while endProgram==False:
#Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
endProgram = True
#Draw graphics
screen.fill ( WHITE )
for ball in all_balls:
#if ball.limit > 0:
pygame.draw.circle ( screen, ball.color, [ball.x, ball.y], ball.r );
mouse_pos = pygame.mouse.get_pos()
kick=180
rect_x = mouse_pos[0]
if mouse_pos[0] + 180 >= w:
rect_x = w - kick
if y + 18 >= h:
y = h-18
bob = pygame.draw.rect(screen,BLUE,(rect_x, y,kick,18))
if mouse_pos[0] > 620:
mouse_pos *= -1
#print 'rect ', mouse_pos[0], y, kick,18
pygame.display.flip();
for ball in all_balls:
if ball.x > rect_x and ball.x < rect_x + kick:
ball.move(w,480)
else:
ball.move(w,h)
if ball.y > y and ball.y < y + 18:
ball.move(0,y)
#print 'ball ', ball.x, ball.y
#50 frames per seconds
clock.tick (30);
pygame.quit();
这是当我试图将其从那个矩形反弹时会发生什么
答案 0 :(得分:0)
看起来游戏在循环中卡在move
方法中,因为球在球拍内部太深并且无法移出碰撞位置。因此,y速度(self.dy
)保持在1和-1之间切换,球和完整的程序卡住。
您可以检查self.dy
速度是否为负,然后将self.y
或ny
设置为划水板上方的非碰撞位置,例如:
if ny < self.r or ny > h-self.r:
self.dy *= -1
if self.dy < 0:
ny = h-self.r
在我看来,程序可以使用pygame.Rect
进行简化(目前你还没有使用真正的圆圈),而while循环可能是不必要的。