A simple snake game - with two problem

Game development specific discussions.
Post Reply
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

A simple snake game - with two problem

Post by ITomi »

Hello!

Something is wrong in my newest program. This is a simple snake or worm game, in which you can use the ARROW keys.
But there is two problems in it:
1. in the line 81 (if dologdb<250 then...) the program ought to create apple or poison at random coordinates, but after it creates four or five times, then never again, and I don't know why.
2. I would like draw a filled circle in line 165. It would be an apple. But the compiler doesn't know the "f" flag to fill the circle.
Somebody can help me? Here is the code:

Code: Select all

#include once "fbgfx.bi"
using FB

randomize timer

const kepszel=640,kepmag=480

dim as ubyte dologcsinalo=100
dim shared as integer palya=1,palyaszel,palyamag

type dolog
    as string neve
    as integer xhely,yhely
    as ubyte elettartama=255
end type

dim shared dologdb as ubyte=0
redim shared egydolog(dologdb) as dolog

type kukac
    as integer iranya=0,xhely,yhely,elozox,elozoy
end type

dim shared kukactesthossz as ubyte=2
redim shared kukactest(kukactesthossz) as kukac

screenres kepszel,kepmag,32

sub utkdologgal()
    if dologdb>0 then
        for i as ubyte=0 to dologdb-1
            if egydolog(i).xhely=kukactest(0).xhely and egydolog(i).yhely=kukactest(0).yhely then
                select case egydolog(i).neve
                case "alma"
                    kukactesthossz+=1
                case "mereg"
                    if kukactesthossz=2 then
                        kukactest(0).iranya=-1
                    else
                        kukactesthossz-=1
                    end if
                end select
                if i<dologdb-1 then
                    for j as ubyte=i to dologdb-2
                        egydolog(j)=egydolog(j+1)
                    next j
                end if
                dologdb-=1
                redim preserve egydolog(dologdb)
            end if
        next i
    end if
end sub

sub utkegyeb()
    if kukactest(0).xhely<=8 or kukactest(0).xhely>=palyaszel-8 or kukactest(0).yhely<=8 or kukactest(0).yhely>=palyamag-8 then
        kukactest(0).iranya=-1
    else
        for i as ubyte=1 to kukactesthossz
            if kukactest(0).xhely=kukactest(i).xhely and kukactest(0).yhely=kukactest(i).yhely then
                kukactest(0).iranya=-1 : exit for
            end if
        next i
    end if
end sub

kukactest(0).xhely=kepszel/2 : kukactest(0).yhely=kepmag/2
kukactest(1).xhely=kukactest(0).xhely-10 : kukactest(1).yhely=kepmag/2
kukactest(2).xhely=kukactest(1).xhely-10 : kukactest(2).yhely=kepmag/2

select case palya
    case 1
        palyaszel=kepszel : palyamag=kepmag
end select

