Python Forum
Can someone help me with collisions ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone help me with collisions ?
#1
So i started with pygame lately and i have problem with collisions.
I wanna created pong and i don´t really know what i do wrong. How i can do the collision of player and ball ?

import pygame
import sys
import math

# initialization
pygame.init()

# screen
x = 600
y = 500
screen = pygame.display.set_mode((x, y))

# icon 32x32
pygame.display.set_caption("Pong")
icon = pygame.image.load("table-tennis.png")
pygame.display.set_icon(icon)

# player 1,2
playerX1 = 0
playerY1 = 200
playerX2 = 585
playerY2 = 200
playerw = 15
playerh = 100
playerchange1 = 0
playerchange2 = 0


def player1(x, y, w, h):
    pygame.draw.rect(screen, (255, 255, 255), [x, y, w, h])


def player2(x, y, w, h):
    pygame.draw.rect(screen, (255, 255, 255), [x, y, w, h])


# ball
ballx = 300
bally = 250
ballt = 15
ball_changex = 0.05
ball_changey = 0.1
ball_touch1 = False
ball_touch2 = False


def ball(x, y, w, h):
    pygame.draw.rect(screen, (255, 255, 255), [x, y, w, h])


# Collision1
def isCollision1(player1x, player1y,playerw, playerh, ballx, bally, ballt):

    if player1x > ballx and player1x < ballx + ballt or player1x+playerw > ballx and player1x + playerw < ballx + ballt:
        if player1y < bally and player1y > bally + ballt:
            return True
        elif player1y + playerh > bally and player1y + playerh < bally + ballt:
            return True
    else:
        return False


# Collision2
#def isCollision2(player2x, player2y, ballx, bally):
#    distance = math.sqrt((math.pow(player2x - ballx, 2)) + (math.pow(player2y - bally, 2)))
#    if distance <= 40:
#        return True
#    else:
#        return False


running = True
while running:
    # background

    screen.fill((80, 125, 0))

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

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                playerchange1 = -0.1
            if event.key == pygame.K_DOWN:
                playerchange1 = 0.1

            # Player 2
            if event.key == pygame.K_w:
                playerchange2 = -0.1
            if event.key == pygame.K_s:
                playerchange2 = 0.1

        if event.type == pygame.KEYUP:
            playerchange1 = 0
            playerchange2 = 0

    # player1:
    playerY1 += playerchange1
    if playerY1 <= 0 or playerY1 >= 400:
        playerY1 -= playerchange1

    player1(playerX1, playerY1, playerw, playerh)

    # player2:
    playerY2 += playerchange2
    if playerY2 <= 0 or playerY2 >= 400:
        playerY2 -= playerchange2

    player2(playerX2, playerY2, playerw, playerh)

    # ball:
    ballx += ball_changex
    bally += ball_changey
    # ball wall touch:
    if ballx >= 585:
        ball_changex = -0.05
    if ballx <= 0:
        ball_changex = +0.05
    if bally <= 0:
        ball_changey = 0.1
    if bally >= 485:
        ball_changey = -0.1
    # ball collision:
    collision1 = isCollision1(playerX1, playerY1, playerw, playerh, ballx, bally, ballt)
    #collision2 = isCollision2(playerX2, playerY2, playerw, playerh, ballx, bally, ballt)

    if collision1:
        ball_changex = 0.05
    #if collision2:
    #   ball_changex = -0.05

    ball(ballx, bally, ballt, ballt)
    pygame.display.update()
Reply
#2
First off, I would suggest using pygame.Rect to store the rect of an object. Next, make classes for the player and the pong, this will make things much easier. Back to rects, using pygame.Rect, you can use a function of it Rect.colliderect. It takes a another rect class as an argument and returns true or false. Now back to classes, I highly suggest making classes for the Player and Pong. This would make it much easier and efficient to work with the player and the pong. Then I would also suggest looking at a more efficient method than the while loop method here.
Reply
#3
(Jun-19-2020, 04:35 PM)SheeppOSU Wrote: First off, I would suggest using pygame.Rect to store the rect of an object. Next, make classes for the player and the pong, this will make things much easier. Back to rects, using pygame.Rect, you can use a function of it Rect.colliderect. It takes a another rect class as an argument and returns true or false. Now back to classes, I highly suggest making classes for the Player and Pong. This would make it much easier and efficient to work with the player and the pong. Then I would also suggest looking at a more efficient method than the while loop method here.
Thank you so much, that was real helpful!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick [PyGame] How to make collisions in isometrick game? Grigory 1 2,311 Jul-01-2021, 01:54 PM
Last Post: Windspar
  Collisions in Arrays Prithak 2 3,059 Mar-19-2021, 03:17 AM
Last Post: Prithak
  Help with collisions. ghost0fkarma 5 6,036 Mar-11-2019, 12:09 AM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020