Fooling around with platform game-like code

Game development specific discussions.
badidea
Posts: 1016
Joined: May 24, 2007 22:10
Location: The Netherlands

Fooling around with platform game-like code

Postby badidea » Feb 19, 2018 23:10

Not even close to a game yet, but it might evolve in to something...
I my first try at platform game logic, inspired after a visit to http://games.freebasic.net/basicgaming.php

Code: Select all

#include "fbgfx.bi"

const as integer SCRN_W = 800, SCRN_H = 600
'const as integer BLOCK_SZ = 20
'const as integer BLOCKS_X = SCRN_W \ 20, BLOCKS_Y = SCRN_H \ 20
const as single GRAVITY = 3000 '[pixels/s^2]

type xy_sgl
   dim as single x, y
end type

type player_type
   dim as xy_sgl p '[pixels]
   dim as xy_sgl v '[pixels/s]
   dim as integer state
end type

sub clearScreen()
   line(0, 0)-(SCRN_W-1, SCRN_H-1), &h00000000, bf
end sub

sub printAt(x as integer, y as integer, text as string)
   locate y, x: print text;
end sub

enum
   ST_STANDING
   ST_WALKING
   ST_JUMPING
   'ST_DIYING
   'ST_DEAD
   ST_NONE
end enum

dim as string states(ST_NONE) = {"STANDING", "WALKING", "JUMPING", "NONE"}

'dim as integer blocks(BLOCKS_X-1, BLOCKS_Y-1)
dim as double tNow, tPrev, dt
dim as boolean quit = false
dim as player_type player
player.p = type(100.0, 100.0)
player.state = ST_JUMPING

screenres(SCRN_W, SCRN_H, 32)

tNow = timer
dt = 0
while not quit
   if multikey(FB.SC_ESCAPE) then quit = true
   
   with player
      if (.state <> ST_JUMPING) then
         .v.x = 0
         .state = ST_STANDING
         if multikey(FB.SC_LEFT) then
            .v.x = -200.0
            .state = ST_WALKING 'maybe
         end if
         if multikey(FB.SC_RIGHT) then
            .v.x = +200.0
            .state = ST_WALKING 'maybe
         end if
      end if
      
      .p.x += .v.x * dt
      
      'jump if not in air
      if multikey(FB.SC_UP) then
         'if (.p.y > 499) then
         if (.state <> ST_JUMPING) then
            .v.y = -800.0
            .state = ST_JUMPING
         end if
      end if
      
      .v.y += GRAVITY * dt '[m/s] = [m/s^2] * [s]
      .p.y += .v.y * dt '[m] = [m/s] * [s]
      
      'jumped (or dropped) on to somthing
      if (.p.y > 500.0) then
         .p.y = 500.0
         .v.y = 0
         if (.state = ST_JUMPING) then .state = ST_STANDING
      end if
   end with
   
   screenlock
   clearScreen()
   printAt(2, 2, "Keys: <left>, <right>, <up>")
   printAt(2, 4, "Player state: " + str(player.state) + " = " + states(player.state))
   printAt(2, 6, "x: " + str(player.p.x) + ", " + str(player.p.y))
   circle(int(player.p.x + 0.5), int(player.p.y + 0.5)), 20, &h00ff0088
   screenunlock

   sleep 1, 1
   inkey 'clear key buffer
   tPrev = tNow
   tNow = timer
   dt = tNow - tPrev
wend

Sometimes, when pressing (LEFT or RIGHT) with UP at the same time, jumping does not work. Not sure if this is a bug in my code or some limitation of my laptop keyboard.
Last edited by badidea on Feb 20, 2018 7:36, edited 1 time in total.
leopardpm
Posts: 1611
Joined: Feb 28, 2009 20:58

Re: Fooling around with platform game-like code

Postby leopardpm » Feb 27, 2018 16:42

nicely done! I didn't notice the left/right + jumping issue... but only tested for a minute
BasicCoder2
Posts: 3310
Joined: Jan 01, 2009 7:03

Re: Fooling around with platform game-like code

Postby BasicCoder2 » Feb 28, 2018 22:40

Your example, I assume, is using the the ideas explained here,
http://games.freebasic.net/BASICGaming/ ... #tutorial1
"Platform Games 101 - A Jump and Run Game Article for the 6 Year Old in All of Us - Part 3"
So, "The number of seconds elapsed in a fast or a slow computer would always be the same."


Must confess it all seemed very complicated. It also seems more like real physics than game physics?
There are other issues like the force of a jump which the user might input by how long the jump key is pressed.

As it must have some state I think ST_NONE is not required?

Although I see variables for tiles I assume you are not going any further with it?
badidea
Posts: 1016
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Fooling around with platform game-like code

Postby badidea » Feb 28, 2018 23:40

Yes, I want to combine this with my Tetris code (don't tell anyone, still a secret). But progress is slow. Busy with some python code and I will be on holiday the next 2 weeks.

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 2 guests