954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Python Brick Game

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"
efregoso
Newbie Poster
12 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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(<em>some function of height and depth</em>) 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(<em>something</em>)


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.

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

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.

efregoso
Newbie Poster
12 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 
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 <strong> <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">list comprehension</a> </strong>
boxes = [[] for x in range(10)]
# iterate over the index AND the value of a list using  <a href="http://docs.python.org/library/functions.html#enumerate">enumerate()</a> 
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)

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

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?

efregoso
Newbie Poster
12 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

There are three obvious options: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.
There is also the built in ConfigParser for .ini type files.
Finally, you could just open() ... read() from a normal text file and parse it in your own code.
Of these...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.
is very maintainable, but there's a bit of a learning curve for ConfigParser
is also pretty easy, and if you already know how to read a text file, the learning curve is not steep.
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...

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

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

efregoso
Newbie Poster
12 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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

griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
 

actually, it greats a line of bricks.

efregoso
Newbie Poster
12 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: