Brick Python snippet help

Stuck trying to make a brick game

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2347 Views - Last Post: 10 December 2010 - 04:19 PM Rate Topic: -----

#1 efregoso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 10

Brick Python snippet help

Posted 09 December 2010 - 07:56 PM

Hi, Im trying to make a brick game, im only allowed to use vpython, not pygame. I am having trouble getting the ball to change direction and to bounce off the bricks, could you look over my code, and lemme me know? if you could email me at *********

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"

This post has been edited by Dogstopper: 10 December 2010 - 02:40 PM
Reason for edit:: Don't put your email in there.


Is This A Good Question/Topic? 0
  • +

Replies To: Brick Python snippet help

#2 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: Brick Python snippet help

Posted 09 December 2010 - 10:46 PM

I had a look over your code, but it is sort of hard to tell what is what with out the proper code tags. You have used the code tags, but at the end where you have '['code']' you need [/code] without the ' marks(at the very end). That way we can see the indentation. Also, after looking over your code I must ask, where are you checking for a collision with the bricks?

This post has been edited by Eric115: 09 December 2010 - 10:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Brick Python snippet help

Posted 10 December 2010 - 07:26 AM

First of all, please don't ask people to email you the answers. Part of the point of a forum is so that other people who have a similar problem can view the answers and the dialog that lead to the answers. Trying to keep the collective knowledge of the forum all to yourself is very selfish. If you want an email notification of a response, please simply click the 'watch this' button at the top of the screen.

Now that we have that out of way... Ahhh brick breaker, that takes me back. You say you're 'having some trouble' but would you mind expanding on that? Does the ball get stuck on the paddle? Does it go straight through? Does your program crash unexpectedly? Be specific and help us help you! I don't know exactly what your problem is but some comments in your code would go a LONG way. I found a few things worth pointing out though:


    
    if ball.y >= 7 and ball.x >.5 and ball.x <.5:
        ball.velocity.y=-ball.velocity.y


Under what circumstance can a number be both greater than .5 and less that .5? This condition is a mathematical impossibility.



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


You're checking to see if
ball.x <= panel.x+1 and ball.x >= panel.x-1


This can only happen if the difference between panel.x and ball.x is less than 1. This is not a mathematical impossibility, but all 3 conditions being true seems tough. I think you are intending to see if the ball is hitting the panel, but you're not checking anything but the panel.x position. what is panel.x? Is it the center of your panel or the top left corner? If it's the center, then your code is correct... so long as your panel is only 1 pixel wide (assuming that the coordinates are measured in pixels). This is an issue that needs to be addressed. If the x,y coordinates are in pixels, then you have to be EXACT to the pixel to make that ball ricochet. Also, that line has a lot going on. I'd recommend making a function that simply checks if the ball is hitting the panel and returns a boolean. Something like:

if objectsCollided(obj1, obj2):

is far more readable for everyone.



    if ball.y <= -10:
        break
        print "Game Over"


This won't work, if you break out the while loop, it will never hit the print statement. I'd recommend changing it to:

    if ball.y <= -10:
        raw_input("Game Over, press enter to quit")
        break

The raw_input will cause everything to stop until the user hits enter. It's a great practice... if you're using Python 2.x.

I do have some suggestions for the way you're making your bricks, but I don't know how familiar you are with Lists, File I/O, and for loops, so we'll just stop here.

This post has been edited by atraub: 10 December 2010 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#4 efregoso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 10

Re: Brick Python snippet help

Posted 10 December 2010 - 08:16 AM

Well, the problem I am having is that the ball goes through the bricks. I am looking to have it, hit the brick at the surface, and bounce off. then the brick disappear. The panel is measured in the center, but my teacher has not really taught upon hitting objects so I was a bit confused on to how to make that happen. Also when I move the panel to far to either side, the ball freezes up and stops moving. However if I leave it, the ball bounces off of the panel perfectly. So here is a correct pasting of my code so far:
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"
        

Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Brick Python snippet help

Posted 10 December 2010 - 08:21 AM

well I can see why :walkman: you never check to see if the ball hit the bricks.

Just as you do with your panel, you need to check for a collision between each brick and the ball. You can do this more easily by having all your bricks inside a list.

Then you could just do something like:

for brick in brickList:
    if objectsCollided(ball, brick):
        #Take appropriate action



This could be optimized later on with some smart design, but we'll worry about that later.

This post has been edited by atraub: 10 December 2010 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#6 efregoso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 10

Re: Brick Python snippet help

Posted 10 December 2010 - 01:51 PM

well, I tried it like the panel, and it did not work, I will make a list for the bricks though, that does seem much better. So I would just take all the bricks, and just put them in a list correct? no need for brick1, brick2, etc.?
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Brick Python snippet help

Posted 10 December 2010 - 01:58 PM

