Computer Science Canada

JezzBall

Author:  Mr. T [ Mon Sep 19, 2005 5:55 pm ]
Post subject:  JezzBall

This section of code doesn't seem to work properly.
code:

if whatdotcolour (barX + a, barY) = blue
                    then
                barInterrupt := false
            end if

What this section of code is supposed to do is cancel the creation of a bar if it intersects with a previously created wall. For some reason this only works in certain instances. Any ideas why?

Here's the entire code.
code:

var wallCreator : boolean := true
var ballTrapperBoolean : boolean := true
var barInterrupt : boolean := true
const RADIUS : int := 5
var NUM_BALLS : int := 1
var x, y, dx, dy : array 1 .. NUM_BALLS of int
var xLeadingEdge, yLeadingEdge : int
var clr1 : int := green
var clr2 : int := red
var barX, barY, button, left, middle, right : int
var lives : int := 3
var fontGameOver : int := Font.New ("Arial:80")
var barXStore1, barXStore2, barYStore1, barYStore2 : int


View.Set ("nocursor,nobuttonbar, title:JezzBall                  Lives: " + intstr (lives))



process ballTrapper



    %Mouse Declaration
    barInterrupt := true
    ballTrapperBoolean := false %cant create new wall while previous one is being created
    Mouse.ButtonChoose ("multibutton")
    Mouse.Where (barX, barY, button)
    left := button mod 10           % left = 0 or 1
    middle := (button - left) mod 100       % middle = 0 or 10
    right := button - middle - left         % right = 0 or 100

    %Left Click = Horizontal Bar
    if left = 1 and barX >= maxx div 2 and whatdotcolour (barX, barY) not= blue then
        for a : 1 .. barX


            if whatdotcolour (barX + a, barY) = blue
                    then
                barInterrupt := false
            end if

            if barInterrupt = true then
                Draw.FillBox (barX + a, barY, barX, barY + 10, clr1)
                Draw.FillBox (barX - a, barY, barX, barY + 10, clr2)
                barXStore1 := barX + a
                delay (1)

            else
                clr1 := blue
                Draw.FillBox (barXStore1, barY, barX, barY + 10, clr1)
                Draw.FillBox (barX - a, barY, barX, barY + 10, clr2)
                delay (1)
            end if

            if barX + a = maxx  %first end to reach the wall turns blue
                    then
                clr1 := blue
            end if

            if barX - a = 1 and wallCreator = true %right before for loop ends
                    then
                clr1 := blue
                clr2 := blue %when the wall is complete, it will turn blue,
                %indicating that the ball can collide with it
                %without losing a life.
                Draw.FillBox (barX + a, 0, barX, barY, clr1)  %fills in the space below the ball
                Draw.FillBox (barX - a - 1, 0, barX, barY, clr2) %fills in the space below the ball
            end if

            if wallCreator = false then %ball collided with green wall
                Draw.FillBox (barX + a, barY, barX, barY + 10, white)
                Draw.FillBox (barX - a, barY, barX, barY + 10, white)
                lives := lives - 1 %lost life
                View.Set ("nocursor,nobuttonbar, title:JezzBall                  Lives: " + intstr (lives))
                exit
            end if




        end for
       

        clr1 := green
        clr2 := red %colour reset, next time a wall is created, it will be
        %green, indicating that if the ball collides with it
        %you will lose a life.

    elsif left = 1 and barX < maxx div 2 and whatdotcolour (barX, barY) not= blue then
       
        for a : 1 .. maxx - barX

            if whatdotcolour (barX - a, barY) = blue
                    then
                barInterrupt := false
            end if

            if barInterrupt = true then
                Draw.FillBox (barX + a, barY, barX, barY + 10, clr1) %fills in the space below the ball
                Draw.FillBox (barX - a, barY, barX, barY + 10, clr2) %fills in the space below the ball
                barXStore2 := barX - a
                delay (1)

            else
                clr2 := blue
                Draw.FillBox (barX + a, barY, barX, barY + 10, clr1) %fills in the space below the ball
                Draw.FillBox (barXStore2, barY, barX, barY + 10, clr2) %fills in the space below the ball
                delay (1)
            end if

            if barX - a = 0 %first end to reach the wall turns blue
                    then
                clr2 := blue
            end if

            if barX + a = maxx - 1 and wallCreator = true %right before for loop ends
                    then
                clr1 := blue
                clr2 := blue
                %when the wall is complete, it will turn blue,
                %indicating that the ball can collide with it
                %without losing a life.
                Draw.FillBox (barX + a + 1, 0, barX, barY, clr1)  %fills in the space below the ball
                Draw.FillBox (barX - a, 0, barX, barY, clr2)  %fills in the space below the ball
            end if

            if wallCreator = false then %ball collided with green wall
                Draw.FillBox (barX + a, barY, barX, barY + 10, white) %fills in the space below the ball
                Draw.FillBox (barX - a, barY, barX, barY + 10, white) %fills in the space below the ball
                lives := lives - 1 %lost life
                View.Set ("nocursor,nobuttonbar, title:JezzBall                  Lives: " + intstr (lives))
                exit
            end if




        end for
       

        clr1 := green
        clr2 := red %colour reset, next time a wall is created, it will be
        %green, indicating that if the ball collides with it
        %you will lose a life.

    end if

    %Right Click = Vertical Bar
    if right = 100 and barY >= maxy div 2 and whatdotcolour (barX, barY) not= blue then
        for a : 1 .. barY


            if whatdotcolour (barX, barY + a) = blue
                    then
                barInterrupt := false
            end if

            if barInterrupt = true then
                Draw.FillBox (barX, barY + a, barX + 10, barY, clr1)
                Draw.FillBox (barX, barY - a, barX + 10, barY, clr2)
                barYStore1 := barY + a
                delay (1)

            else
                clr1 := blue
                Draw.FillBox (barX, barYStore1, barX + 10, barY, clr1)
                Draw.FillBox (barX, barY - a, barX + 10, barY, clr2)
                delay (1)
            end if

            if barY + a = maxy %first end to reach the wall turns blue
                    then
                clr1 := blue
            end if

            if barY - a = 1 and wallCreator = true %right before for loop ends
                    then
                clr1 := blue
                clr2 := blue %when the wall is complete, it will turn blue,
                %indicating that the ball can collide with it
                %without losing a life.
                Draw.FillBox (0, barY + a, barX, barY, clr1)   %fills in the space to the side the ball
                Draw.FillBox (0, barY - a - 1, barX, barY, clr2) %fills in the space to the side the ball
            end if

            if wallCreator = false then %ball collided with green wall
                Draw.FillBox (barX, barY + a, barX + 10, barY, white)
                Draw.FillBox (barX, barY - a, barX + 10, barY, white)
                lives := lives - 1 %lost life
                View.Set ("nocursor,nobuttonbar, title:JezzBall                  Lives: " + intstr (lives))
                exit
            end if
           
           
             
       
           
           
        end for
       
 
        clr1 := green
        clr2 := red %colour reset, next time a wall is created, it will be
        %green, indicating that if the ball collides with it
        %you will lose a life.

    elsif right = 100 and barY < maxy div 2 and whatdotcolour (barX, barY) not= blue then
        for a : 1 .. maxy - barY


            if whatdotcolour (barX, barY - a) = blue
                    then
                barInterrupt := false
            end if

            if barInterrupt = true then
                Draw.FillBox (barX, barY + a, barX + 10, barY, clr1)
                Draw.FillBox (barX, barY - a, barX + 10, barY, clr2)
                barYStore2 := barY - a
                delay (1)

            else
                clr2 := blue
                  Draw.FillBox (barX, barY + a, barX + 10, barY, clr1)
                Draw.FillBox (barX, barYStore2, barX + 10, barY, clr2)
                delay (1)
            end if

            if barY - a = 0 %first end to reach the wall turns blue
                    then
                clr2 := blue
            end if

            if barY + a = maxy - 1 and wallCreator = true %right before for loop ends
                    then
                clr1 := blue
                clr2 := blue %when the wall is complete, it will turn blue,
                %indicating that the ball can collide with it
                %without losing a life.
                Draw.FillBox (0, barY + a + 1, barX, barY, clr1) %fills in the space to the side the ball
                Draw.FillBox (0, barY - a, barX, barY, clr2) %fills in the space to the side the ball
            end if

            if wallCreator = false then %ball collided with green wall

                Draw.FillBox (barX, barY + a, barX + 10, barY, white)
                Draw.FillBox (barX, barY - a, barX + 10, barY, white)
                lives := lives - 1 %lost life
                View.Set ("nocursor,nobuttonbar, title:JezzBall                  Lives: " + intstr (lives))
                exit
            end if
 
           
        end for
       

        clr1 := green
        clr2 := red %colour reset, next time a wall is created, it will be
        %green, indicating that if the ball collides with it
        %you will lose a life.
    end if

    %Boolean reset
    wallCreator := true
    ballTrapperBoolean := true

