Sam K.
Sam K.

Reputation: 1

Collision without Sprites?

I have been trying to create Pong for a class using python 3.4.3. The last thing I have to do is create the collision between the ball and the paddles. Also, I only learned about Sprites just now, but I have gotten too far to restart.

Here is my code:

#import and start up pygame
import pygame
import math
import time

pygame.init()

#define colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED =   ( 255, 0, 0)
BLUE =  ( 0, 0, 255)
PURPLE = (255, 0, 255)
YELLOW = (255, 255, 0)
TEAL = ( 0, 255, 255)
BROWN = ( 70, 50, 30)

#create window
size = (800, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")

done = False

#manage time
clock = pygame.time.Clock()


#ball movement
rect_x = 375
rect_y = 250
rect_change_x = 3
rect_change_y = 3

#paddle movement
y_speed_1 = 0
y_speed_2 = 0

#paddle coords so we can track them in real time
x_coord_1 = 70
x_coord_2 = 700
y_coord_1 = 200
y_coord_2 = 200

#score
score_player_1 = 0
score_player_2 = 0


time.sleep(1)
#main loop
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        #program that moves the paddles
        if event.type == pygame.KEYDOWN:

        #player one uses the WS keys
        if event.key == pygame.K_w:
            y_speed_1 = -3
        elif event.key == pygame.K_s:
            y_speed_1 = 3

        #player two uses the arrow keys
        elif event.key == pygame.K_DOWN:
            y_speed_2 = 3
        elif event.key == pygame.K_UP:
            y_speed_2 = -3

    #stops the paddle after done pressing key
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_w or event.key == pygame.K_s:
            y_speed_1 = 0

        elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            y_speed_2 = 0


y_coord_1 += y_speed_1
y_coord_2 += y_speed_2


#ball bounces off of the top and bottom sides
if rect_y > 450 or rect_y < 0:
    rect_change_y = rect_change_y * -1

#player 1 score
elif rect_x > 751: 
    score_player_1 += 1
    rect_x = 375
    rect_y = 250
    rect_change_x = 0
    rect_change_y = 0
    rect_change_x = -4
    rect_change_y = -4
    time.sleep(1)


#player 2 score
elif rect_x < -1:
    score_player_2 += 1
    rect_x = 375
    rect_y = 250
    rect_change_x = 0
    rect_change_y = 0
    rect_change_x = 4
    rect_change_y = 4
    time.sleep(1)

#player 1 collision


#player 2 collision


#paddles program
def draw_paddle_1(x, y, z):
    pygame.draw.rect(screen, WHITE, [x_coord_1, y_coord_1, 30, 100])

def draw_paddle_2(x, y, z):
    pygame.draw.rect(screen, WHITE, [x_coord_2, y_coord_2, 30, 100])

#fill the screen
screen.fill(BLACK)

#ball
pygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50, 50])


#move the starting point of the ball
rect_x += rect_change_x
rect_y += rect_change_y

#paddles
draw_paddle_1(screen, x_coord_1, y_coord_1)
draw_paddle_2(screen, x_coord_2, y_coord_2)

#score
def display_score():
    font = pygame.font.SysFont('System Bold', 35, True, False)
    text1 = font.render(str(score_player_1), True, WHITE)
    text2 = font.render(str(score_player_2), True, WHITE)
    screen.blit(text1, [375, 10])
    screen.blit(text2, [425, 10])

def player_win():
    font = pygame.font.SysFont('System Bold', 40, True, False)
    player_1_win = font.render("Player 1 Wins!", True, WHITE)
    player_2_win = font.render("Player 2 Wins!", True, WHITE)

    if score_player_1 == 7:
        screen.blit(player_1_win, [300, 100])
        pygame.display.flip()
        time.sleep(1)
        pygame.quit()

    if score_player_2 == 7:
        screen.blit(player_2_win, [300, 100])
        pygame.display.flip()
        time.sleep(1)
        pygame.quit()

    #call programs
    display_score()
    player_win()

    #make the window display the drawings
    pygame.display.flip()



    #limit to 60 frames per second
    clock.tick(60)


#close window and quit
pygame.quit()

Please also let me know if I should make any other edits. FYI, this needs to be done by tomorrow, so I really cannot spend much more time on this.

Upvotes: 0

Views: 124

Answers (1)

user10030086
user10030086

Reputation:

Hey welcome to stackoverflow! I got your solution, however it's not very good and not a lot of effort, but I will tell you the algorithm so hopefuly you can make a better one than me. Basically all you have to say is if the ball's rectanglar area, is at an intersection with any of the paddles, bounce it (to bounce in this case just reverse the x velocity). To see if it is in intersection is the same way you checked if the ball was inside the screen, you just match the x and y values of the balls and paddles. I made the paddle's thin so that they basically only bounce on the actual paddle(tell me if you want this changed, and I will happily do it!). Heres the code, It was bad indentation but I fixed that, I do recommend you make you while loop mainly functions for readability as it is a bit long.

