Sprite and tile collision demo

Game development specific discussions.
BasicCoder2
Posts: 3596
Joined: Jan 01, 2009 7:03
Location: Australia

Sprite and tile collision demo

Postby BasicCoder2 » Mar 05, 2018 14:33

This was just an experiment in writing a sprite (black rectangle) and tile collision routine.
When you run the program there will be a grid showing where a tile can go. You can use the mouse to add obstacle tiles by holding down the left button or remove tiles by holding down the right button.
When you tap the space bar gravity will be activated and the player (black rectangle) will descend until it hits an obstacle or the bottom of the screen.
The left and right arrow keys control direction along the x axis and the up arrow will cause the player to jump.
NOTE: The arrow keys only work when the player is sitting on an obstacle tile or bottom of screen.
While moving the player about you can use the mouse to continue to erase or add tiles.

Code: Select all

' hit1 o-------o hit2
'      |       |
'      |       |
'      |       |
' hit3 o       o hit4
'      |       |
'      |       |
'      |       |
' hit5 o-------o hit6
const SCRW = 1280
const SCRH = 480
screenres SCRW,SCRH,32
color rgb(0,0,0),rgb(200,200,255):cls

dim as integer mx,my,mb

dim shared as any ptr LAYER2
LAYER2 = imagecreate(SCRW,SCRH,rgb(255,0,255))

dim shared as integer hit1,hit2,hit3,hit4,hit5,hit6,hit
dim shared as integer hitTop,hitBot,hitLft,hitRht

type SPRITE
    as integer x
    as integer y
    as integer w
    as integer h
    as integer xd
    as integer yd
end type

dim shared as SPRITE s
s.x = 10
s.y = 10
s.w = 15
s.h = 25

sub update()
    screenlock
    cls
    for j as integer = 0 to 23
        for i as integer = 0 to 63
            line (i*20,j*20)-step(20,20),rgb(127,127,127),b
        next i
    next j
    put (0,0),LAYER2,trans
    line (s.x,s.y)-step(s.w,s.h),rgb(0,0,0),bf
    screenunlock
end sub

sub getHits()
    hit1 = 0:hit2 = 0: hit3 = 0: hit4 = 0: hit5 = 0: hit6 = 0: hit = 0
    if point(s.x,s.y,LAYER2)<>rgb(255,0,255) then  'top/left corner hit
        hit1 = 1
    end if
    if point(s.x+s.w,s.y,LAYER2)<>rgb(255,0,255) then 'top/right corner hit
        hit2 = 1
    end if
    if point(s.x,s.y+s.h\2,LAYER2)<>rgb(255,0,255) then 'middle/left side
        hit3 = 1
    end if
    if point(s.x+s.w,s.y+s.h\2,LAYER2)<>rgb(255,0,255) then 'middle/right side
        hit4 = 1
    end if
    if point(s.x,s.y+s.h,LAYER2)<>rgb(255,0,255) then 'bottom/left
        hit5 = 1
    end if
    if point(s.x+s.w,s.y+s.h,LAYER2)<>rgb(255,0,255) then 'bottom/right
        hit6 = 1
    end if
   
    hitTop = hit1 or hit2
    hitBot = hit5 or hit6
    hitLft = hit1 or hit3 or hit5
    hitRht = hit2 or hit4 or hit6
    if hitLft=1 and hitRht=1 then
        hitLft=0
        hitRht=0
    end if
    hit    = hit1 or hit2 or hit3 or hit4 or hit5 or hit6
end sub

dim as string key
dim as integer go
dim as integer onGround

