乒乓球拍移动得如此之快,以至于在检测到碰撞之前,球会在球拍内卷起。问题是用户的输入移动了一个像素,所以我不知道如何减慢速度。解决办法是什么?代码如下:
import pygame, sys, os
from pygame import*
from pygame.locals import*
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (0, 0, 128)
RED = (255,0,0)
os.environ["SDL_VIDEO_CENTERED"]="1"
displaysize=600
DISPLAYSURF = pygame.display.set_mode((displaysize,displaysize))
rectwidth = 50
rectheight= 50
rectposx =0
rectposy =0
class Player(object):
def __init__(self):
self.rect = pygame.rect.Rect((rectposx, rectposy, rectwidth, rectheight))
def handle_keys(self):
key = pygame.key.get_pressed()
dist = 1
if key[pygame.K_LEFT] and (player.rect.x>0):
self.rect.move_ip(-1, 0)
if key[pygame.K_RIGHT] and (player.rect.x<600-rectwidth):
self.rect.move_ip(1, 0)
if key[pygame.K_UP] and (player.rect.y>0):
self.rect.move_ip(0, -1)
if key[pygame.K_DOWN] and (player.rect.y<600-rectheight):
self.rect.move_ip(0, 1)
def draw(self, DISPLAYSURF):
pygame.draw.rect(DISPLAYSURF, BLUE, self.rect)
def postext(self):
pygame.image.load(self.rect).convert_alpha()
pygame.init()
player =Player()
pygame.display.set_caption('Hello World!')
clock=pygame.time.Clock()
fontObj = pygame.font.Font(None,32)
textSurfaceObj = fontObj.render('Hello World!', True, GREEN, BLUE)
#textPosition =
dt=0.1
v = pygame.math.Vector2(5,5)
ballposx=200
ballposy=200
ballrad=10
#DISPLAYSURF.fill(WHITE)
#x=10
#y=10
#dx=5
#rectpos = pygame.Rect(x,y,50,50)
#rect = pygame.draw.rect(DISPLAYSURF, BLUE, rectpos)
pygame.display.update()
running = True
n=0
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
if event.type==QUIT:
pygame.quit()
sys.exit
player.handle_keys()
ballposx=ballposx+v[0]*dt
ballposy=ballposy+v[1]*dt
DISPLAYSURF.fill(WHITE)
DISPLAYSURF.blit(textSurfaceObj,(0,0))
player.draw(DISPLAYSURF)
ball=pygame.draw.circle(DISPLAYSURF, GREEN, (int(ballposx),int(ballposy)), ballrad)
rectposx1=player.rect.x
rectposy1=player.rect.y
rectvelx=-(rectposx-rectposx1)/dt
rectvely=-(rectposy-rectposy1)/dt
if ballposx-ballrad<0:
v[0]=-v[0]
if ballposy-ballrad<0:
v[1]=-v[1]
if ballposx+ballrad>600:
v[0]=-v[0]
if ballposy+ballrad>600:
v[1]=-v[1]
if player.rect.colliderect(ball):
pygame.math.Vector2.reflect_ip(v,-v+5*pygame.math.Vector2(rectvelx,rectvely))
#print (player.rect.x, rectposy, ball.x, ball.y)
ballmass=1
rectmass=5
rectposx=rectposx1
rectposy=rectposy1
print (v)
#raise SystemExit("You win!")
pygame.display.update()
clock.tick(120)
有两种策略可以消除这种空虚感
在检测到碰撞后,将球移动到与球员接触但不与球员相交的位置。e、 g:
仅当球的运动矢量指向球的“反”方向时,才反映球的运动。e、 g:
另见Sometimes the ball doesn't bounce off the paddle in pong game
将这两个修正应用到代码中,球将正确地反映在球员身上。e、 g:
如果要避免球员将球推出窗外,需要将球限制在窗口区域,并将球像台球一样反射到窗口边缘:
另见Use vector2 in pygame. Collide with the window frame and restrict the ball to the rectangular area分别How to make ball bounce off wall with Pygame?
当检测到碰撞时,必须限制球员的位置,以便球可以在寡妇边界和球员之间进行:
通过这些更改,球甚至可以“挤压”在窗口边缘和球员之间:
最简单的例子:
事实上,碰撞检测期望球接触挡板,但你的球每帧移动5个像素,因此它可以在一次移动中跳过挡板的边缘
这就是我进行碰撞检测的方法:
一种选择是使用如下内容减少fps:
clock.tick(30)
。我怀疑你打乒乓球需要120 FPS但是如果你这样做了,你需要考虑像素间的移动(即使你降低了帧率,你也应该这样做)。这将涉及一些变化,以下是我注意到需要改变的:
台词:
确保将位置
[ballposx,ballposy]
转换为图形的整数,并将其保留为计算的整数。这将提供比以下更少的控制:在这里,我们仍然使用整数绘制矩形,但在计算中,我们保留了球位置的更精确值。这样,如果速度为每帧1/2像素,则每2帧移动一次,而不是完全不移动或每帧移动1像素
相关问题 更多 >
编程相关推荐