-1

I've been working on a pygame project, where 2 sprites hit a football around and try to score into the corresponding colour's nets. I'm trying to add a boost aspect, where the cars can press a particular button, and move forward at an increased speed, but ONLY for a specific speed(e.g. 2 seconds). My attempt at it is in Line 177, but the speed doesn't increase to speed 10. There is no error message.My code is below, please can somebody fix it so it is successful?

import pygame
import pygame as pg
from pygame.math import Vector2

pygame.font.init()
pygame.init()


WIDTH = 1150
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rocket League")
clock = pygame.time.Clock()
# Images.
bgImg = pygame.image.load("Football_pitch.png")
REDGOAL = pg.Surface((50, 150), pg.SRCALPHA)
REDGOAL.fill((255, 0, 0))
redgoal_rect = REDGOAL.get_rect(topleft=(0, 340))
redgoal_mask = pg.mask.from_surface(REDGOAL)
BLUEGOAL = pg.Surface((50, 150), pg.SRCALPHA)
BLUEGOAL.fill((0, 0, 255))
bluegoal_rect = REDGOAL.get_rect(topleft=(1100, 340))
bluegoal_mask = pg.mask.from_surface(REDGOAL)

BLUECAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
    BLUECAR_ORIGINAL, (0, 0, 255), [(0, 30), (50, 20), (50,10), (0, 0)])
bluecar = BLUECAR_ORIGINAL
REDCAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
    REDCAR_ORIGINAL, (255, 0, 0), [(0, 0), (50, 10), (50, 20), (0, 30)])
redcar = REDCAR_ORIGINAL

redspeed = 5
bluespeed = 5
ball_x = 575
ball_y = 400
dx = 0
dy = 0
x = 800
y = 500
redscore = 0
bluescore = 0






BALL = pygame.Surface((30, 30), pygame.SRCALPHA)
pygame.draw.circle(BALL, [0,0,0], [15, 15], 15)
# Ball variables.
ball_pos = Vector2(ball_x, ball_y)
ballrect = BALL.get_rect(center=ball_pos)
ball_vel = Vector2(dx, dy)
ball_mask = pg.mask.from_surface(BALL)
# Car variables.
pos_red = Vector2(x,y)
vel_red = Vector2(redspeed,0)
redrect = redcar.get_rect(center=pos_red)
redangle = 180
pos_blue = Vector2(275,300)
vel_blue = Vector2(bluespeed,0)
bluerect = bluecar.get_rect(center=pos_red)
blueangle = 0
# Masks.
mask_blue = pygame.mask.from_surface(bluecar)
mask_red = pygame.mask.from_surface(redcar)
mask_ball = pygame.mask.from_surface(BALL)
ballrect = BALL.get_rect(center=ball_pos)
vel_red.rotate_ip(-180)



def printredscore():
        print(bluescore, "-", redscore)

def printbluescore():
        print(bluescore, "-", redscore)


def redtextscore():
        font = pygame.font.Font(None, 72)
        redscore1 = str(redscore)
        redtext = font.render(redscore1, False, (255,255,255))
        screen.blit(redtext, (650,15))
        pygame.display.flip()

def bluetextscore():
        font1 = pygame.font.Font(None, 72)
        bluescore1 = str(bluescore)
        bluetext = font1.render(bluescore1, False, (255,255,255))
        screen.blit(bluetext, (500,15))
        pygame.display.flip()

def dashtext():
        font2 = pygame.font.Font(None, 72)
        dash = font2.render("-", False, (255,255,255))
        screen.blit(dash, (575,15))
        pygame.display.flip()






