1

This is my first time using pygame and I apologize for possible misunderstandings and 'silly' questions. I am developing a breaking game, consisting of a ball ,a paddle and a wall of bricks. So far I ve the ball, and that s fine. I am trying to implement my code in order to get the wall, but I am missing something and it doesn t show up on the screen when I run it. Could anyone help me? The bricks are 50x60, 3 rows. Thank you in advance. (If anyone would also recommend something about creating the paddle and making the ball stick to it, that would be highly appreciated.) Here is my code so far:

  """"BREAKING GAME"""
   #Import and initialize pygame
   import pygame as pg

   pg.init()

   #Set the colour of the background and store the ball data
   backgroundcolour = (50,50,50)
   randomcolour=(205,55,0)
   ballimg = pg.image.load("ball.gif")
   ballimage = ballimg.get_rect()


   #Set the frame
   xmax = 800
   ymax = 800
   screen = pg.display.set_mode((xmax,ymax))

   #Starting values

   horizontalposition = 400.     #pixels (unit f length of computer)
   verticalposition = 400.       #pixels
   v_x = 400.                    #pixels
   v_y = 300.                    #pixels

   #Set the clock of the computer
   t0 = float(pg.time.get_ticks())/1000.


   # Create wall of bricks
   position_Bricks_x=[0,50,100,150,200,250,300,350,400,450,500,550,600,650,700,750]



   position_Bricks_y = [16,32,48]


    i = 0
    j = 0






    #Infinite loop
    running = True
    while running:
        t = float(pg.time.get_ticks())/1000.
        dt = min(t-t0, 0.1)
        t0 = t

             # Motion of the ball
        horizontalposition = horizontalposition+v_x*dt
        verticalposition = verticalposition+v_y*dt

        #Bounce the ball on the edges of the screen
        if horizontalposition > xmax:
            v_x=-abs(v_x)
        elif horizontalposition < 0:
            v_x= abs(v_x)
       if verticalposition > ymax:
            v_y = -abs(v_y)
       elif verticalposition<0:
            v_y = abs(v_y)

     # Draw the frame
       screen.fill(backgroundcolour)
       ballimage.centerx = int(horizontalposition)
       ballimage.centery = int(verticalposition)

       screen.blit(ballimg,ballimage)

       while i < len(position_Bricks_x):
             if j < len(position_Bricks_y):

        pg.draw.rect(screen,randomcolour,[position_Bricks_x[i],position_Bricks_y[j],50,16])
        j = j + 1
    else:
        j=0
        i=i+1
pg.display.update()
pg.display.flip()
# Event handling
pg.event.pump()
for event in pg.event.get():
    if event.type == pg.QUIT:
        running = False

Quit pygame

pg.quit() print "Ready"

  • 2
    Make sure that the code you post is correctly formated. This is especially important for questions involving python, since python's logic is based on indention. Also, creating a [mcve] is a great why on both making the question better for Stackoverflow and simultaneously improving your debugging skill. Stackoverflow isn't a site for debugging question, unless they have a clear question with a specific problem or error, the desired behavior, and a [mcve]. My guess however is that you meant to write `while j < len(position_Bricks_y):` instead of `if j < len(position_Bricks_y):` – Ted Klein Bergman Jun 01 '17 at 09:30

0 Answers0