end ballTrapper






for i : 1 .. NUM_BALLS
    x (i) := Rand.Int (RADIUS, maxx - RADIUS)
    y (i) := Rand.Int (RADIUS, maxy - RADIUS)
    dx (i) := Rand.Int (-6, 6)
    dy (i) := Rand.Int (-6, 6)
end for

loop

    %Boundary Wall Collision
    for i : 1 .. NUM_BALLS
        if x (i) + dx (i) < RADIUS or
                x (i) + dx (i) > maxx - RADIUS then

            %Music.Sound (440, 30)
            dx (i) := -dx (i)
        end if
        if y (i) + dy (i) < RADIUS or
                y (i) + dy (i) > maxy - RADIUS then
            %Music.Sound (440, 30)
            dy (i) := -dy (i)
        end if
        x (i) := x (i) + dx (i)
        y (i) := y (i) + dy (i)
        Draw.FillOval (x (i), y (i), RADIUS, RADIUS, red)
        delay (5)
        View.Update
        Draw.FillOval (x (i), y (i), RADIUS, RADIUS, white)

        %Completed Wall Collision


    end for


    %Uncompleted Wall Collision
    if whatdotcolour (x (1) + RADIUS, y (1) + RADIUS) = green or whatdotcolour (x (1) + RADIUS, y (1) + RADIUS) = red then
        wallCreator := false     %if ball collides with green wall, cancel wall creation
    end if


    % Ball Trapper
    if ballTrapperBoolean = true
            then
        fork ballTrapper
    else
    end if


    %Game Over
    if lives = 0 then
        wallCreator := false
        exit
    end if