go = 0
s.yd = 0
do

   
    key = inkey
    if key = " " then
        go = 1
        s.yd = 4  'start gravity
    end if
   
    getmouse mx,my,,mb
    if mb = 1 then
        line LAYER2,(int(mx\20)*20,int(my\20)*20)-step(20,20),rgb(200,100,0),bf
    end if
    if mb = 2 then
        line LAYER2,(int(mx\20)*20,int(my\20)*20)-step(20,20),rgb(255,0,255),bf
    end if

    onGround = 0
    if point(s.x+s.w\2,s.y+s.h+1,LAYER2)<>rgb(255,0,255) then onGround = 1
   
    if onGround = 1 then
        s.xd = 0
       
        if multikey(&H4D) then 'MOVE RIGHT
            s.xd = 4
        end if
       
        if multikey(&H4B) then 'MOVE LEFT
            s.xd =  -4
        end if

        if multikey(&H48) then 'MOVE UP jump
            s.yd = -15
        end if
    end if
   
    if s.yd < 4 and go = 1 then s.yd = s.yd + 1

    s.x = s.x + s.xd
    s.y = s.y + s.yd
   
    'test for out of bounds
    if s.x < 0 then
        s.x = 0
    end if
    if s.x > SCRW - s.w then
        s.x = SCRW - s.w
    end if
    if s.y < 0 then
        s.y = 0
    end if
    if s.y > SCRH - s.h then
        s.y = SCRH - s.h
    end if
   
    'check for sprite/tile collision
    getHits()
    while hit = 1
        if s.xd > 0 and hitRht = 1 then
            s.x = s.x - 1
        elseif s.xd < 0 and hitLft = 1 then
            s.x = s.x + 1
        elseif s.yd > 0 and hitBot = 1 then
            s.y = s.y - 1
        elseif s.yd < 0 and hitTop = 1 then
            s.y = s.y + 1
        end if
        getHits()
    wend   
   
    update()
   
    sleep 2
loop until multikey(&H01)
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Sprite and tile collision demo

Postby lizard » Mar 05, 2018 23:25

Looks good. Its almost the whole game like this you gave the link:
at http://www.lessmilk.com/game/dark-blue/
But the movement is better and faster in FB.
BasicCoder2
Posts: 3596
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Sprite and tile collision demo

Postby BasicCoder2 » Mar 06, 2018 19:42

I once wrote a lot of test FreeBASIC code for retro game mechanics and only gave it another look after reading posts from coderJeff and badidea but clearly no one is much interested in using FreeBASIC to write a serious retro game anymore. Instead other languages appear to be a better choice. The blue games were apparently written using Phaser. Not only can you develop games faster they will also run on mobile phones which I suspect is the most common computer in use today for such simple games.
https://www.discoverphaser.com/
So I guess FreeBASIC is well and truly dead in the water when it comes to writing such games.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Sprite and tile collision demo

Postby lizard » Mar 06, 2018 20:10

There are already to much programs of all sorts. Doesn't make much sense to add another simple program, if not for demonstration purposes. The real strength of FB is to create individual solutions for your own stuff like texts, pictures, formulas and such to bring it in a structured form.

But i think your game is a interesting demo that can help people to understand sprite collision.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Sprite and tile collision demo

Postby sancho3 » Mar 08, 2018 7:35

You can't seriously be believing that Phaser is the tool of choice for mobile development?
Give your head a shake man.

If it was easy or even if it was easier in any other language, you would have done it by now.
You have most of the game components written already. Go ahead and use Phaser, Game Maker, PyGame, Unity, Flash, or any of dozens of game development platforms and you will find out the reality.
And that is... its still up to you and only you and nothing else.
BasicCoder2
Posts: 3596
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Sprite and tile collision demo

Postby BasicCoder2 » Mar 08, 2018 8:53

sancho3 wrote:You can't seriously be believing that Phaser is the tool of choice for mobile development?.

I don't think I claimed that? I have never used it myself it was just the one being used by the author of those simple programs.
From memory I think some ex FB game writers use Unity.
As far as I know you can't compile FB code for the android os? I almost bought a book on programming games for the android system using Java but decided I wasn't interested enough and the book would probably collect dust.
For me writing simple retro like games would be easier with FB but they will not run on a mobile.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Sprite and tile collision demo

Postby sancho3 » Mar 08, 2018 21:20

Well I must say that I share Mr. Swiss's frustration with posts like your follow up to Lizards reply.
That post was totally unnecessary and brought nothing to this thread.
You have stated that you regretted the long debate in the community forum thread that you started, but you brought it here, again anyway.
Certainly, that follow up post was way off topic to this threads topic (your original topic). Maybe keep that stuff in Community Forums?

Now back on topic.
This is an excellent code example. It plays quite well and as Lizard says it is one or two steps from being that Dark Blue game.
I recommend taking those steps. This is a worthwhile exercise and since the graphics are absolutely minimal, you won't get distracted by trying to draw fantastic animations.
Perhaps add the side scrolling coupled with pre-designed platforms as a construct that would be a level. You would need a goal in mind for the level, such as "get to the right end of the level".
I think that the collision checks based on pixel color are going to be a stalling point. They should be changed to mathematical checks; is_point_in_rect() kind of thing.
BasicCoder2
Posts: 3596
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Sprite and tile collision demo

Postby BasicCoder2 » Mar 08, 2018 23:07

sancho3 wrote:Well I must say that I share Mr. Swiss's frustration with posts like your follow up to Lizards reply.