do
    if dologcsinalo>0 then
        dologcsinalo-=1
    else
        dologcsinalo=int(rnd*255)
        if dologdb<250 then 'HERE IS THE 1st PROBLEM!!!
            do
                egydolog(dologdb).xhely=int(rnd*kepszel)
            loop until egydolog(dologdb).xhely mod 10=0
            do
                egydolog(dologdb).yhely=int(rnd*kepmag)
            loop until egydolog(dologdb).yhely mod 10=0
            select case int(rnd*2)
            case 0
                egydolog(dologdb).neve="alma"
            case else
                egydolog(dologdb).neve="mereg"
            end select
            dologdb+=1
            redim preserve egydolog(dologdb)
        end if
    end if
    if multikey(sc_left) and kukactest(0).iranya<>-1 then
        if kukactest(0).iranya<>0 then kukactest(0).iranya=180
    end if
    if multikey(sc_right) and kukactest(0).iranya<>-1 then
        if kukactest(0).iranya<>180 then kukactest(0).iranya=0
    end if
    if multikey(sc_up) and kukactest(0).iranya<>-1 then
        if kukactest(0).iranya<>270 then kukactest(0).iranya=90
    end if
    if multikey(sc_down) and kukactest(0).iranya<>-1 then
        if kukactest(0).iranya<>90 then kukactest(0).iranya=270
    end if
    select case kukactest(0).iranya
    case 0
        kukactest(0).elozox=kukactest(0).xhely : kukactest(0).elozoy=kukactest(0).yhely
        kukactest(0).xhely+=10
        utkdologgal() : utkegyeb()
        for i as ubyte=1 to kukactesthossz
            kukactest(i).elozox=kukactest(i).xhely : kukactest(i).elozoy=kukactest(i).yhely
            kukactest(i).xhely=kukactest(i-1).elozox : kukactest(i).yhely=kukactest(i-1).elozoy
        next i
    case 90
        kukactest(0).elozox=kukactest(0).xhely : kukactest(0).elozoy=kukactest(0).yhely
        kukactest(0).yhely-=10
        utkdologgal() : utkegyeb()
        for i as ubyte=1 to kukactesthossz
            kukactest(i).elozox=kukactest(i).xhely : kukactest(i).elozoy=kukactest(i).yhely
            kukactest(i).xhely=kukactest(i-1).elozox : kukactest(i).yhely=kukactest(i-1).elozoy
        next i
    case 180
        kukactest(0).elozox=kukactest(0).xhely : kukactest(0).elozoy=kukactest(0).yhely
        kukactest(0).xhely-=10
        utkdologgal() : utkegyeb()
        for i as ubyte=1 to kukactesthossz
            kukactest(i).elozox=kukactest(i).xhely : kukactest(i).elozoy=kukactest(i).yhely
            kukactest(i).xhely=kukactest(i-1).elozox : kukactest(i).yhely=kukactest(i-1).elozoy
        next i
    case 270
        kukactest(0).elozox=kukactest(0).xhely : kukactest(0).elozoy=kukactest(0).yhely
        kukactest(0).yhely+=10
        utkdologgal() : utkegyeb()
        for i as ubyte=1 to kukactesthossz
            kukactest(i).elozox=kukactest(i).xhely : kukactest(i).elozoy=kukactest(i).yhely
            kukactest(i).xhely=kukactest(i-1).elozox : kukactest(i).yhely=kukactest(i-1).elozoy
        next i
    end select
    screenlock
    cls
        select case palya
        case 1
            Line (8,8)-(palyaszel-8,palyamag-8),rgb(255,255,255),b
        end select
        for i as ubyte=0 to kukactesthossz
            select case i
            case 0
                circle (kukactest(i).xhely,kukactest(i).yhely),5,rgb(255,0,0)
            case kukactesthossz
                circle (kukactest(i).xhely,kukactest(i).yhely),5,rgb(0,255,0)
            case else
                circle (kukactest(i).xhely,kukactest(i).yhely),5,rgb(255,255,255)
            end select
        next i
        if dologdb>0 then
            for i as ubyte=0 to dologdb-1
                if egydolog(i).elettartama>0 then
                    select case egydolog(i).neve
                    case "alma"
                        circle (egydolog(i).xhely,egydolog(i).yhely),5,rgb(255,0,0) 'HERE IS THE 2nd PROBLEM!!! It is not know the 'f' flag to fill itself.
                    case "mereg"
                        line (egydolog(i).xhely,egydolog(i).yhely)-(egydolog(i).xhely+10,egydolog(i).yhely+10),rgb(0,0,255),bf
                        draw string (egydolog(i).xhely+2,egydolog(i).yhely+2),"X",rgb(255,255,255)
                    end select
                    egydolog(i).elettartama-=1
                else
                    if i<dologdb-1 then
                        for j as ubyte=i to dologdb-2
                            egydolog(j)=egydolog(j+1)
                        next j
                    end if
                    dologdb-=1
                    redim preserve egydolog(dologdb)
                end if
            next i
        end if
        draw string (8,1),"Long of the worm: "+str(kukactesthossz)+" ESC: exit, Thing counter: "+str(dologcsinalo)+" NumofThings: "+str(dologdb)
    screenunlock
    sleep 100
loop until multikey(sc_escape)
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: A simple snake game - with two problem

Post by grindstone »

The second problem is easy to solve. You forgot a bunch of commas:

Code: Select all

Circle (egydolog(i).xhely,egydolog(i).yhely),5,RGB(255,0,0),,,,F 'HERE IS THE 2nd PROBLEM!!! 
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: A simple snake game - with two problem

Post by ITomi »

grindstone wrote:The second problem is easy to solve. You forgot a bunch of commas:
Thanks, GrindStone, now it is works! Previously I tried with 3 commas, but it seems, that was not enough...
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: A simple snake game - with two problem

Post by fxm »

To skip n optional parameters, you must put n+1 commas.
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: A simple snake game - with two problem

Post by angros47 »

I found the cause of issue 1.
In line 94, just before the line

Code: Select all

dologdb+=1

Add this line:

Code: Select all

egydolog(dologdb).elettartama=255
In fact, the reason why after a while no new objects are created is that your program actually creates them, but after a while they are created with elettartama = 0 (and so they are instantly destroyed)
That happens because you, instead of setting elettartama when you create the object, have put an initializer in the type: the initializer sets them to 255 when the memory is allocated, but once it has been allocated, it doesn't set the parameter anymore, and your program tends to decrease the value of elettartama to zero, so it will remain 0 after a while.
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: A simple snake game - with two problem

Post by ITomi »

Yeah, this is it! Thank you, Angros47!
I should listen better to this things, too...
One more thing that I forgot two "redim preserve" lines from my program. So, put these at 36. and 42. lines:

Code: Select all

select case egydolog(i).neve
                case "alma"
                    kukactesthossz+=1
                    redim preserve kukactest(kukactesthossz) '<- This
                case "mereg"
                    if kukactesthossz=2 then
                        kukactest(0).iranya=-1
                    else
                        kukactesthossz-=1
                        redim preserve kukactest(kukactesthossz) '<- and this.
                    end if
                end select
And after these, this little game hopefully will works perfectly.
Post Reply