Hi, I am trying to make a brick breaker game, but I am stuck. i need help moving the panel, and getting the ball to bounce off of bricks upon collision and erase the bricks.
Also I wanted to create two lists, one that contains coordinates, the second would be an empty list, which would read the coordinates, and then create a brick, then add it to that empty list, I am just unsure on how to do this.
here is my outline code

from visual import*

scene.title = 'brick'
scene.range=10

brick1=box(pos=(-5,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick2=box(pos=(-4,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick3=box(pos=(-3,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick4=box(pos=(-2,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick5=box(pos=(-1,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick6=box(pos=(0,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick7=box(pos=(1,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick8=box(pos=(2,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick9=box(pos=(3,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick10=box(pos=(4,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick11=box(pos=(5,9,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick12=box(pos=(-4,8,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick13=box(pos=(-3,8,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick14=box(pos=(-2,8,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick15=box(pos=(-1,8,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick16=box(pos=(0,8,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick17=box(pos=(1,8,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick18=box(pos=(2,8,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick19=box(pos=(3,8,0), length=1, height=1, width=1, color=color.white, material=materials.marble)
brick20=box(pos=(4,8,0), length=1, height=1, width=1, color=color.white, material=materials.marble)




ball=sphere(color=color.cyan, pos=vector(0,0,0), radius=0.2)

ball.velocity = vector(0,-1,0)

delta_t= 0.01

panel=box(pos=(0,-9,0), length=2, height=0.5, width=0, color=color.white)
panel.velocity = vector(1,0,0)
while True:
    rate(300)
    ball.pos= ball.pos + ball.velocity*delta_t

    if ball.y<10:
        ball.velocity.y=-ball.velocity.y
    if ball.y >= panel.y +.5 and ball.x<= panel.x+1 and ball.x >= panel.x-1 :
        ball.velocity.y= -ball.velocity.y


    
    if scene.kb.keys:
        s=scene.kb.getkey()
        if s == 'right':
            panel.pos.x = panel.pos.x + 0.2
        if s == 'left':
            panel.pos.x = panel.pos.x - 0.2
    if ball.y >= 7 and ball.x >.5 and ball.x <.5:
        ball.velocity.y=-ball.velocity.y
     
    if ball.y <= -10:
        break
        print "Game Over"

Recommended Answers

All 8 Replies

Well for starters, lines 6 through 25 make me itchy. This is what I would do (assuming you really want a single dimension array of boxes)

bricks = []
for i in range(-5,6):
  bricks.append(box((i,9,0),1,1,'white',materials.marble))
for i in range(-4,5):
  bricks.append(box((i,8,0),1,1,'white',materials.marble))

That seems to answer your first question.

On the other hand, I think you may actually want to have an array of bricks whose coordinates are associated with the index in the array: bricks[height][depth] = box([I]some function of height and depth[/I]) for that, you would do something like:

boxes = [[] for x in range(10)]
for index,value in enumerate(boxes):
  for d in (9,8):
    boxes[index].append([I]something[/I])

where something probably looks kind of like box((index-5,0,d),'white',materials.marble) I have no idea about the visual module, so you'll have to get help on that part elsewhere.

Oh my word, you are a genius, I have been trying to figure out a way to do that, but I havent been able to, thank you for lines 1-25!
although, the part

boxes = [[] for x in range(10)]
for index,value in enumerate(boxes):
  for d in (9,8):
    boxes[index].append(something)

is totally greek to me.

Oh my word, you are a genius, I have been trying to figure out a way to do that, but I havent been able to, thank you for lines 1-25!
although, the two-dimensional part is totally greek to me.

OK. A little translation, then:

# create a list of 10 empty lists using [B] list comprehension [/B]
boxes = [[] for x in range(10)]
# iterate over the index AND the value of a list using  enumerate() 
for index,value in enumerate(boxes):
  for d in (9,8): # for each index, we want two boxes at depth 9, 8
    boxes[index].append(something) # append to the list-in-a-list

and all the rest as in my previous post.

The reason to do something like this (you don't have to do it exactly like this, of course) is because when you try to discover whether the ball has intersected a brick, you want a good way to look up which brick it is in your array. If there's an easy relationship between the location and the index(s), that lookup is easy. Laying things out in one long array as suggested by your first attempt is probably not the easiest way to do it: A two dimensional array of bricks will make the lookup easier every time you need it (at the cost of making the setup a little bit harder. Once)

okay, thank you. I also stumbled across a thread that says you can import a text file, and have python read it, and print out cubes using the cordinates from the text file, do you know how to do that?

There are three obvious options:

  1. If you create a file named, perhaps, bricks.py that contains real python code, then it is possible to just have a line in your main file that imports it: import bricks There can be issues if that file isn't in the same directory as your main file, but they can be resolved when encountered.
  2. There is also the built in ConfigParser for .ini type files.
  3. Finally, you could just open() ... read() from a normal text file and parse it in your own code.

Of these...

  1. is probably the easiest to do, but also perhaps the most brittle (hard to maintain); particularly if a non-programmer is expected to keep things up to date.
  2. is very maintainable, but there's a bit of a learning curve for ConfigParser
  3. is also pretty easy, and if you already know how to read a text file, the learning curve is not steep.
  4. a kind of compromise between options two and three is possible: http://docs.python.org/library/csv.html

I have done each of these things (and even odder ones) depending on the exact circumstances. None of them is perfect; all of them can be made to work...

Could you help me with my brick game? I am having a difficult time checking if the ball has collided with the panel and the bricks. so far I have

from visual import *
from __future__ import math *
scene.range=10

bricks = [box(pos=vector(i-5,9,0),lenght=1,height=1, width=0, color=color.white)for i in range(0,11)]

ball=sphere(color=color.cyan, pos=vector(0,0,0), radius=0.2)
ball.velocity = vector(0,-1,0)


delta_t= 0.01

panel=box(pos=(0,-9,0), length=2, height=0.5, width=0, color=color.white)
panel.velocity = vector(1,0,0)

       
    

while True:
    rate(600)
    
    ball.pos= ball.pos + ball.velocity*delta_t

    if ball.y<10:
        ball.velocity.y=-ball.velocity.y
    if distance(ball.pos, panel.pos) <= 0.5:
        ball.velocity.y= -ball.velocity.y
    if scene.kb.keys:
        s=scene.kb.getkey()
        if s == 'right':
            panel.pos.x = panel.pos.x + 0.2
        if s == 'left':
            panel.pos.x = panel.pos.x - 0.2

Now, I had made a distance function earlier in my class:

x1 = int(raw_input("please enter your first x value  "))
y1 = int(raw_input("please enter your first y value  "))
x2 = int(raw_input("please enter your second x value  "))
y2 = int(raw_input("please enter your second y value  "))

def distance():
    

    dx = x2 - x1 #Takes the x2 and subtracts the x1

    dy = y2 - y1 #Takes the y2 and subtraces the y1

    return math.sqrt(dx**2 + dy**2

however this is taking two points a user inputs. how would I get it to check the distance of the ball and panel. In theory, I want to make a function like this

dist(a,b):
returns a #
if dist(ball.pos,panel.pos)<= 0.5:
ball.velocity.y=-ball.velocity.y + ball.velocity.x

however, I am not sure exactly how to get it to check the panel, and the ball, then the ball and the bricks

at the first line 5: "lenght" is not going to correctly create your box, I bet.

actually, it greats a line of bricks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.