run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    redtextscore()
    bluetextscore()
    dashtext()


    if y <0:
        y = 10
    if y > 450:
        y = 440
    if x > 480:
        x = 470

    def redgoal():
        ball_vel.x = 0
        ball_vel.y = 0
        ball_pos.x = 575
        ball_pos.y = 400
        global redscore
        redscore += 1




    def bluegoal():
        ball_vel.x = 0
        ball_vel.y = 0
        ball_pos.x = 575
        ball_pos.y = 400
        global bluescore
        bluescore += 1


    if ballrect.top < 0 and ball_vel.y < 0:
        ball_vel.y *= -1
    elif ballrect.bottom > screen.get_height() and ball_vel.y > 0:
        ball_vel.y *= -1
    if ballrect.left < 0 and ball_vel.x < 0:
        ball_vel.x *= -1
    elif ballrect.right > screen.get_width() and ball_vel.x > 0:
        ball_vel.x *= -1

    if redrect.top < 0 and redrect.y < 0:
        redrect.y *= +10
    elif redrect.bottom > screen.get_height() and redrect.y > 0:
        redrect.y *= -10
    if redrect.left < 0 and redrect.x < 0:
        redrect.x *= -10
    elif redrect.right > screen.get_width() and redrect.x > 0:
        redrect.x *= -10

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        redangle += 5
        vel_red.rotate_ip(-5)
        redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
        redrect = redcar.get_rect(center=redrect.center)
        # We need a new mask after the rotation.
        mask_red = pygame.mask.from_surface(redcar)
    elif keys[pygame.K_RIGHT]:
        redangle -= 5
        vel_red.rotate_ip(5)
        redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
        redrect = redcar.get_rect(center=redrect.center)
        mask_red = pygame.mask.from_surface(redcar)
    elif keys[pygame.K_UP]:
        redspeed == 10



    if keys[pygame.K_a]:
        blueangle += 5
        vel_blue.rotate_ip(-5)
        bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
        bluerect = bluecar.get_rect(center=bluerect.center)
        mask_blue = pygame.mask.from_surface(bluecar)
    elif keys[pygame.K_d]:
        blueangle -= 5
        vel_blue.rotate_ip(5)
        bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
        bluerect = bluecar.get_rect(center=bluerect.center)
        mask_blue = pygame.mask.from_surface(bluecar)


    # Move the cars.
    pos_red += vel_red
    redrect.center = pos_red
    pos_blue += vel_blue
    bluerect.center = pos_blue
    # Move the ball.
    ball_vel *= .97  # Friction.
    ball_pos += ball_vel
    ballrect.center = ball_pos

    # Red car collision.
    # We need the offset between the redrect and the ballrect.
    offset_red = redrect[0] - ballrect[0], redrect[1] - ballrect[1]
    # Pass the offset to the `overlap` method. If the masks collide,
    # overlap will return a single point, otherwise `None`.
    overlap_red = mask_ball.overlap(mask_red, offset_red)
    # Blue car collision.
    offset_blue = bluerect[0] - ballrect[0], bluerect[1] - ballrect[1]
    overlap_blue = mask_ball.overlap(mask_blue, offset_blue)
    offset = redgoal_rect[0] - ballrect[0], redgoal_rect[1] - ballrect[1]
    redgoaloverlap = ball_mask.overlap(redgoal_mask, offset)
    offset = bluegoal_rect[0] - ballrect[0], bluegoal_rect[1] - ballrect[1]
    bluegoaloverlap = ball_mask.overlap(bluegoal_mask, offset)

    if redgoaloverlap:
        redgoal()
        printredscore()
    if bluegoaloverlap:
        bluegoal()
        printbluescore()

    if overlap_red and overlap_blue:  # Both collide with the ball.
        # Not sure what should happen here.
        ball_vel = vel_red + vel_blue * 1.4
    elif overlap_red:  # Red collides with the ball.
        ball_vel = Vector2(vel_red) * 1.4
    elif overlap_blue:  # Blue collides with the ball.
        ball_vel = Vector2(vel_blue) * 1.4




    # Drawing.
    screen.blit(bgImg, (0, 0))
    screen.blit(BALL, ballrect)
    screen.blit(redcar, redrect)
    screen.blit(bluecar, bluerect)
    screen.blit(REDGOAL, redgoal_rect)
    screen.blit(BLUEGOAL, bluegoal_rect)

    pygame.display.flip()
    pygame.display.update()
    clock.tick(60)

pygame.quit()
Hussain Hammad
  • 125
  • 2
  • 14

1 Answers1

1

If you want to increase the velocity, you can just scale the vel_red vector.

elif keys[pygame.K_UP]:
    vel_red.scale_to_length(10)

Changing the redspeed variable has no effect, since the vel_red is the velocity of the red car. Also, you shouldn't use == (equality) but = (assignment) here: redspeed == 10.

To make the effect time based, you can use one of these timers and scale the vector to the original length when the effect should end.

skrx
  • 19,980
  • 5
  • 34
  • 48