samedi 30 septembre 2023

Why is my sprite not changing its direction after hitting the top? [duplicate]

I'm writing a program in pygame to make 'Pong' for a school project. I'm having issues with getting the ball to bounce back after it gets to the top, even though I'm having no problem with sprite collision or it hitting the bottom of the screen. Only the top of the window is where I'm having a problem

This is my code:

import pygame, sys
from pygame.locals import *
import random

COLOR = (255, 100, 98)
SURFACE_COLOR = (255,255,255)
WIDTH = 1000
HEIGHT = 500

class GameSprite(pygame.sprite.Sprite):
    def __init__(self, player_image, player_x, player_y):
        super().__init__()

        self.image = pygame.transform.scale(pygame.image.load(player_image),(55,100))
        self.rect = self.image.get_rect()
        self.rect.x = player_x
        self.rect.y = player_y

class Player1(GameSprite):
    def update(self):
        keys = pygame.key.get_pressed()
        if keys[K_w]:
            self.rect.y -= 8
        if keys[K_s]:
            self.rect.y += 8
        if self.rect.top <= 0:
            self.rect.top = 0
        if self.rect.bottom >= HEIGHT:
            self.rect.bottom = HEIGHT
        

class Player2(GameSprite):
    def update(self):
        keys = pygame.key.get_pressed()
        if keys[K_UP]:
            self.rect.y -= 8
        if keys[K_DOWN]:
            self.rect.y += 8
        if self.rect.top <= 0:
            self.rect.top = 0
        if self.rect.bottom >= HEIGHT:
            self.rect.bottom = HEIGHT


class Ball(pygame.sprite.Sprite):
    def __init__(self, player_image, player_x, player_y):
        super().__init__()
    
        self.image = pygame.transform.scale(pygame.image.load(player_image),(55,55))
        self.rect = self.image.get_rect()
        self.rect.x = player_x
        self.rect.y = player_y

pygame.init()

RED = (255, 0, 0)

size = (WIDTH, HEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Creating Sprite")
#bg = pygame.image.load('background.jpg').convert()
#bg = pygame.transform.scale(bg,(WIDTH,HEIGHT))
direction = 1
speed_x = random.randint(4,6)
speed_y = random.randint(4,6)

all_sprites_list = pygame.sprite.Group()

player1 = Player1('racket1.png', 0, HEIGHT/2 - 50)
player2 = Player2('racket2.png', WIDTH-50, HEIGHT/2 - 50)
ball = Ball('tennis_ball.png',WIDTH/2,HEIGHT/2 - 50)



all_sprites_list.add(player1)
all_sprites_list.add(player2)
all_sprites_list.add(ball)

exit = True
clock = pygame.time.Clock()

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


    all_sprites_list.update()
    screen.fill(SURFACE_COLOR)
    all_sprites_list.draw(screen)
    pygame.display.flip()
    clock.tick(60)

    if ball.rect.top <= 0:
        speed_y = random.randint(4,6)*-1

    if ball.rect.bottom >= HEIGHT:
        speed_y = random.randint(4,6)*-1

    if pygame.sprite.collide_rect(player1,ball):
        speed_x = random.randint(4,6)*-1

    if pygame.sprite.collide_rect(player2,ball):
        speed_x = random.randint(4,6)*-1
    
    ball.rect.x += speed_x
    ball.rect.y += speed_y

pygame.quit()

Please tell me about what's wrong with this. I'm sorry if this is a poorly worded question




Aucun commentaire:

Enregistrer un commentaire