Breakout in an hour (edit 2 hours)

User projects written in or related to FreeBASIC.
Deleter
Posts: 975
Joined: Jun 22, 2005 22:33

Breakout in an hour (edit 2 hours)

Postby Deleter » May 05, 2007 14:37

What the title said(actually 57 minutes, but close enough):


Code: Select all

#ifdef __FB_WIN32__
SetEnviron("fbgfx=gdi")
#endif
screenres 640,480,32,2

sub flipscr()
    static as integer pg
    screenset pg,1-pg
    pg=1-pg
end sub

const blockW=50
const blockH=20

dim as integer speedMod
do
    input "Difficulty?(1=baby,3=avg,6=gettin dirty,7-10 hardcore)", speedMod
loop until (speedMod > 0 and speedMod < 11)

do
    sleep 5,1
loop while multikey(28)

type block
    as integer x,y
    as integer alive
    as integer colB,colM,colD
    as integer pts
end type

setmouse 320,470,0
dim as integer x,y,b
dim as integer lives=5
dim as integer level=1
dim as integer pX
dim as single bX,bY,bR=4,bVx,bVy
'is ball attached to paddle
dim as integer attached=0
dim as integer pWidth=80
dim as double tm=timer
dim as single flash=0

dim as block grid(640/blockW,240/blockH)

dim as integer combo=0
dim as integer pts,tmppts
for tmpy as integer = 2 to 240/blockH
    for tmpx as integer=1 to 640/blockW-2
        with grid(tmpx,tmpy)
        .x=tmpx*blockW
        .y=tmpy*blockH
        read .alive
        .alive*=-1
        .pts=(240/blockH-tmpy+1)*10*speedMod
        .colB= rgb((1-tmpx/(640/blockW-1))*255,tmpy/(240/blockH)*255,(tmpx/(640/blockW-1))*255)
        .colM= rgb((1-tmpx/(640/blockW-1))*128,tmpy/(240/blockH)*128,(tmpx/(640/blockW-1))*128)
        .colD= rgb((1-tmpx/(640/blockW-1))*64,tmpy/(240/blockH)*64,(tmpx/(640/blockW-1))*64)
        end with
    next
next

dim as any ptr img = imagecreate(640,240+blockH,0)
dim as double fpstm=timer+1
dim as integer tfps,fps

dim as any ptr ballimg=imagecreate(bR,bR,255 shl 16)

