物体反弹和运动

时间:2018-09-08 13:14:11

标签: python pygame

我一直在做一场比赛,两个人试图在球网上进球,但是要这样做,球必须移动并弹跳。我希望球以现实的角度向上和向上移动,然后在地面上反弹几次,然后再次变得静止。我不确定如何实现此目标,我的代码在下面,我真的希望有人可以对其进行编辑以使其工作。

运行代码时,出现以下错误

  

TypeError:+ =:'pygame.math.Vector2'和'int'(对于“ ball_vel + = x2”行)不支持的操作数类型

import pygame as pg
from pygame.math import Vector2
pg.init()
LIGHTBLUE = pg.Color('lightskyblue2')
screen = pg.display.set_mode((800, 600))
width, height = screen.get_size()
clock = pg.time.Clock()
pg.display.set_caption("Super Acrobatic Rocket Powered Battle Polygons(SARPBP)")
x = 740
y = 0
x2 = 370
y2 = 500
x_change = 0
y_change = 0
bluespeed = 5
on_ground = False
bluecar = pg.Surface((60, 30))
bluecar.fill((0,0,255))
BALL = pg.Surface((30, 30), pg.SRCALPHA)
pg.draw.circle(BALL, [0,0,0], [15, 15], 15)
ball_pos = Vector2(x2, y2)
ballrect = BALL.get_rect(center=ball_pos)
ball_vel = Vector2(0, 0)
ball_mask = pg.mask.from_surface(BALL)
mask_blue = pg.mask.from_surface(bluecar)
mask_ball = pg.mask.from_surface(BALL)

pos_blue = Vector2(x,y)
bluerect = bluecar.get_rect(center = pos_blue)
vel_blue = Vector2(bluespeed,0)



# A constant value that you add to the y_change each frame.
GRAVITY = .5
done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_a:
                x_change = -5
            elif event.key == pg.K_d:
                x_change = 5
            elif event.key == pg.K_w:
                if on_ground:  # Only jump if the player is on_ground.
                    y_change = -12
                    on_ground = False
        elif event.type == pg.KEYUP:
            if event.key == pg.K_a and x_change < 0:
                x_change = 0
            elif event.key == pg.K_d and x_change > 0:
                x_change = 0

    # Add the GRAVITY value to y_change, so that
    # the object moves faster each frame.
    y_change += GRAVITY
    x += x_change
    y += y_change
    #x += vel_blue
    # Stop the object when it's near the bottom of the screen.
    if y >= height - 100:
        y = height - 100
        y_change = 0
        on_ground = True

    if x == 0:
        x = 5
    elif x == 740:
        x = 735

    offset_blue = bluerect[0] - ballrect[0], bluerect[1] - ballrect[1]
    overlap_blue = mask_ball.overlap(mask_blue, offset_blue)
    if overlap_blue:  # Blue collides with the ball.
        ball_vel = Vector2(x_change) * 1.4
    ball_vel += x2

    # Draw everything.
    screen.fill(LIGHTBLUE)
    pg.draw.line(screen, (0, 0, 0), (0, height-70), (width, height-70))
    screen.blit(bluecar, (x, y))
    screen.blit(BALL, (x2,y2))

    pg.display.update()
    clock.tick(60)

pg.quit()

2 个答案:

答案 0 :(得分:1)

该错误表明您无法直接将intVector2一起添加。该程序不知道如何。相反,您需要添加到向量的x值中。将行ball_vel += x2更改为ball_vel.x += x2

答案 1 :(得分:1)

当球接触地面使其弹跳时,您只需反转速度矢量的y分量:ball_vel.y *= -1。更改乘以速度以调整弹性的值:ball_vel.y *= -0.94 # Lower elasticity

import pygame as pg
from pygame.math import Vector2


pg.init()
LIGHTBLUE = pg.Color('lightskyblue2')
screen = pg.display.set_mode((800, 600))
width, height = screen.get_size()
clock = pg.time.Clock()
BALL = pg.Surface((30, 30), pg.SRCALPHA)
pg.draw.circle(BALL, [0,0,0], [15, 15], 15)
ball_pos = Vector2(370, 0)
ballrect = BALL.get_rect(center=ball_pos)
ball_vel = Vector2(0, 0)
ground_pos = 70
GRAVITY = .9

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    ball_vel.y += GRAVITY  # Accelerate downwards.
    ball_pos += ball_vel  # Move the ball.
    ballrect.center = ball_pos
    # Bounce when the ball touches the bottom of the screen.
    if ballrect.bottom >= height - ground_pos:
        # Just invert the y-velocity to bounce.
        ball_vel.y *= -0.94  # Change this value to adjust the elasticity.
        # Don't go below the ground.
        ballrect.bottom = height - ground_pos
        ball_pos.y = ballrect.centery

    # Draw everything.
    screen.fill(LIGHTBLUE)
    pg.draw.line(screen, (0, 0, 0), (0, height-70), (width, height-ground_pos))
    screen.blit(BALL, ballrect)

    pg.display.update()
    clock.tick(60)

pg.quit()