correct, if all your bricks were in a list, (perhaps called brick) it would then be brick[1], brick[2], etc...

I have so much more I want to tell you based on my experience building this game, but I really don't want to get you ahead of yourself. Haha, buut I can't resist this one. The only difference between ALL of your bricks are the coordinates, right? So what if you read the coordinates from a text file? This would make it really easy for you to design different levels. If you made your bricks more sophisticated (ie different colors and whatnot) your level files will need to change, however, this allows you to create levels without writing them directly into your code.

MUCH later down the line, you could even design a level editor to simplify the job of making brick levels. Level editors vary in difficulty, but it's something to keep in the back of your mind.
Was This Post Helpful? 0
  • +
  • -

#8 efregoso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 10

Re: Brick Python snippet help

Posted 10 December 2010 - 02:02 PM

What do you mean putting the coordinates into another brick file? I am sorry, this is all somewhat greek to me, I am not too familiar with the python coding. I think the most difficult thing I have programed has been a 3d Sierpinski square. Also where could I learn more about lists, so I can stick the bricks into a list
Was This Post Helpful? 0
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Brick Python snippet help

Posted 10 December 2010 - 02:05 PM

The only difference between your bricks are the coordinates, such as
"-5,9,0" for brick 1.

What if you had a text file, and the first 3 lines of that text file were:
-5,9,0
-4,9,0
-3,9,0

and then read the file, 1 line at a time, and said that each line represented coordinates... and you used those coordinates to create your bricks. Essentially, each file would represent 1 level of your game.

This post has been edited by atraub: 10 December 2010 - 02:06 PM

Was This Post Helpful? 0
  • +
  • -

#10 efregoso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 10

Re: Brick Python snippet help

Posted 10 December 2010 - 02:15 PM

So have a list that reads the coordinates, then have a for loop that says for cord in cordlist length = 1, height = 1, width = 1, or something like that? Is that what you mean?
Was This Post Helpful? 0
  • +
  • -

#11 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Brick Python snippet help

Posted 10 December 2010 - 02:23 PM

Not exactly, but don't worry. I haven't had the chance to talk about game programming in a while, I'm so giddy that my mind is way ahead of my mouth.



Alright, so, you understand that a text file in a simple file on your pc. It's simply plain text, nothing special about it. They have the extension .txt

Now, imagine that you make a simple text file and inside that file you have all the coordinates for your bricks. Then you could tell Python

create an empty list to hold your bricks
open my level file
for each line in that file:
    read the current line
    parse the line turning it from a string to 3 ints
    create a brick using those 3 ints and place the brick in the brick list


Now you have created your brick list using a simple text file. The text file is independent of your program, so you don't have to write out all the coordinates into your code.

This design makes your program much more powerful because you can write 10 levels or 1000 with only minimal to no code change!

This post has been edited by atraub: 10 December 2010 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#12 efregoso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 10

Re: Brick Python snippet help

Posted 10 December 2010 - 02:29 PM

okay, so do i write the text file in python? I am sorry, this is the first time I have heard of textfile, I have a great wonder for programming, but I am trying to cram everything in so suddenly. so I understand I would have to create a text file, so like in word or python? then I tell python to open it. Then for each line create a brick out of the cordinates that the line contains? correct?
Was This Post Helpful? 0
  • +
  • -

#13 efregoso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 10

Re: Brick Python snippet help

Posted 10 December 2010 - 02:39 PM

also, I tried the collide statement, and it did not work for me. it is written
if objectsCollide(ball, panel) correct?
Was This Post Helpful? 0
  • +
  • -

#14 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Brick Python snippet help

Posted 10 December 2010 - 02:47 PM

haha no, the text file would be written in notepad. If you're using windows then simply right click on your desktop, choose new, and text document. Text files aren't directly related to Python, or programming in any direct way.

As for objectCollided, I was suggesting you write the function yourself. To my knowledge, the function doesn't exist yet. To write your own function, you would say something like:

def objectCollided(obj1, obj2):
    if the objects have collided: #replace with your code
        return True
    else:
        return False



Let me give you an example:

#Returns true if both values are positive
def bothPositive(val1, val2):
    if val1 > 0 and val2 >0:
        return True
    else:
        return False




By creating your own functions, your program will be easier to read and maintain.

UPDATE:
For more info on File Input and Outpu(I/O), please visit: http://docs.python.o...nputoutput.html
and look at 7.2

This post has been edited by atraub: 10 December 2010 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

#15 efregoso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 10

Re: Brick Python snippet help

Posted 10 December 2010 - 03:39 PM

Oh okay, so like
def objectscollide(obj1, obj2):
if the objects have collided  ball.velocity.y=-ball.velocity.y:
return True:
else return False: 


How would I check if the ball collided with the panel or bricks?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2