#import and start up pygame
import pygame
import math
import time

pygame.init()

#define colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED =   ( 255, 0, 0)
BLUE =  ( 0, 0, 255)
PURPLE = (255, 0, 255)
YELLOW = (255, 255, 0)
TEAL = ( 0, 255, 255)
BROWN = ( 70, 50, 30)

#create window
size = (800, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")

done = False

#manage time
clock = pygame.time.Clock()


#ball movement
rect_x = 375
rect_y = 250
rect_change_x = 3
rect_change_y = 3

#paddle movement
y_speed_1 = 0
y_speed_2 = 0

#paddle coords so we can track them in real time
x_coord_1 = 70
x_coord_2 = 700
y_coord_1 = 200
y_coord_2 = 200

#score
score_player_1 = 0
score_player_2 = 0


time.sleep(1)
def display_score():
    font = pygame.font.SysFont('System Bold', 35, True, False)
    text1 = font.render(str(score_player_1), True, WHITE)
    text2 = font.render(str(score_player_2), True, WHITE)
    screen.blit(text1, [375, 10])
    screen.blit(text2, [425, 10])

def player_win():
    font = pygame.font.SysFont('System Bold', 40, True, False)
    player_1_win = font.render("Player 1 Wins!", True, WHITE)
    player_2_win = font.render("Player 2 Wins!", True, WHITE)

    if score_player_1 == 7:
        screen.blit(player_1_win, [300, 100])
        pygame.display.flip()
        time.sleep(1)
        pygame.quit()

    if score_player_2 == 7:
        screen.blit(player_2_win, [300, 100])
        pygame.display.flip()
        time.sleep(1)
        pygame.quit()

def draw_paddle_1(x, y, z):
    pygame.draw.rect(screen, WHITE, [x_coord_1, y_coord_1, 3, 100])

def draw_paddle_2(x, y, z):
    pygame.draw.rect(screen, WHITE, [x_coord_2, y_coord_2, 3, 100])
def collidep1(x,y):
    if x<= x_coord_1+3:#it will only bounce off one side
        if y+50<= y_coord_1+150 and y+50>=y_coord_1:
            return True
    return False
def collidep2(x,y):
    if x+50>= x_coord_2:#it will only bounce off one side
        if y+50<= y_coord_2+150 and y+50>=y_coord_2:
            return True
    return False


#fill the screen

#main loop
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        #program that moves the paddles
        if event.type == pygame.KEYDOWN:

        #player one uses the WS keys
            if event.key == pygame.K_w:
                y_speed_1 = -3
            elif event.key == pygame.K_s:
                y_speed_1 = 3

        #player two uses the arrow keys
            elif event.key == pygame.K_DOWN:
                y_speed_2 = 3
            elif event.key == pygame.K_UP:
                y_speed_2 = -3

    #stops the paddle after done pressing key
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_w or event.key == pygame.K_s:
                y_speed_1 = 0

            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                y_speed_2 = 0


    y_coord_1 += y_speed_1
    y_coord_2 += y_speed_2


#ball bounces off of the top and bottom sides
    if rect_y > 450 or rect_y < 0:
        rect_change_y = rect_change_y * -1

#player 1 score
    elif rect_x > 751: 
        score_player_1 += 1
        rect_x = 375
        rect_y = 250
        rect_change_x = 0
        rect_change_y = 0
        rect_change_x = -4
        rect_change_y = -4
        time.sleep(1)


#player 2 score
    elif rect_x < -1:
        score_player_2 += 1
        rect_x = 375
        rect_y = 250
        rect_change_x = 0
        rect_change_y = 0
        rect_change_x = 4
        rect_change_y = 4
        time.sleep(1)

    #player 1 collision
    if collidep1(rect_x,rect_y):
        rect_change_x = 4
    if collidep2(rect_x,rect_y):
        rect_change_x = -4


    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50, 50])


    #move the starting point of the ball
    rect_x += rect_change_x
    rect_y += rect_change_y

    #paddles
    draw_paddle_1(screen, x_coord_1, y_coord_1)
    draw_paddle_2(screen, x_coord_2, y_coord_2)
    #call programs
    display_score()
    player_win()

    #make the window display the drawings
    pygame.display.flip()



    #limit to 60 frames per second
    clock.tick(60)


#paddles program

#ball

#score
#close window and quit
pygame.quit()

Upvotes: 1

Related Questions