-2

I need help with my programming assignment. I need to make it so the blocks in the game bounce off of each other when the collide. The code below uses Pygame. I've been trying to do this for a couple hours now and keep running into walls.

import pygame
from pygame.locals import *
import time

class Block:
    def __init__(self,win,left,top,
      width,height,color,velocity):
        self.win = win
        self.rect = pygame.Rect(left,top,
          width,height)
        self.color = color
        self.velocity = velocity
    def move(self):
        self.rect = self.rect.move(
          self.velocity[0],self.velocity[1])
        if ((self.rect.top < 0) or
            (self.rect.bottom > self.win.height)):
              self.velocity[1] = -self.velocity[1]
        if ((self.rect.left < 0) or
            (self.rect.right > self.win.width)):
                self.velocity[0] = -self.velocity[0]
    def tupleRep(block):
        return ((block.rect.left, block.rect.top),(block.rect.right,     block.rect.bottom))
        colliderect()
    def draw(self):
        pygame.draw.rect(self.win.surface,
          self.color, self.rect,0)

class BlockWindow:
    def __init__(self,width,height,caption):
        self.surface = pygame.display.set_mode((width,height))
        self.caption = caption
        pygame.display.set_caption(self.caption)
        self.height = height
        self.width = width
        self.blocks = [ ]
        self.blocks.append(Block(self,300,80,50,
           100,RED,[BASESPEED,-BASESPEED]))
        self.blocks.append(Block(self,200,200,20,
           20,GREEN,[-BASESPEED,-BASESPEED]))
        self.blocks.append(Block(self,100,150,60,
           60,BLUE,[-BASESPEED,BASESPEED]))
        self.blocks.append(Block(self,100,100,70,
            200,PURPLE,[BASESPEED,BASESPEED]))
        self.blocks.append(Block(self,300,70,50,
            60,TEAL,[-BASESPEED,BASESPEED]))
        Quit = False
        while not Quit:
            self.surface.fill(BLACK)
            for b in self.blocks:
                b.move()
                b.draw()
            pygame.display.update()
            time.sleep(0.02)  #import what for this?
            for event in pygame.event.get():
                if event.type == QUIT:
                   Quit = True
# set up the colors
BLACK = (0, 0, 0)
RED   = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE  = (0, 0, 255)
PURPLE= (200,0,200)
TEAL  = (0,200,200)

BASESPEED = 2
# set up pygame
pygame.init()

win = BlockWindow(800,800,'Animation with Objects')

pygame.quit()
Douglas
  • 1,304
  • 10
  • 26
  • It's important to show what you've tried. It shows you've made an effort at solving your problem, and it keeps us from reiterating things you've already tried. – Christian Dean Nov 28 '16 at 23:35
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your code and accurately describe the problem. I don't see anything here that attempts to determine when two blocks collide. – Prune Nov 28 '16 at 23:37
  • pygame has function to check collision between two rects - ie `block1.rect.colliderect(block2.rect)` but it doesn't answer if block collides on left/right or on top/bottom so you don't know if you have to change direction vertically or horizontally. You will have to do own function and check collision between one block and point from other block - you could use `block1.rect.collidepoint(block2.rect.topleft)`, etc. – furas Nov 29 '16 at 00:00
  • see: http://stackoverflow.com/questions/20180594/pygame-collision-by-sides-of-sprite – furas Nov 29 '16 at 00:02

1 Answers1

0

Here is the most I can help you without preventing you from learning anything:

http://www.pygame.org/docs/ref/sprite.html
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_rect
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_circle

Also try the book "Hello World!" by Carter and Warren Sande.

furas
  • 134,197
  • 12
  • 106
  • 148
Douglas
  • 1,304
  • 10
  • 26