pygame乒乓球碰撞格莱西

时间:2019-09-27 18:26:56

标签: pygame

乒乓球拍的运动速度如此之快,以至于在检测到碰撞之前,球就已经在球拍内缠绕起来。问题是用户输入将操纵板移动了一个像素,所以我不知道如何降低操纵板的速度。解决方法是什么?这是代码:

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)

3 个答案:

答案 0 :(得分:0)

一种选择是使用诸如clock.tick(30)之类的来降低fps。我怀疑您需要120 FPS进行乒乓球

但是,如果这样做,您将需要考虑像素间的移动(即使您降低了帧速率,也应该这样做)。这将涉及一些更改,这是我注意到需要更改的内容:

行:

ball=pygame.draw.circle(DISPLAYSURF, GREEN, (int(ballposx),int(ballposy)), ballrad)
        rectposx1=player.rect.x
        rectposy1=player.rect.y

确保将位置[ballposx,ballposy]转换为工程图的整数,并在计算时将其保留为整数。与以下内容相比,这将提供较少的控制权:

ball=pygame.draw.circle(DISPLAYSURF, GREEN, (int(ballposx),int(ballposy)), ballrad)
        rectposx1=ballposx
        rectposy1=ballposy

在这里,我们仍然将其转换为整数以绘制矩形BUT,以便进行计算,我们保留了球位置的更精确值。这样,如果速度为每帧1/2像素,则每2帧移动一次,而不是完全不移动或每帧移动1像素。

答案 1 :(得分:0)

实际上,您的碰撞检测期望球触摸球拍,但是您的球每帧移动5个像素,因此它可以单步跳过球拍的边缘。

这就是我进行碰撞检测的方式:

  • 跟踪当前和上一个球的位置,至少要跟踪垂直位置。
  • 在每帧上,检查球的先前位置是否在桨叶上方,而当前位置是否在桨叶或低于
  • 如果球在桨上,则反射是微不足道的:只需反转垂直速度即可。
  • 如果球以下,则反射不那么琐碎,但仍然很容易:反转垂直速度并将垂直速度加5(或任何垂直单位速度)。 / li>

答案 2 :(得分:0)

有两种策略可以消除这种情况。

  1. 以某种方式移动球,即一旦检测到碰撞,它正在碰触球员但并未与球员相交。例如:

    dx = ballposx - player.rect.centerx
    dy = ballposy - player.rect.centery
    
    if abs(dx) > abs(dy):
        ballposx = player.rect.left-ballrad if dx < 0 else player.rect.right+ballrad
    else:
        ballposy = player.rect.top-ballrad if dy < 0 else player.rect.bottom+ballrad
    
  2. 仅当球的运动矢量指向“反对”球的方向时,才反映球的运动。例如:

    if abs(dx) > abs(dy):
        if (dx < 0 and v[0] > 0) or (dx > 0 and v[0] < 0):
            v.reflect_ip(pygame.math.Vector2(1, 0))
    else:
        if (dy < 0 and v[1] > 0) or (dy > 0 and v[1] < 0):
            v.reflect_ip(pygame.math.Vector2(0, 1))
    

将这2个更正应用于代码时,则球正确地反映在球员身上。例如:

ball = pygame.Rect((0,0), (ballrad*2, ballrad*2))
ball.center = int(ballposx),int(ballposy)
if player.rect.colliderect(ball):
    dx = ballposx - player.rect.centerx
    dy = ballposy - player.rect.centery
    if abs(dx) > abs(dy):
        ballposx = player.rect.left-ballrad if dx < 0 else player.rect.right+ballrad
        if (dx < 0 and v[0] > 0) or (dx > 0 and v[0] < 0):
            v.reflect_ip(pygame.math.Vector2(1, 0))
    else:
        ballposy = player.rect.top-ballrad if dy < 0 else player.rect.bottom+ballrad
        if (dy < 0 and v[1] > 0) or (dy > 0 and v[1] < 0):
            v.reflect_ip(pygame.math.Vector2(0, 1))

如果要避免玩家将球推出窗口,则必须将球限制在窗口区域:

min_x, min_y, max_x, max_y = 0, 0, displaysize, displaysize

ballposx = ballposx + v[0]*dt
ballposy = ballposy + v[1]*dt
if ballposx-ballrad < min_x:
    ballposx = ballrad+min_x
    v[0]=-v[0]
if ballposy-ballrad < min_y:
    ballposy = ballrad+min_y
    v[1]=-v[1]
if ballposx + ballrad > max_x:
    ballposx = max_x-ballrad
    v[0]=-v[0]
if ballposy + ballrad > max_y:
    ballposy = max_y-ballrad
    v[1]=-v[1]

检测到碰撞时,必须限制球员的位置,以便球可以发生在寡妇的边界和球员之间。

if abs(dx) > abs(dy):
    if dx < 0:
        ballposx = max(player.rect.left-ballrad, ballrad+min_x)
        player.rect.left = int(ballposx)+ballrad
    else:
        ballposx = min(player.rect.right+ballrad, max_x-ballrad)
        player.rect.right = int(ballposx)-ballrad

通过这种更改,球甚至可以被“挤压”在窗口边框和玩家之间:

min_x, min_y, max_x, max_y = 0, 0, displaysize, displaysize

ballposx = ballposx + v[0]*dt
ballposy = ballposy + v[1]*dt
if ballposx-ballrad < min_x:
    ballposx = ballrad+min_x
    v[0]=-v[0]
if ballposy-ballrad < min_y:
    ballposy = ballrad+min_y
    v[1]=-v[1]
if ballposx + ballrad > max_x:
    ballposx = max_x-ballrad
    v[0]=-v[0]
if ballposy + ballrad > max_y:
    ballposy = max_y-ballrad
    v[1]=-v[1]

ball = pygame.Rect((0,0), (ballrad*2, ballrad*2))
ball.center = int(ballposx),int(ballposy)
if player.rect.colliderect(ball):
    dx = ballposx - player.rect.centerx
    dy = ballposy - player.rect.centery
    if abs(dx) > abs(dy):
        if dx < 0:
            ballposx = max(player.rect.left-ballrad, ballrad+min_x) 
            player.rect.left = int(ballposx)+ballrad
        else:
            ballposx = min(player.rect.right+ballrad, max_x-ballrad)
            player.rect.right = int(ballposx)-ballrad
        if (dx < 0 and v[0] > 0) or (dx > 0 and v[0] < 0):
            v.reflect_ip(pygame.math.Vector2(1, 0))
    else:
        if dy < 0:
            ballposy = max(player.rect.top-ballrad, ballrad+min_y) 
            player.rect.top = int(ballposy)+ballrad
        else:
            ballposy = min(player.rect.bottom+ballrad, max_y-ballrad)
            player.rect.bottom = int(ballposy)-ballrad
        ballposy = player.rect.top-ballrad if dy < 0 else player.rect.bottom+ballrad
        if (dy < 0 and v[1] > 0) or (dy > 0 and v[1] < 0):
            v.reflect_ip(pygame.math.Vector2(0, 
相关问题