windowtitle "Deleter's Breakout Clone"
do
    screenset 1,1
    cls
    locate 30,37
    ? "Level ";str(level)
   
    do
        getmouse x,y,,b
   
        if b > 0 then exit do
        for tmp as integer=0 to 128
            if multikey(tmp) then exit do
        next
        sleep 5,1
    loop
   
    do
        getmouse x,y,,b
   
        if b = 0 then
            for tmp as integer=0 to 128
                if multikey(tmp) then exit for
                if tmp=128 then exit do
            next
        end if
        sleep 5,1
    loop
   
    do
        locate 1,1
        ? "Score:";pts+tmppts*combo;"   "
        locate 1,71
        ? " Lives:";lives;" "
        '? "FPS:"; fps
       
        getmouse x,y,,b
       
        tm=(timer-tm)
        flash-=tm
        if x > -1 then
            if y < 300 or y > 410 then setmouse x,350
            if x < pWidth/2 then setmouse pWidth/2,350
            if x > 640-pWidth/2 then setmouse 640-pWidth/2,350
            dim as integer opX=pX
            pX+=(x-pX)*tm*16
            if pX > x and opX < x then pX=x
            if pX < x and opX > x then pX=x
            'pset(x,y),255
            if pX-pWidth/2 < 0 then pX=pWidth/2
            if pX+pWidth/2 > 640 then pX=640-pWidth/2
           
        end if
       
        if attached<>0 then
           
            'update position
            tm=tm/10
            for tmstep as double = tm to tm*10 step tm
               
                bX+=bVx*tm
                bY+=bVy*tm
                if bX < 0 then
                    bX=0
                    bVx*=-1
                end if
                if bX+bR > 640 then
                    bX=640-bR
                    bVx*=-1
                end if
               
                if bY < 0 then
                    bY=0
                    bVy*=-1
                end if
                if bY+bR > 480 then
                    combo=0
                    tmppts=0
                    lives-=1
                    attached=0
                    exit for
                end if
               
                'check ball collision with paddle
                if bY+bR >= 460 then
                    if bX+bR >= pX-pWidth/2 then
                        if bX < pX+pWidth/2 then
                            pts+=tmppts*combo
                            tmppts=0
                            combo=0
                            bVy*=-1
                            bVx+=(bX-pX)*speedMod
                           
                            bY=460-bR
                            flash=.15
                        end if
                    end if
                end if
               
                'check block collision with ball
                for tmpx as integer=0 to 640/blockW-1
                    for tmpy as integer = 0 to 240/blockH
                        with grid(tmpx,tmpy)
                        if .alive=-1 then
                           
                            if bY+bR >= .y then
                                if bY <= .y+blockH then
                                    if bX+bR >= .x then
                                        if bX <= .x+blockW then
                                            .alive=0
                                            combo+=1
                                            tmppts+=.pts*level
                                            'reflect y
                                            if bY-bVy*tm+bR < .y then
                                                bY=.y-bR
                                            elseif bY-bVy*tm > .y+blockH then
                                                by=.y+blockH+1
                                            else
                                                bVy*=-1
                                            end if
                                           
                                            'reflect x
                                            if bX-bVx*tm+bR < .x then
                                                bX=.x-bR
                                            elseif bX-bVx*tm > .x+blockW then
                                                bX=.x+blockW+1
                                            else
                                                bVx*=-1
                                            end if
                                           
                                            bVx*=-1
                                            bVy*=-1
                                        end if
                                    end if
                                end if
                            end if
                        end if
                        end with
                    next
                next
            next       
        else
            if (b > -1) and (b and 1) then
                bVy=-100*(speedMod)-10*level
                attached=-1
            end if
           
            bX=pX-bR/2
            bY=450
            bVx=0
        end if
       
        tm=timer
       
        dim as integer tmpcount
        'display grid
        for tmpx as integer=0 to 640/blockW-1
            for tmpy as integer = 0 to 240/blockH
                with grid(tmpx,tmpy)
                if .alive=-1 then
                    tmpcount+=1
                    line (.x,.y)-(.x+blockW-1,.y+blockH-1),.colM,bf
                    line (.x,.y)-(.x+blockW-1,.y+blockH-1),.colB,b
                   
                    line (.x,.y+blockH-1)-(.x+blockW-1,.y+blockH-1),.colD,bf
                    line (.x+blockW-1,.y)-(.x+blockW-1,.y+blockH-1),.colD,bf
                end if
                end with
            next
        next
       
        get(1,1)-(639,239+blockH),img
       
        'display paddle
        if flash > 0 then
            line (pX-pWidth/2,460)-(pX+pWidth/2,475),128+1280/1.5*flash,bf
        else
            line (pX-pWidth/2,460)-(pX+pWidth/2,475),128,bf
        end if
        line (pX-pWidth/2,460)-(pX+pWidth/2,475),255,b
        line (pX-pWidth/2,475)-(pX+pWidth/2,475),64,b
        line (pX+pWidth/2,460)-(pX+pWidth/2,475),64,b
       
        if combo > 1 then
            locate 40,37
            ? combo;"x combo"
            line (36*8,39*8-1)-(36*8+8*10,39*8-1),0
        end if
        if tmpcount < 1 then exit do
        if lives < 0 then exit do
       
        'display ball
        'line (bX,bY)-(bX+bR,bY+bR),255 shl 16 , bf
        put(int(bX),int(bY)),ballimg,alpha,100
        put(int(bX),int(bY+1.5)),ballimg,alpha,100
        put(int(bX+1.5),int(bY)),ballimg,alpha,100
        put(int(bX+1.5),int(bY+1.5)),ballimg,alpha,100
       
        if multikey(25) then
            locate 45,39
            ? "Paused"
            flipscr
            tm=timer-tm
            fpstm=timer-fpstm
            while multikey(25):sleep 5,1:wend 'let the user let go
            do:sleep 5,1: loop until multikey(25)
            while multikey(25):sleep 5,1:wend 'let the user let go
            tm=timer-tm
            fpstm=timer-fpstm
            locate 45,39
            ? "      "
        else
            flipscr()
        end if
       
        sleep 5,1
        cls
        put(0,0),img,alpha,240
        if fpstm < timer then
            fpstm=timer+1
            fps=tfps
            tfps=0
            windowtitle "Deleter's Breakout Clone - "+str(fps)+" FPS"
            'getmouse x,y,,b
            'setmouse x,400
        end if
       
        tfps+=1
    loop until multikey(1)
    if multikey(1) then exit do
    screenset 1,1
    cls
    level+=1
    if lives > -1 then
       
    else
        ? "You lose."
        sleep
        exit do
    end if
    attached=0
    combo=0
    if level > 5 then
        select case int(rnd*5)
        case 0
            restore lev1       
        case 1
            restore lev2
        case 2
            restore lev3
        case 3
            restore lev4
        case 4
            restore lev5           
        end select
    end if
   
    for tmpy as integer = 2 to 240/blockH
        for tmpx as integer=1 to 640/blockW-2
            read grid(tmpx,tmpy).alive
            grid(tmpx,tmpy).alive*=-1
        next
    next
loop
sleep 100,1
screenset 1,1
cls
? "Final points: ";str(pts)
sleep
imagedestroy(img)
imagedestroy(ballimg)
end

'level data
lev1:
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1

lev2:
data 0,0,0,0,1,1,1,0,0,0,0
data 0,0,0,1,1,1,1,1,0,0,0
data 0,0,1,1,1,1,1,1,1,0,0
data 0,1,1,1,1,1,1,1,1,1,0
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 0,1,1,1,1,1,1,1,1,1,0
data 0,0,1,1,1,1,1,1,1,0,0
data 0,0,0,1,1,1,1,1,0,0,0
data 0,0,0,0,1,1,1,0,0,0,0

lev3:
data 1,1,1,1,1,1,1,1,1,1,1
data 1,0,0,0,1,1,0,0,0,1,1
data 1,0,0,0,1,1,0,0,0,1,1
data 1,0,1,1,1,1,0,1,0,0,1
data 1,0,1,1,1,1,0,1,0,0,1
data 1,0,0,0,1,1,0,0,0,1,1
data 1,0,0,0,1,1,0,1,0,0,1
data 1,0,1,1,1,1,0,1,0,0,1
data 1,0,1,1,1,1,0,0,0,1,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1

lev4:
data 1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,0,1,1,1,1,1
data 1,1,1,1,0,0,0,1,1,1,1
data 1,1,1,0,0,1,0,0,1,1,1
data 1,1,0,0,1,1,1,0,0,1,1
data 1,0,0,1,1,1,1,1,0,0,1
data 1,1,0,0,1,1,1,0,0,1,1
data 1,1,1,0,0,1,0,0,1,1,1
data 1,1,1,1,0,0,0,1,1,1,1
data 1,1,1,1,1,0,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1

lev5:
data 1,1,1,1,1,1,1,1,1,1,1
data 1,0,1,0,1,0,1,0,1,0,1
data 1,0,1,0,1,0,1,0,1,0,1
data 1,0,1,0,1,0,1,0,1,0,1
data 1,0,1,0,1,0,1,0,1,0,1
data 1,1,1,1,1,1,1,1,1,1,1
data 1,0,1,0,1,0,1,0,1,0,1
data 1,0,1,0,1,0,1,0,1,0,1
data 1,0,1,0,1,0,1,0,1,0,1
data 1,0,1,0,1,0,1,0,1,0,1
data 1,1,1,1,1,1,1,1,1,1,1
Last edited by Deleter on May 15, 2007 3:07, edited 5 times in total.
rdc
Posts: 1725
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Postby rdc » May 05, 2007 15:36

Not bad for an hour's worth of work. A little more polish and this would be a good Breakout clone.
D.J.Peters
Posts: 8357
Joined: May 28, 2005 3:28
Contact:

Postby D.J.Peters » May 05, 2007 18:37

nice for an one our project

Joshy
Deleter
Posts: 975
Joined: Jun 22, 2005 22:33

Postby Deleter » May 05, 2007 21:42

Thanks.

Code updated to include scoring and add clarity to the visual effects as well as an fps counter.
TheBlueKeyboard
Posts: 29
Joined: Oct 23, 2006 8:19

Postby TheBlueKeyboard » May 05, 2007 22:50

I see you've jumped the gun :P

This is a competition between me and Deleter, we'll post both the games up tonight with a vote ;)
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » May 05, 2007 23:23

Image

Biggest Combo: 35x
Score: 1.37mil.
Win?: Yes.

Very awesome. I'm confused as to how your drawing of the block tiles works though. ;_; I just don't understand how those lines result in such an awesome smooth block pattern.
counting_pine
Site Admin
Posts: 6311
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » May 05, 2007 23:59

I changed the getmouse code a bit, so it wouldn't mess up when I (inevitably) went offscreen:

Code: Select all

    tm=timer-tm
    fpstm=timer-fpstm
    while getmouse (x,y,,b): sleep 1: wend
    tm=timer-tm
    fpstm=timer-fpstm
shwS
Posts: 32
Joined: Jan 19, 2007 15:03

Postby shwS » May 06, 2007 0:17

Really fun game ^^

Image
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » May 06, 2007 0:41

shwS wrote:Really fun game ^^
Whoa! Mad skillz!
Deleter
Posts: 975
Joined: Jun 22, 2005 22:33

Postby Deleter » May 06, 2007 3:00

anonymous1337 wrote:Very awesome. I'm confused as to how your drawing of the block tiles works though. ;_; I just don't understand how those lines result in such an awesome smooth block pattern.


Well i don't know if you mean the psuedo 3d effect or the colors, so I'll explain both.

The 3d effect is simply the result of copying the screen to a temporary buffer, and then after cls, 'put'ting it back but one up and one to the left and with an alpha value so it fades out.

For the colors, its all pre calcuated. Each block is button-like in that you have the base color, and then the upper and left edges are brighter and the lower and right edges are darker. For a clear concise fade from one color to the next, the precalc loop translates what step its on to going from 0 to 1 and then multiplies this fractional number times 255 for blue, and 255 - that for red. The green value is calculated by the same process, except using vertical loop values rather than horizontal.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » May 06, 2007 3:14

Thanks @ De Le' Tar
Deleter
Posts: 975
Joined: Jun 22, 2005 22:33

Postby Deleter » May 06, 2007 3:56

@pritch np

Leveling and difficulty choice added.
Dr_D
Posts: 2435
Joined: May 27, 2005 4:59
Contact:

Postby Dr_D » May 06, 2007 4:45

That looks realy nice dude. I think you should make a nice round ball, add some sound effects and call it a game. :)
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » May 06, 2007 4:52

Well, when I run it on my pc on difficulty 10, the ball moves right back through the paddle, I can't win

You have to calculate each movement base don time, and then apply the movement to the ball one 'step' at a time, checking collision each time.

Or i'm sure you could probably use some fancy line intersection math that I don't know. Either way, it's a bug and it's pretty much unplayable for me.
TheBlueKeyboard
Posts: 29
Joined: Oct 23, 2006 8:19

Postby TheBlueKeyboard » May 06, 2007 5:21

http://www.freebasic.net/forum/viewtopic.php?t=8175

This is where the fun is at now ;)

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 8 guests