Well I could explain that but I won't bother as it would also add nothing to the thread.
With regards to MrSwiss I took his main objection being that I wasn't interested in using FreeBASIC's advanced programming possibilies and that my code would essentially addle the brain of beginners who would have to unlearn bad coding.

My view is I use FreeBASIC the way a C programmer might use a C++ compiler.

This is an excellent code example. It plays quite well and as Lizard says it is one or two steps from being that Dark Blue game.


According to MrSwiss it was not an excellent code example and I really don't have the time or interest to write an OOP version although I know that is now common practice.

I had tweaked the tile editor and written examples showing how to use its maps in a simple platform game program which included the simple code addition that enabled it to become a side scrolling display and was working on alternate tile collision routines but decided not to post after the MrSwiss roasting and warning not to post old fashioned code.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Sprite and tile collision demo

Postby sancho3 » Mar 09, 2018 0:01

Neither I nor Mr. Swiss run the show here. Ours is opinion and only opinion no matter how it is stated.
My opinion is keep the FB criticisms out of code examples.
If you have a specific concern like "FB doesn't allow me to X, how do I work around it", those are legit questions.
The generic swipes at FB are unnecessary. We all know what FB is and what it isn't.

Its clear that there is interest in the code, so press on, OOP or no OOP.
If a reader has an improvement they would like to share, they can.
If someone is so inclined, they may themselves make an OOP version.

Even if there were no interest, I would press on.
A hobbyist's only concern should be whether he himself is enjoying it.
BasicCoder2
Posts: 3596
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Sprite and tile collision demo

Postby BasicCoder2 » Mar 13, 2018 0:24

sancho3 wrote:Even if there were no interest, I would press on.
A hobbyist's only concern should be whether he himself is enjoying it.

More enjoyment with an audience than without one after all we are to varying degrees social animals.
I had thought about translating the examples in this tutorial to FreeBASIC but I see that it is all OOP.
https://gamedevelopment.tutsplus.com/tu ... -cms-25799
Also for platform games the moving platforms need to be sprites (pixel movement of image).
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Sprite and tile collision demo

Postby lizard » Mar 13, 2018 0:59

From the simple game types i am playing sometimes "break out". There were many good implementations till today. And you have the excuse it is for reaction training.
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Sprite and tile collision demo

Postby Boromir » Mar 13, 2018 2:13

Nice demo.
I noticed that while falling the object does not lose forward momentum which can look pretty weird when falling from high up.

I had actually started working on a stealth & physics platformer a while back but it never got beyond the basic engine phase.
It had ledge hanging zones, water physics and non-tile base collisions. 2d object physics was where I left off.
Image
The graphics were just sloppy test graphics but I had made a nice skeletal sprite tool for animated sprites.
D.J.Peters
Posts: 8189
Joined: May 28, 2005 3:28
Contact:

Re: Sprite and tile collision demo

Postby D.J.Peters » Mar 13, 2018 10:57

Boromir wrote:I had made a nice skeletal sprite tool for animated sprites.
Looks great good job

Joshy
BasicCoder2
Posts: 3596
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Sprite and tile collision demo

Postby BasicCoder2 » Mar 13, 2018 17:41

Boromir wrote:I noticed that while falling the object does not lose forward momentum which can look pretty weird when falling from high up.

So it does! Never had it walking off a cliff. Have to remove forward motion when no jump button was pressed.
I wasn't aiming for real physics. Usually they aim for easy character control. In most games the character can be moved back and forth to some degree in mid air and the jump height varied by techniques such as hitting the jump key twice.
I had made a nice skeletal sprite tool for animated sprites.

Did you ever post it?
I posted some first efforts at isometric rotating animated stick figure rigging. The problem was always motion capture.
Motion capture is easier for 2D side views.
Boromir
Posts: 451
Joined: Apr 30, 2015 19:28
Location: Texas,U.S., Earth,Solar System
Contact:

Re: Sprite and tile collision demo

Postby Boromir » Mar 13, 2018 23:05

D.J.Peters wrote:Looks great good job

Joshy

Thanks!
BasicCoder2 wrote:Did you ever post it?
I posted some first efforts at isometric rotating animated stick figure rigging. The problem was always motion capture.
Motion capture is easier for 2D side views.

I never posted it because some features like building skeletons were less than user friendly. If you're interested I could send you the program though. I was kinda hoping to add some sort of rotoscoping feature where it will play an animation in the background and you move the bones over top but I never got around to that.

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 5 guests