2 Replies - 926 Views - Last Post: 25 July 2011 - 08:00 AM Rate Topic: -----

#1 d0nteatm3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-July 11

2D pygame collision faulty if statement

Posted 24 July 2011 - 07:57 PM

To start I am a begginer programer. I am having issues with collision detection and response. The game is just supposed to replicate the basic game of pong. Under the bumper1collision comment the if statement is where my collision rule is. If the statement is true the the speed is reversed (*-1). The problem is every time I run the game the ball seems to vibrate in place. I've tested around with it and I think what is happening is that the if statment is coming out true every time around in the loop. So the speed is rapidly changing between 150 and -150 (speed was set to 150 to begin with). Its just i have no clue how to fix this.

here is the code.

import pygame, sys, os
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((640, 480))

#Variables
bum1="bumper1.png"
back="bg.jpg"
puck="ball.png"
boun="sideboundry.jpg"
bumper1=pygame.image.load(bum1).convert_alpha()
background=pygame.image.load(back).convert()
pongball=pygame.image.load(puck).convert_alpha()
sideboundry=pygame.image.load(boun).convert()

xcor=41                
ycor=0
clock=pygame.time.Clock()
speed=(150)

#Main Loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
            
    #background
    screen.blit(background, (0,0))
    
    #bumper1   
    x,y = pygame.mouse.get_pos()
    x -= bumper1.get_width()/2
    screen.blit (bumper1, (x,420))
    
    #pongball
    screen.blit (pongball, (xcor,ycor))
    milli=clock.tick()
    seconds=milli/1000.0
    distance=seconds*speed
    xcor+=distance
    ycor+=distance
    

    #sideboundry
    screen.blit (sideboundry, (0,0))
    screen.blit (sideboundry, (600,0))

    #bumper1collision
    pbc=pongball.get_rect()
    b1c=bumper1.get_rect()
    if pbc.colliderect(b1c):
        speed=-1*speed
    
    
    #run
    pygame.display.update()


This post has been edited by d0nteatm3: 24 July 2011 - 07:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: 2D pygame collision faulty if statement

#2 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 161
  • View blog
  • Posts: 247
  • Joined: 19-July 09

Re: 2D pygame collision faulty if statement

Posted 25 July 2011 - 02:20 AM

Problem is in lines 50 and 51
pbc=pongball.get_rect()
b1c=bumper1.get_rect()



get_rect() returns a rectangle with topleft in (0, 0). See pygame's documentation. So your two rects are always colliding.

But you can pass a keyword argument to describe the position of the rect:
pbc=pongball.get_rect(topleft=(xcor, ycor))
b1c=bumper1.get_rect(topleft=(x,420))


Was This Post Helpful? 1
  • +
  • -

#3 d0nteatm3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-July 11

Re: 2D pygame collision faulty if statement

Posted 25 July 2011 - 08:00 AM

Thanks I really apperciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1