#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Brick breaker game pygame


    Hello! this is my first post on this forum.
    Right now i am working on a brick breaker type game. i am using python 3.2 with pygame. right now i cannot get the sprites to collide correctly, if someone could help me out that would be great.

    here is the code:

    import pygame
    import random

    # Define some colors
    black = ( 0, 0, 0)
    white = ( 255, 255, 255)
    green = ( 0, 255, 0)
    red = ( 255, 0, 0)

    pygame.init()

    class Ball(pygame.sprite.Sprite):
    x = 0
    y = 0
    x_offset = 1
    y_offset = 2
    size = 0
    color = [255,255,255]
    angle = 0
    def __init__(self, x, y, color, size):
    pygame.sprite.Sprite.__init__(self)
    self.x = x
    self.y = y
    self.color = color
    self.size = size
    self.image = pygame.Surface([20, 20])
    self.image.fill(white)
    pygame.draw.circle(self.image, self.color, (10,10), 10)
    self.rect = self.image.get_rect()
    def update(self, hit_paddle):
    if(self.x <= 0 or self.x >= 490):
    self.x_offset = -self.x_offset
    if(self.y <= 160):
    self.y_offset = -self.y_offset
    if(hit_paddle == True):
    self.y_offset = -self.y_offset
    def draw(self):
    self.x = self.x + self.x_offset
    self.y = self.y + self.y_offset
    pygame.draw.circle(self.image, self.color, (10,10), 10)
    self.rect = self.image.get_rect()


    class Rectangle(pygame.sprite.Sprite):
    width = 0
    height = 0
    x = 10
    y = 10
    def __init__(self,newX,newY,newWidth,newHeight):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.Surface([newWidth, newHeight])
    self.image.fill(black)
    self.x = newX
    self.y = newY
    self.width = newWidth
    self.height = newHeight
    #pygame.draw.rect(screen,white,(self.x,self.y,self.width,self.height),2)
    #pygame.draw.circle(self.image,white,(17, 17), 10)
    self.rect = self.image.get_rect()

    # Set the width and height of the screen [width,height]
    size=[700,500]
    screen=pygame.display.set_mode(size)
    pygame.display.set_caption("Quintens Game")

    all_sprites_list = pygame.sprite.RenderPlain()
    wall_sprites_list = pygame.sprite.RenderPlain()

    # Used to manage how fast the screen updates
    clock=pygame.time.Clock()
    done=False
    for i in range(4):
    for j in range(10):
    block = Rectangle(j*70, i*40, 70, 40)
    block.rect.x = j*70
    block.rect.y = i*40
    all_sprites_list.add(block)
    wall_sprites_list.add(block)

    paddle = Rectangle(350, 470, 50, 15)
    paddle.rect.x = 350
    paddle.rect.y = 470
    all_sprites_list.add(paddle)

    ball = Ball (350, 250, black, 10)
    ball.rect.x = 350
    ball.rect.y = 250
    all_sprites_list.add(ball)

    #Loop until the user clicks the close button.
    # -------- Main Program Loop -----------
    while done==False:


    # ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
    for event in pygame.event.get(): # User did something
    if event.type == pygame.QUIT: # If user clicked close
    done=True # Flag that we are done so we exit this loop


    # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT

    # First, clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.
    screen.fill(white)

    #TODO: add comment
    pos = pygame.mouse.get_pos()
    if(pos[0] > size[0]-50):
    paddle.rect.x = size[0] - 50
    else:
    paddle.rect.x = pos[0]

    ball.rect.y += 10

    #collision with wall removes wall block
    blocks_hit_list = pygame.sprite.spritecollide(ball, wall_sprites_list, True)

    if(pygame.sprite.collide_rect(ball, paddle)):
    ball.update(True)

    ##game over logic
    if(ball.rect.y > 460):
    font = pygame.font.Font(None, 36)
    text = font.render("Game Over", 1, (10,10,10))
    textpos = text.get_rect(centerx = screen.get_width()/2)
    screen.blit(text, [250,250])
    pygame.display.flip()
    pygame.time.wait(2000)
    done = True

    #block.draw(screen)
    all_sprites_list.draw(screen)
    ball.draw()
    # Go ahead and update the screen with what we've drawn.


    # Limit to 20 frames per second
    clock.tick(20)
    pygame.display.flip()
    # Close the window and quit.
    # If you forget this line, the program will 'hang'
    # on exit if running from IDLE.
    pygame.quit ()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    You aren't getting help because:
    1) IndentationError Please read link at my signature about code posting at these forums.
    2) Looks like you've pasted a "fill in the missing algorithm" homework assignment with no original work or thought of your own.
    3) pygame isn't part of the standard distribution.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo