freebasic game "Russian roulette" code review

Game development specific discussions.
ron77
Posts: 194
Joined: Feb 21, 2019 19:24
Location: Israel
Contact:

freebasic game "Russian roulette" code review

Postby ron77 » Feb 19, 2021 12:05

hello...

i've coded a game of Russian roulette design for two players

here is the code:

Code: Select all

#INCLUDE "fbsound_dynamic.bi"
#INCLUDE "fbgfx.bi"

ENUM colors
   fbRed = RGBA(255, 0,0,255)
   fbWhite = RGBA(255,255,255,255)
END ENUM

TYPE player
   PRIVATE:
   AS STRING _player_name
   AS INTEGER _score
   
   PUBLIC:
   AS boolean isTurn
   DECLARE CONSTRUCTOR()
   DECLARE DESTRUCTOR()
   DECLARE PROPERTY player_name AS STRING
   DECLARE PROPERTY player_name (BYVAL player_name1 AS STRING)
   DECLARE PROPERTY score AS INTEGER
   DECLARE PROPERTY score (BYVAL score1 AS INTEGER)
END TYPE

CONSTRUCTOR player
   _score = 5000
END CONSTRUCTOR

DESTRUCTOR player

END DESTRUCTOR


PROPERTY player.player_name AS STRING
   RETURN this._player_name
END PROPERTY

PROPERTY player.player_name(BYVAL player_name1 AS STRING)
   this._player_name = player_name1
END PROPERTY

PROPERTY player.score AS INTEGER
   RETURN this._score
END PROPERTY

PROPERTY player.score(BYVAL score1 AS INTEGER)
   this._score = score1
END PROPERTY

DIM AS SHORT scr_w = 800, scr_h = 600

DIM num_players AS INTEGER


DIM AS INTEGER gun(1 TO 6)

RANDOMIZE TIMER

Function rnd_range (first As Double, last As Double) As Double
    Function = Rnd * (last - first) + first
End FUNCTION

SUB SleepEx()
  sleep
  While Inkey <> "":Wend
end SUB

SUB cprint(row AS INTEGER, s AS STRING)
   LOCATE row, (LOWORD(WIDTH) - LEN(s)) SHR 1 : PRINT s
END SUB

FUNCTION _
   getKeys(_
   BYREF keysToCatch AS CONST STRING) _
   AS STRING
   
   DIM AS STRING _
   k
   
   DO
        k = > INKEY()
       
        SLEEP(1 , 1)
    LOOP UNTIL (INSTR(keysToCatch , k))
   
   'CLEAR keyboard buffer
   DO WHILE (LEN(INKEY()) > 0)
        SLEEP(1 , 1)
    LOOP
   
   RETURN(k)
END FUNCTION


SUB SOUND(f AS STRING , t AS INTEGER)
   DIM AS Integer hWave
   fbs_Load_WAVFile(f , @hWave)
   fbs_Play_Wave(hWave , t)
   SLEEPEX()
   fbs_Destroy_Wave(@hWave)
END SUB


SUB roulette(gun() AS INTEGER, p1 AS player, p2 AS player)
   CLS
   DIM AS INTEGER bank = 2500
   p1.isTurn = TRUE
   DIM AS boolean isShot = FALSE
   DIM shot AS INTEGER
   DIM k AS STRING
   DIM bullet AS INTEGER = 1
   CPRINT 2, "press 1 to load revolver"
   k = GETKEYS("1")
   IF k = "1" THEN
      gun(INT(RND*(rnd_range(1, 7)))) = bullet
      SOUND("gun_ready.wav",1)
      CPRINT 15, "gun is loaded with one bullet"
      sleep
   ENDIF
   DO
   CLS
   CPRINT 2,"Russian Roulette"
   CPRINT 3, "------------------"
   IF p1.isTurn = TRUE THEN
      CPRINT 4, "it's : " & p1.player_name & " TURN"
      p2.isTurn = FALSE
   ELSE
      CPRINT 4, "it's :" & p2.player_name & " TURN"
      p2.isTurn = TRUE
   ENDIF
   
   CPRINT 6, "press key 'r' to roll revolver"
   CPRINT 8, "press space bar to pull the trigger"
   CPRINT 10, "press ESC to quit game"
   CPRINT 12, "press key 'd' for vodka"
   CPRINT 14, "player 1 " & p1.player_name & "                     " & "player 2 " & p2.player_name
   CPRINT 15, "Russian ruble: " & p1.score & "                     " & "Russian ruble: " & p2.score
   CPRINT 16, "Roulette Bank: " & bank & " Russian ruble"
   k = GETKEYS("rd"+ CHR(32) + CHR(27))
   IF k = "r" THEN
      isShot = TRUE
      SOUND("gun_roll.wav",1)
      shot = gun(INT(rnd_range(1, 7)))
   ELSEIF k = CHR(32) THEN
      IF shot = 0 THEN
         IF isShot = FALSE THEN
            CPRINT 17, "empty shot - roll over revolver"
            SLEEPEX()
         ELSEIF isShot = TRUE THEN
            'FOR i AS INTEGER = 1 TO 6
            '   PRINT gun(i)
            'NEXT
            SOUND("empty_gun.wav",1)
            isShot = FALSE
            IF p1.isTurn = TRUE THEN
               p1.score = p1.score + 1000
               p2.score = p2.score - 1000
               p1.score = p1.score + bank
               p1.isTurn = FALSE
            ELSE
               p2.score = p2.score + 1000
               p1.score = p1.score - 1000
               p2.score = p2.score + bank
               p1.isTurn = TRUE
            ENDIF
         ENDIF       
      ELSEIF shot = 1 THEN
         CLS
         COLOR colors.fbRed
         IF p1.isTurn = TRUE THEN
            CPRINT 17, p1.player_name & " YOU LOST!"
         ELSE
            CPRINT 17, p2.player_name & " YOU LOST!"
         ENDIF
         SOUND("gun_shot.wav",1)
         IF p1.isTurn = TRUE THEN
            p2.score = p2.score + p1.score
            p1.score = 0
         ELSE
            p1.score = p1.score + p2.score
            p2.score = 0
         ENDIF
         EXIT SUB
      ENDIF
   ELSEIF k = CHR(27) THEN
      CLS
      CPRINT 17, "chicken $%#@!"
      SOUND("laugh.wav",1)
      EXIT SUB
   ELSEIF k = "d" THEN
      CLS
      CPRINT 17, "another round of vodka!"
      SOUND("vodka.wav", 1)
   ENDIF
   bank += 750
   loop
END SUB

SUB main(gun() AS INTEGER, p1 AS player, p2 AS player)
   DO
      CLS
      COLOR colors.fbWhite
   DIM k AS STRING
   CPRINT 2,"Russian Roulette"
   CPRINT 3, "------------------"
   CPRINT 5, "press 'd' to drink some vodka"
   CPRINT 7, "press esc to quit game"
   CPRINT 9, "press 'r' to play Russian Roulette"
   CPRINT 11, "player 1 " & p1.player_name & "                     " & "player 2 " & p2.player_name
   CPRINT 12, "Russian ruble: " & p1.score & "                     " & "Russian ruble: " & p2.score
   
   k = GETKEYS("rd" + CHR(27))
   IF k = CHR(27) THEN
      CLS
      CPRINT 15, "you quit the game"
      IF p1.score > p2.score THEN
         CPRINT 16, p1.player_name & " YOU WIN!"
      ELSE
         CPRINT 16, p2.player_name & " YOU WIN!"
      ENDIF
      SOUND("laugh.wav", 1)
      END
   ELSEIF k = "d" THEN
      CLS
      CPRINT 15, "another round of vodka!"
      SOUND("vodka.wav", 1)
   ELSEIF k = "r" THEN
      ROULETTE(gun(), p1, p2)
   ENDIF
   
   LOOP
   
   
END SUB

SUB opening(f AS STRING, t AS INTEGER, p1 AS player, p2 AS player)
   DIM k AS STRING
   DIM AS Integer hWave
   CPRINT 15, "Russian roulette"
   SLEEPEX()
   COLOR colors.fbRed
   CLS
   CPRINT 15, "Russian roulette"
   fbs_Load_WAVFile(f , @hWave)
   fbs_Play_Wave(hWave , t)
   SLEEPEX()
   COLOR colors.fbWhite
   fbs_Destroy_Wave(@hWave)
   CLS
   CPRINT 4, "this game is for two players"
   INPUT "player 1 enter your name: ", k
   p1.player_name = k
   INPUT "player 2 enter your name: ", k
   p2.player_name = k
   
END SUB

IF fbs_Init()=false then
  print "error: FBS_INIT() " & FBS_Get_PlugError()
  beep : sleep : end 1
end IF

SCREENRES scr_w, scr_h, 32
WIDTH scr_w \ 8, scr_h \ 16
DIM AS player p1, p2
OPENING("gun_shot.wav", 1, p1, p2)
MAIN(gun(), p1, p2)
SLEEP


dependencies: fbsound1.1 lib

the game works as expected except for one time it crashed after a long play... please try it - Code criticism is welcome as well as code review or any issues you may find...

ron77
Last edited by ron77 on Feb 19, 2021 21:00, edited 1 time in total.
ron77
Posts: 194
Joined: Feb 21, 2019 19:24
Location: Israel
Contact:

Re: freebasic game "Russian roulette" code review

Postby ron77 » Feb 19, 2021 13:50

hello...

hell i fixed a bug in my code that caused it to crash :)

the problem was i didn't use properly the "rand_range()" function inside "gun()" array i added "INT" and "RAD" while the function already had a "RAD"

here is the new updated code:

Code: Select all

#INCLUDE "fbsound_dynamic.bi"
#INCLUDE "fbgfx.bi"

ENUM colors
   fbRed = RGBA(255, 0,0,255)
   fbWhite = RGBA(255,255,255,255)
END ENUM

TYPE player
   PRIVATE:
   AS STRING _player_name
   AS INTEGER _score
   
   PUBLIC:
   AS boolean isTurn
   DECLARE CONSTRUCTOR()
   DECLARE DESTRUCTOR()
   DECLARE PROPERTY player_name AS STRING
   DECLARE PROPERTY player_name (BYVAL player_name1 AS STRING)
   DECLARE PROPERTY score AS INTEGER
   DECLARE PROPERTY score (BYVAL score1 AS INTEGER)
END TYPE

CONSTRUCTOR player
   _score = 5000
END CONSTRUCTOR

DESTRUCTOR player

END DESTRUCTOR


PROPERTY player.player_name AS STRING
   RETURN this._player_name
END PROPERTY

PROPERTY player.player_name(BYVAL player_name1 AS STRING)
   this._player_name = player_name1
END PROPERTY

PROPERTY player.score AS INTEGER
   RETURN this._score
END PROPERTY

PROPERTY player.score(BYVAL score1 AS INTEGER)
   this._score = score1
END PROPERTY

DIM AS SHORT scr_w = 800, scr_h = 600

DIM num_players AS INTEGER


DIM AS INTEGER gun(1 TO 6)

RANDOMIZE TIMER

Function rnd_range (first As Double, last As Double) As Double
    Function = Rnd * (last - first) + first
End FUNCTION

SUB SleepEx()
  sleep
  While Inkey <> "":Wend
end SUB

SUB cprint(row AS INTEGER, s AS STRING)
   LOCATE row, (LOWORD(WIDTH) - LEN(s)) SHR 1 : PRINT s
END SUB

FUNCTION _
   getKeys(_
   BYREF keysToCatch AS CONST STRING) _
   AS STRING
   
   DIM AS STRING _
   k
   
   DO
        k = > INKEY()
       
        SLEEP(1 , 1)
    LOOP UNTIL (INSTR(keysToCatch , k))
   
   'CLEAR keyboard buffer
   DO WHILE (LEN(INKEY()) > 0)
        SLEEP(1 , 1)
    LOOP
   
   RETURN(k)
END FUNCTION


SUB SOUND(f AS STRING , t AS INTEGER)
   DIM AS Integer hWave
   fbs_Load_WAVFile(f , @hWave)
   fbs_Play_Wave(hWave , t)
   SLEEPEX()
   fbs_Destroy_Wave(@hWave)
END SUB


SUB roulette(gun() AS INTEGER, p1 AS player, p2 AS player)
   CLS
   DIM AS INTEGER bank = 2500
   p1.isTurn = TRUE
   DIM AS boolean isShot = FALSE
   DIM shot AS INTEGER
   DIM k AS STRING
   DIM bullet AS INTEGER = 1
   CPRINT 2, "press 1 to load revolver"
   k = GETKEYS("1")
   IF k = "1" THEN
      gun(INT(rnd_range(1, 7))) = bullet
      SOUND("gun_ready.wav",1)
      CPRINT 15, "gun is loaded with one bullet"
      sleep
   ENDIF
   DO
   CLS
   CPRINT 2,"Russian Roulette"
   CPRINT 3, "------------------"
   IF p1.isTurn = TRUE THEN
      CPRINT 4, "it's : " & p1.player_name & " TURN"
      p2.isTurn = FALSE
   ELSE
      CPRINT 4, "it's :" & p2.player_name & " TURN"
      p2.isTurn = TRUE
   ENDIF
   
   CPRINT 6, "press key 'r' to roll revolver"
   CPRINT 8, "press space bar to pull the trigger"
   CPRINT 10, "press ESC to quit game"
   CPRINT 12, "press key 'd' for vodka"
   CPRINT 14, "player 1 " & p1.player_name & "                     " & "player 2 " & p2.player_name
   CPRINT 15, "Russian ruble: " & p1.score & "                     " & "Russian ruble: " & p2.score
   CPRINT 16, "Roulette Bank: " & bank & " Russian ruble"
   k = GETKEYS("rd"+ CHR(32) + CHR(27))
   IF k = "r" THEN
      isShot = TRUE
      SOUND("gun_roll.wav",1)
      shot = gun(INT(rnd_range(1, 7)))
   ELSEIF k = CHR(32) THEN
      IF shot = 0 THEN
         IF isShot = FALSE THEN
            CPRINT 17, "empty shot - roll over revolver"
            SLEEPEX()
         ELSEIF isShot = TRUE THEN
            'FOR i AS INTEGER = 1 TO 6
            '   PRINT gun(i)
            'NEXT
            SOUND("empty_gun.wav",1)
            isShot = FALSE
            IF p1.isTurn = TRUE THEN
               p1.score = p1.score + 1000
               p2.score = p2.score - 1000
               p1.score = p1.score + bank
               p1.isTurn = FALSE
            ELSE
               p2.score = p2.score + 1000
               p1.score = p1.score - 1000
               p2.score = p2.score + bank
               p1.isTurn = TRUE
            ENDIF
         ENDIF       
      ELSEIF shot = 1 THEN
         CLS
         COLOR colors.fbRed
         IF p1.isTurn = TRUE THEN
            CPRINT 17, p1.player_name & " YOU LOST!"
         ELSE
            CPRINT 17, p2.player_name & " YOU LOST!"
         ENDIF
         SOUND("gun_shot.wav",1)
         IF p1.isTurn = TRUE THEN
            p2.score = p2.score + p1.score
            p1.score = 0
         ELSE
            p1.score = p1.score + p2.score
            p2.score = 0
         ENDIF
         EXIT SUB
      ENDIF
   ELSEIF k = CHR(27) THEN
      CLS
      CPRINT 17, "chicken $%#@!"
      SOUND("laugh.wav",1)
      EXIT SUB
   ELSEIF k = "d" THEN
      CLS
      CPRINT 17, "another round of vodka!"
      SOUND("vodka.wav", 1)
   ENDIF
   bank += 750
   loop
END SUB

SUB main(gun() AS INTEGER, p1 AS player, p2 AS player)
   DO
      CLS
      COLOR colors.fbWhite
   DIM k AS STRING
   CPRINT 2,"Russian Roulette"
   CPRINT 3, "------------------"
   CPRINT 5, "press 'd' to drink some vodka"
   CPRINT 7, "press esc to quit game"
   CPRINT 9, "press 'r' to play Russian Roulette"
   CPRINT 11, "player 1 " & p1.player_name & "                     " & "player 2 " & p2.player_name
   CPRINT 12, "Russian ruble: " & p1.score & "                     " & "Russian ruble: " & p2.score
   
   k = GETKEYS("rd" + CHR(27))
   IF k = CHR(27) THEN
      CLS
      CPRINT 15, "you quit the game"
      IF p1.score > p2.score THEN
         CPRINT 16, p1.player_name & " YOU WIN!"
      ELSE
         CPRINT 16, p2.player_name & " YOU WIN!"
      ENDIF
      SOUND("laugh.wav", 1)
      END
   ELSEIF k = "d" THEN
      CLS
      CPRINT 15, "another round of vodka!"
      SOUND("vodka.wav", 1)
   ELSEIF k = "r" THEN
      ROULETTE(gun(), p1, p2)
   ENDIF
   
   LOOP
   
   
END SUB

SUB opening(f AS STRING, t AS INTEGER, p1 AS player, p2 AS player)
   DIM k AS STRING
   DIM AS Integer hWave
   CPRINT 15, "Russian roulette"
   SLEEPEX()
   COLOR colors.fbRed
   CLS
   CPRINT 15, "Russian roulette"
   fbs_Load_WAVFile(f , @hWave)
   fbs_Play_Wave(hWave , t)
   SLEEPEX()
   COLOR colors.fbWhite
   fbs_Destroy_Wave(@hWave)
   CLS
   CPRINT 4, "this game is for two players"
   INPUT "player 1 enter your name: ", k
   p1.player_name = k
   INPUT "player 2 enter your name: ", k
   p2.player_name = k
   
END SUB

IF fbs_Init()=false then
  print "error: FBS_INIT() " & FBS_Get_PlugError()
  beep : sleep : end 1
end IF

SCREENRES scr_w, scr_h, 32
WIDTH scr_w \ 8, scr_h \ 16
DIM AS player p1, p2
OPENING("gun_shot.wav", 1, p1, p2)
MAIN(gun(), p1, p2)
SLEEP


ron77
MrSwiss
Posts: 3826
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: freebasic game "Russian roulette" code review

Postby MrSwiss » Feb 19, 2021 14:14

For 32-bit colors I would NOT use Enum (result is: Integer) but rather ULong Const's.

Example below:

Code: Select all

Const As ULong  red    = &hFFCF0000, orange = &hFF7F7F00, _
                yellow = &hFFFFDF00, green  = &hFF00CF3F, _
                cyan   = &hFF00CFCF, blue   = &hFF003FCF, _
                purple = &hFFCF00CF, black  = &hFF000000, _
                d_grey = &hFF3F3F3F, m_grey = &hFF7F7F7F, _
                b_grey = &hFFCFCFCF, white  = &hFFFFFFFF
ron77
Posts: 194
Joined: Feb 21, 2019 19:24
Location: Israel
Contact:

Re: freebasic game "Russian roulette" code review

Postby ron77 » Feb 19, 2021 16:07

hello @MrSwiss and everyone...

thanks for the tip...

i've changed the code to use ulong consts for the colors... and i also changed the use of sleepex() back to "sleep" command in the "sound()" sub...

here is the updated code:

Code: Select all

#INCLUDE "fbsound_dynamic.bi"
#INCLUDE "fbgfx.bi"

'ENUM colors
CONST AS ULONG fbRed = RGBA(255, 0,0,255)
CONST AS ULONG fbWhite = RGBA(255,255,255,255)
'END ENUM

TYPE player
   PRIVATE:
   AS STRING _player_name
   AS INTEGER _score
   
   PUBLIC:
   AS boolean isTurn
   DECLARE CONSTRUCTOR()
   DECLARE DESTRUCTOR()
   DECLARE PROPERTY player_name AS STRING
   DECLARE PROPERTY player_name (BYVAL player_name1 AS STRING)
   DECLARE PROPERTY score AS INTEGER
   DECLARE PROPERTY score (BYVAL score1 AS INTEGER)
END TYPE

CONSTRUCTOR player
   _score = 5000
END CONSTRUCTOR

DESTRUCTOR player

END DESTRUCTOR


PROPERTY player.player_name AS STRING
   RETURN this._player_name
END PROPERTY

PROPERTY player.player_name(BYVAL player_name1 AS STRING)
   this._player_name = player_name1
END PROPERTY

PROPERTY player.score AS INTEGER
   RETURN this._score
END PROPERTY

PROPERTY player.score(BYVAL score1 AS INTEGER)
   this._score = score1
END PROPERTY

DIM AS SHORT scr_w = 800, scr_h = 600

DIM num_players AS INTEGER


DIM AS INTEGER gun(1 TO 6)

RANDOMIZE TIMER

Function rnd_range (first As Double, last As Double) As Double
    Function = Rnd * (last - first) + first
End FUNCTION

SUB SleepEx()
  sleep
  While Inkey <> "":Wend
end SUB

SUB cprint(row AS INTEGER, s AS STRING)
   LOCATE row, (LOWORD(WIDTH) - LEN(s)) SHR 1 : PRINT s
END SUB

FUNCTION _
   getKeys(_
   BYREF keysToCatch AS CONST STRING) _
   AS STRING
   
   DIM AS STRING _
   k
   
   DO
        k = > INKEY()
       
        SLEEP(1 , 1)
    LOOP UNTIL (INSTR(keysToCatch , k))
   
   'CLEAR keyboard buffer
   DO WHILE (LEN(INKEY()) > 0)
        SLEEP(1 , 1)
    LOOP
   
   RETURN(k)
END FUNCTION


SUB SOUND(f AS STRING , t AS INTEGER)
   DIM AS Integer hWave
   fbs_Load_WAVFile(f , @hWave)
   fbs_Play_Wave(hWave , t)
   SLEEP()
   fbs_Destroy_Wave(@hWave)
END SUB


SUB roulette(gun() AS INTEGER, p1 AS player, p2 AS player)
   CLS
   DIM AS INTEGER bank = 2500
   p1.isTurn = TRUE
   DIM AS boolean isShot = FALSE
   DIM shot AS INTEGER
   DIM k AS STRING
   DIM bullet AS INTEGER = 1
   CPRINT 2, "press 1 to load revolver"
   k = GETKEYS("1")
   IF k = "1" THEN
      gun(INT(rnd_range(1, 7))) = bullet
      SOUND("gun_ready.wav",1)
      CPRINT 15, "gun is loaded with one bullet"
      sleep
   ENDIF
   DO
   CLS
   CPRINT 2,"Russian Roulette"
   CPRINT 3, "------------------"
   IF p1.isTurn = TRUE THEN
      CPRINT 4, "it's : " & p1.player_name & " TURN"
      p2.isTurn = FALSE
   ELSE
      CPRINT 4, "it's :" & p2.player_name & " TURN"
      p2.isTurn = TRUE
   ENDIF
   
   CPRINT 6, "press key 'r' to roll revolver"
   CPRINT 8, "press space bar to pull the trigger"
   CPRINT 10, "press ESC to quit game"
   CPRINT 12, "press key 'd' for vodka"
   CPRINT 14, "player 1 " & p1.player_name & "                     " & "player 2 " & p2.player_name
   CPRINT 15, "Russian ruble: " & p1.score & "                     " & "Russian ruble: " & p2.score
   CPRINT 16, "Roulette Bank: " & bank & " Russian ruble"
   k = GETKEYS("rd"+ CHR(32) + CHR(27))
   IF k = "r" THEN
      isShot = TRUE
      SOUND("gun_roll.wav",1)
      shot = gun(INT(rnd_range(1, 7)))
   ELSEIF k = CHR(32) THEN
      IF shot = 0 THEN
         IF isShot = FALSE THEN
            CPRINT 17, "empty shot - roll over revolver"
            SLEEP
         ELSEIF isShot = TRUE THEN
            'FOR i AS INTEGER = 1 TO 6
            '   PRINT gun(i)
            'NEXT
            SOUND("empty_gun.wav",1)
            isShot = FALSE
            IF p1.isTurn = TRUE THEN
               p1.score = p1.score + 1000
               p2.score = p2.score - 1000
               p1.score = p1.score + bank
               p1.isTurn = FALSE
            ELSE
               p2.score = p2.score + 1000
               p1.score = p1.score - 1000
               p2.score = p2.score + bank
               p1.isTurn = TRUE
            ENDIF
         ENDIF       
      ELSEIF shot = 1 THEN
         CLS
         COLOR fbRed
         IF p1.isTurn = TRUE THEN
            CPRINT 17, p1.player_name & " YOU LOST!"
         ELSE
            CPRINT 17, p2.player_name & " YOU LOST!"
         ENDIF
         SOUND("gun_shot.wav",1)
         IF p1.isTurn = TRUE THEN
            p2.score = p2.score + p1.score
            p1.score = 0
         ELSE
            p1.score = p1.score + p2.score
            p2.score = 0
         ENDIF
         EXIT SUB
      ENDIF
   ELSEIF k = CHR(27) THEN
      CLS
      CPRINT 17, "chicken $%#@!"
      SOUND("laugh.wav",1)
      EXIT SUB
   ELSEIF k = "d" THEN
      CLS
      CPRINT 17, "another round of vodka!"
      SOUND("vodka.wav", 1)
   ENDIF
   bank += 750
   loop
END SUB

SUB main(gun() AS INTEGER, p1 AS player, p2 AS player)
   DO
      CLS
      COLOR fbWhite
   DIM k AS STRING
   CPRINT 2,"Russian Roulette"
   CPRINT 3, "------------------"
   CPRINT 5, "press 'd' to drink some vodka"
   CPRINT 7, "press esc to quit game"
   CPRINT 9, "press 'r' to play Russian Roulette"
   CPRINT 11, "player 1 " & p1.player_name & "                     " & "player 2 " & p2.player_name
   CPRINT 12, "Russian ruble: " & p1.score & "                     " & "Russian ruble: " & p2.score
   
   k = GETKEYS("rd" + CHR(27))
   IF k = CHR(27) THEN
      CLS
      CPRINT 15, "you quit the game"
      IF p1.score > p2.score THEN
         CPRINT 16, p1.player_name & " YOU WIN!"
      ELSE
         CPRINT 16, p2.player_name & " YOU WIN!"
      ENDIF
      SOUND("laugh.wav", 1)
      END
   ELSEIF k = "d" THEN
      CLS
      CPRINT 15, "another round of vodka!"
      SOUND("vodka.wav", 1)
   ELSEIF k = "r" THEN
      ROULETTE(gun(), p1, p2)
   ENDIF
   
   LOOP
   
   
END SUB

SUB opening(f AS STRING, t AS INTEGER, p1 AS player, p2 AS player)
   DIM k AS STRING
   DIM AS Integer hWave
   CPRINT 15, "Russian roulette"
   SLEEPEX()
   COLOR fbRed
   CLS
   CPRINT 15, "Russian roulette"
   fbs_Load_WAVFile(f , @hWave)
   fbs_Play_Wave(hWave , t)
   SLEEPEX()
   COLOR fbWhite
   fbs_Destroy_Wave(@hWave)
   CLS
   CPRINT 4, "this game is for two players"
   INPUT "player 1 enter your name: ", k
   p1.player_name = k
   INPUT "player 2 enter your name: ", k
   p2.player_name = k
   
END SUB

IF fbs_Init()=false then
  print "error: FBS_INIT() " & FBS_Get_PlugError()
  beep : sleep : end 1
end IF

SCREENRES scr_w, scr_h, 32
WIDTH scr_w \ 8, scr_h \ 16
DIM AS player p1, p2
OPENING("gun_shot.wav", 1, p1, p2)
MAIN(gun(), p1, p2)
SLEEP


ron77
ron77
Posts: 194
Joined: Feb 21, 2019 19:24
Location: Israel
Contact:

Re: freebasic game "Russian roulette" code review

Postby ron77 » Feb 27, 2021 9:33

hello...

here is the updated code of the game "Russian roulette" for 2 players...

Code: Select all

#INCLUDE "fbsound_dynamic.bi"
#INCLUDE "fbgfx.bi"

'ENUM colors
CONST AS ULONG fbRed = RGBA(255, 0,0,255)
CONST AS ULONG fbWhite = RGBA(255,255,255,255)
'END ENUM

TYPE player
   PRIVATE:
   AS STRING _player_name
   AS INTEGER _score
   
   PUBLIC:
   AS boolean isTurn
   DECLARE CONSTRUCTOR()
   DECLARE DESTRUCTOR()
   DECLARE PROPERTY player_name AS STRING
   DECLARE PROPERTY player_name (BYVAL player_name1 AS STRING)
   DECLARE PROPERTY score AS INTEGER
   DECLARE PROPERTY score (BYVAL score1 AS INTEGER)
END TYPE

CONSTRUCTOR player
   _score = 5000
END CONSTRUCTOR

DESTRUCTOR player

END DESTRUCTOR


PROPERTY player.player_name AS STRING
   RETURN this._player_name
END PROPERTY

PROPERTY player.player_name(BYVAL player_name1 AS STRING)
   this._player_name = player_name1
END PROPERTY

PROPERTY player.score AS INTEGER
   RETURN this._score
END PROPERTY

PROPERTY player.score(BYVAL score1 AS INTEGER)
   this._score = score1
END PROPERTY

DIM AS SHORT scr_w = 800, scr_h = 600

DIM num_players AS INTEGER


DIM AS INTEGER gun(1 TO 6)

RANDOMIZE TIMER

Function rnd_range (first As Double, last As Double) As Double
    Function = Rnd * (last - first) + first
End FUNCTION

SUB SleepEx()
  sleep
  While Inkey <> "":Wend
end SUB

SUB cprint(row AS INTEGER, s AS STRING)
   LOCATE row, (LOWORD(WIDTH) - LEN(s)) SHR 1 : PRINT s
END SUB

FUNCTION _
   getKeys(_
   BYREF keysToCatch AS CONST STRING) _
   AS STRING
   
   DIM AS STRING _
   k
   
   DO
        k = > INKEY()
       
        SLEEP(1 , 1)
    LOOP UNTIL (INSTR(keysToCatch , k))
   
   'CLEAR keyboard buffer
   DO WHILE (LEN(INKEY()) > 0)
        SLEEP(1 , 1)
    LOOP
   
   RETURN(k)
END FUNCTION


SUB SOUND(f AS STRING , t AS INTEGER)
   DIM AS Integer hWave
   fbs_Load_WAVFile(f , @hWave)
   fbs_Play_Wave(hWave , t)
   SLEEP()
   fbs_Destroy_Wave(@hWave)
END SUB


SUB roulette(gun() AS INTEGER, p1 AS player, p2 AS player)
   CLS
   DIM AS INTEGER bank = 2500
   p1.isTurn = TRUE
   DIM AS boolean isShot = FALSE
   DIM shot AS INTEGER
   DIM k AS STRING
   DIM bullet AS INTEGER = 1
   CPRINT 2, "press key 1 to load revolver with one bullet"
   CPRINT 4, "press key 2 to load revolver with two bullets"
   k = GETKEYS("12")
   IF k = "1" THEN
      gun(INT(rnd_range(1, 7))) = bullet
      CPRINT 15, "gun is loaded with one bullet"
      SOUND("gun_ready.wav",1)
   ELSEIF k = "2" THEN
      gun(1) = bullet
      gun(4) = bullet
      CPRINT 15, "gun is loaded with two bullets"
      SOUND("gun_ready.wav",1)     
   ENDIF
   DO
   CLS
   CPRINT 2,"Russian Roulette"
   CPRINT 3, "------------------"
   IF p1.isTurn = TRUE THEN
      CPRINT 4, "it's : " & p1.player_name & " TURN"
      p2.isTurn = FALSE
   ELSE
      CPRINT 4, "it's :" & p2.player_name & " TURN"
      p2.isTurn = TRUE
   ENDIF
   
   CPRINT 6, "press key 'r' to roll revolver"
   CPRINT 8, "press space bar to pull the trigger"
   CPRINT 10, "press ESC to quit game"
   CPRINT 12, "press key 'd' for vodka"
   CPRINT 14, "player 1 " & p1.player_name & "                     " & "player 2 " & p2.player_name
   CPRINT 15, "Russian ruble: " & p1.score & "                     " & "Russian ruble: " & p2.score
   CPRINT 16, "Roulette Bank: " & bank & " Russian ruble"
   k = GETKEYS("rd"+ CHR(32) + CHR(27))
   IF k = "r" THEN
      isShot = TRUE
      SOUND("gun_roll.wav",1)
      shot = gun(INT(rnd_range(1, 7)))
   ELSEIF k = CHR(32) THEN
      IF shot = 0 THEN
         IF isShot = FALSE THEN
            CPRINT 17, "empty shot - roll over revolver"
            SLEEP
         ELSEIF isShot = TRUE THEN
            SOUND("empty_gun.wav",1)
            isShot = FALSE
            IF p1.isTurn = TRUE THEN
               p1.score = p1.score + 1000
               p2.score = p2.score - 1000
               p1.score = p1.score + bank
               p1.isTurn = FALSE
            ELSE
               p2.score = p2.score + 1000
               p1.score = p1.score - 1000
               p2.score = p2.score + bank
               p1.isTurn = TRUE
            ENDIF
         ENDIF       
      ELSEIF shot = 1 THEN
         CLS
         COLOR fbRed
         IF p1.isTurn = TRUE THEN
            CPRINT 17, p1.player_name & " YOU LOST!"
            p2.score = p2.score + p1.score
            p1.score = 0
         ELSE
            CPRINT 17, p2.player_name & " YOU LOST!"
            p1.score = p1.score + p2.score
            p2.score = 0
         ENDIF
         SOUND("gun_shot.wav",1)
         EXIT SUB
      ENDIF
   ELSEIF k = CHR(27) THEN
      CLS
      CPRINT 17, "chicken $%#@!"
      SOUND("laugh.wav",1)
      EXIT SUB
   ELSEIF k = "d" THEN
      CLS
      CPRINT 17, "another round of vodka!"
      SOUND("vodka.wav", 1)
   ENDIF
   bank += 750
   loop
END SUB

SUB main(gun() AS INTEGER, p1 AS player, p2 AS player)
   DO
      CLS
      COLOR fbWhite
   DIM k AS STRING
   CPRINT 2,"Russian Roulette"
   CPRINT 3, "------------------"
   CPRINT 5, "press 'd' to drink some vodka"
   CPRINT 7, "press esc to quit game"
   CPRINT 9, "press 'r' to play Russian Roulette"
   CPRINT 11, "player 1 " & p1.player_name & "                     " & "player 2 " & p2.player_name
   CPRINT 12, "Russian ruble: " & p1.score & "                     " & "Russian ruble: " & p2.score
   
   k = GETKEYS("rd" + CHR(27))
   IF k = CHR(27) THEN
      CLS
      CPRINT 15, "you quit the game"
      IF p1.score > p2.score THEN
         CPRINT 16, p1.player_name & " YOU WIN!"
      ELSE
         CPRINT 16, p2.player_name & " YOU WIN!"
      ENDIF
      SOUND("laugh.wav", 1)
      END
   ELSEIF k = "d" THEN
      CLS
      CPRINT 15, "another round of vodka!"
      SOUND("vodka.wav", 1)
   ELSEIF k = "r" THEN
      ROULETTE(gun(), p1, p2)
   ENDIF
   
   LOOP
   
   
END SUB

SUB opening(f AS STRING, t AS INTEGER, p1 AS player, p2 AS player)
   DIM k AS STRING
   DIM AS Integer hWave
   CPRINT 15, "Russian roulette"
   SLEEPEX()
   COLOR fbRed
   CLS
   CPRINT 15, "Russian roulette"
   fbs_Load_WAVFile(f , @hWave)
   fbs_Play_Wave(hWave , t)
   SLEEPEX()
   COLOR fbWhite
   fbs_Destroy_Wave(@hWave)
   CLS
   CPRINT 4, "this game is for two players"
   INPUT "player 1 enter your name: ", k
   p1.player_name = k
   INPUT "player 2 enter your name: ", k
   p2.player_name = k
   
END SUB

IF fbs_Init()=false then
  print "error: FBS_INIT() " & FBS_Get_PlugError()
  beep : sleep : end 1
end IF

SCREENRES scr_w, scr_h, 32
WIDTH scr_w \ 8, scr_h \ 16
DIM AS player p1, p2
OPENING("gun_shot.wav", 1, p1, p2)
MAIN(gun(), p1, p2)
SLEEP


full project in zip folder can be downloaded here https://www.dropbox.com/s/nht1n0nezjeyqbt/fb_Russian_roulette.zip?dl=0

Image
Image
Image

ron77

Return to “Game Dev”

Who is online

Users browsing this forum: No registered users and 1 guest