end loop

cls
View.Set ("nocursor,nobuttonbar, title:JezzBall                  Lives: " + intstr (lives) + "                  You Lose!")
for i : 1 .. 10
    delay (100)
    Font.Draw ("Game Over!", 30, (maxy div 2) - 20, fontGameOver, i)
end for

Author:  beard0 [ Mon Sep 19, 2005 6:53 pm ]
Post subject: 

I was wondering when this would show up again :P
I think I need some info at this point on what you want the overall program to do. Also, with this much code, please post it as an attachment.

Author:  Mr. T [ Mon Sep 19, 2005 7:09 pm ]
Post subject:  Alex's Opinion

This segment of code
code:

if whatdotcolour (barX + a, barY) = blue
                    then
                barInterrupt := false
            end if

should interrupt the creation of a new bar if it detects that it has hit a previously existing bar. For some reason this only happens in certain instances. Test it out and see for yourself.

Author:  beard0 [ Mon Sep 19, 2005 7:12 pm ]
Post subject: 

Yep, I got that. I want to know what the overal program does, or rather, will do eventually.

Author:  Mr. T [ Mon Sep 19, 2005 7:13 pm ]
Post subject:  Alex's Opinion

well have u ever played the real jezzball before?

Author:  beard0 [ Mon Sep 19, 2005 7:18 pm ]
Post subject: 

well, obviously, no, or I wouldn't be asking.

Author:  Mr. T [ Mon Sep 19, 2005 7:38 pm ]
Post subject:  Alex's Opinion

You could have just said that from the get-go. I'm to tired to infer what you are implying. Here's an online version. It's in spanish but wont hinder ur game play. It shouldnt take too long to figure out either.
http://www.bolaloca.com/way/cool/bl/s_play.html

Author:  wtd [ Mon Sep 19, 2005 7:44 pm ]
Post subject:  Re: Alex's Opinion

Pwned wrote:
code:

if whatdotcolour (barX + a, barY) = blue
                    then
                barInterrupt := false
            end if


Bad, bad programmer! Smile

You should know where on the screen any bars are, and at any given time where balls are. Then it's a reasonably simple matter to determine if they're colliding.

Author:  beard0 [ Mon Sep 19, 2005 7:57 pm ]
Post subject: 

Okay, I got you now ;)

So, the problem is that your not resseting your barInterrupt variable to true everywhere you should be. On top of which, you really shouldn't need to use that variable anyway, just put the code that you have in the if barInterrupt blah blah blah... later in this area, under the appopriate section of your if construct.

wtd is one of the people I was talking about earlier who scoffs at the whatdotcolor collision detection, and, right as he is, it can be quite uesful, especially when you are a less experienced programmer.

Author:  Mr. T [ Mon Sep 19, 2005 8:00 pm ]
Post subject:  Re: Alex's Opinion

wtd wrote:
Pwned wrote:
code:

if whatdotcolour (barX + a, barY) = blue
                    then
                barInterrupt := false
            end if


Bad, bad programmer! Smile

Not very supportive. Sad

Author:  Mr. T [ Mon Sep 19, 2005 8:01 pm ]
Post subject:  Alex's Opinion

beard0 wrote:
So, the problem is that your not resseting your barInterrupt variable to true everywhere you should be.

where should it be?

Author:  beard0 [ Mon Sep 19, 2005 8:08 pm ]
Post subject: 

well, your code is pretty messy. Rather than asking that, I was hoping that you would go with the later part of what I was saying, and not use the boolean variable at all.

Author:  Mr. T [ Mon Sep 19, 2005 8:15 pm ]
Post subject:  Alex's Opinion

Ok i took it out like u said, but im still getting the same result Crying or Very sad

Author:  [Gandalf] [ Mon Sep 19, 2005 8:19 pm ]
Post subject:  Re: Alex's Opinion

wtd wrote:
You should know where on the screen any bars are, and at any given time where balls are. Then it's a reasonably simple matter to determine if they're colliding.

I agree, but if you are looking for more speed (Turing-wise) then whatdotcolour would be better (if it is sufficient for the program), since instead of checking each bar against each bar it would only check for a colour once in the whole 'frame'.

Pwned, you took out the variable, now do the next step... beard0 said it already (although slightly 'blahish').

Author:  Mr. T [ Mon Sep 19, 2005 8:24 pm ]
Post subject: 

The second part of what beard0 said was...
beard0 wrote:
just put the code that you have in the if barInterrupt blah blah blah... later in this area, under the appopriate section of your if construct.

but if i already took out the barInterrupt variable, how can i do this?

Author:  beard0 [ Mon Sep 19, 2005 8:30 pm ]
Post subject: 

You need to put the code that should stop the bar moving, within the if construct which detects the blue dot (i.e. collision). Sorry, I know I wasn't very clear before.

Author:  Mr. T [ Mon Sep 19, 2005 8:35 pm ]
Post subject:  Alex's Opinion

i think that's what i tried to do in...
code:

 if whatdotcolour (barX + a, barY) not= blue then

                Draw.FillBox (barX + a, barY, barX, barY + 10, clr1)
                Draw.FillBox (barX - a, barY, barX, barY + 10, clr2)
                barXStore1 := barX + a
                delay (1)

            else
                clr1 := blue
                Draw.FillBox (barXStore1, barY, barX, barY + 10, clr1)
                Draw.FillBox (barX - a, barY, barX, barY + 10, clr2)
                delay (1)
            end if

because if whatdotcolour detects blue, then the current position of barX remains stationary in the form of barXStore1. But for some reason this still isn't working.

Author:  wtd [ Mon Sep 19, 2005 11:27 pm ]
Post subject:  Re: Alex's Opinion

[Gandalf] wrote:
wtd wrote:
You should know where on the screen any bars are, and at any given time where balls are. Then it's a reasonably simple matter to determine if they're colliding.

I agree, but if you are looking for more speed


Doing something wrong fast isn't necessarily good. Wink

Author:  wtd [ Mon Sep 19, 2005 11:28 pm ]
Post subject:  Re: Alex's Opinion

Pwned wrote:
wtd wrote:
Pwned wrote:
code:

if whatdotcolour (barX + a, barY) = blue
                    then
                barInterrupt := false
            end if


Bad, bad programmer! Smile

Not very supportive. Sad


You should have read past that.

Author:  [Gandalf] [ Tue Sep 20, 2005 3:57 pm ]
Post subject:  Re: Alex's Opinion

wtd wrote:
Doing something wrong fast isn't necessarily good. Wink

Sure, but it is the only way around it if you have a quite large Turing program. It's a solution, not a good one, but a solution.

Author:  wtd [ Tue Sep 20, 2005 4:02 pm ]
Post subject: 

If it isn't a good practice, then are you really sure it's a solution at all? Smile

Author:  beard0 [ Tue Sep 20, 2005 4:08 pm ]
Post subject: 

wtd wrote:
If it isn't a good practice, then are you really sure it's a solution at all? :)


That would depend entirely on the situation. Let's say that you needed to write a program that needed to use very little memory, and you wanted to cut any corners you could; this would make whatdotcolour a great solution, though still not a good practice.

For a very real life example, I was working in a laser lab over the summer, and had to do up some assembly. My code was next to incomprehensible - but it got the loop done in the required 17 instruction cycles for the experiment to work. There are always circumstances...

Author:  Mr. T [ Tue Sep 20, 2005 4:27 pm ]
Post subject:  Alex's Opinion

Can we stop pondering about the mysteries of life and start helping me with what I already have? Crying or Very sad

Author:  beard0 [ Tue Sep 20, 2005 4:30 pm ]
Post subject: 

Woah! Calm down there. It's not like you're paying us to help you, were doing this for free, and if something interesting comes up, we'll discuss it. In terms of your problems, it looks like you are detecting collisions on the right side of your wall only.

Author:  wtd [ Tue Sep 20, 2005 4:35 pm ]
Post subject: 

Indeed. If solving one problem does not in turn raise at least one new issue, then you're not doing it right. Especially in programming, there's no such thing as a clean solution in that sense.


: