Tiny Adventure 1micha.elok Member Posts: 176 Threads: 18 Joined: Nov 2023 Reputation: 0 02-16-2024, 10:17 AM (This post was last modified: 02-16-2024, 10:34 AM by 1micha.elok.) TINY ADVENTURE  Objective of the game :  Touch "Q" to finish the game  Controls  Movement : Left, Right  Jump    : Up  Hints :  "Q" will be displayed if you eat  the forbidden fruits "x" and reach score 20 Code:```'---------------- ' INITIALIZATION '---------------- '#win32 set window "Tiny Adventure",200,100,false,4 set redraw off player   = [] fruit    = [] q        = [] camera   = [] 'Initial Value randomize clock() player.x=width(primary)/2;player.y=height(primary)-15;player.img="P" player.dx=2;player.dy=5;player.jump = 0 for i = 0 to 19; fruit[i] = [x:rnd(-80,280),y:rnd(50,80),img:"x"];next q = [x:rnd(-80,280),y:rnd(20,50),img:"Q"] camera.x = 0 score = 0 '-------------- ' MAIN PROGRAM '-------------- do        set color 0,0,0 ; cls 'clear screen     Say(40,1,"Tiny Adventure")         'camera's position on top left while the initial player's position in the middle     camera.x = player.x - width(primary)/2     camera.y = player.y - height(primary)/2     'update mountain's position relative to the camera.x     mountain =[-100-camera.x,90,0-camera.x,50,100-camera.x,70,200-camera.x,50,300-camera.x,90]          'player's control        if keydown(KEY_LEFT) then         player.x = player.x - player.dx      elseif keydown(KEY_RIGHT) then         player.x = player.x + player.dx     elseif keydown(KEY_UP) then         player.y = player.y - player.dy; player.jump = 1 'jump     endif           'collision detection between player and fruits     for i = 0 to 19         if (player.x>= fruit[i].x-10-camera.x and player.x <= fruit[i].x+10-camera.x) and         (player.y >= fruit[i].y-10-camera.y and player.y <= fruit[i].y+10-camera.y) then             score = score + 1             fruit[i].y = -10 'out of screen         endif     next         'collision detection between player and "Q"     if (player.x>=q.x-10-camera.x and player.x<=q.x+10-camera.x) and     (player.y>=q.y-10-camera.y and player.y<=q.y+10-camera.y) then         Say(40,24,"Completed")         q.y = -10 'out of screen         redraw         do;wait 1;until keydown(KEY_RETURN)         end        endif         'if the player jumps then goes down slowly     if player.jump and player.y<=(height(primary)-15) then         player.y = player.y + 1         if player.y <=-4 then player.y = 0 'don't fly to outer space :-)     endif        'boundaries     if player.x <5 then 'left boundary         player.x = player.x+5          Say(5,40,"Rrr")     elseif player.x >width(primary)-5 then 'right boundary         player.x = player.x-5         Say(width(primary)-35,40,"Rrr")               endif     'draw blue mountains , green fruits , white player and red "Q"     set color 255,255,255; set caret width(primary)/2,12; center "Score :"+score     set color 0,0,70 ; draw poly mountain,1      set color 0,150,0         foreach i in fruit             set caret fruit[i].x-camera.x,fruit[i].y             wln fruit[i].img         next     if score >= 20 then         set color 150,0,0;set caret q.x-camera.x,q.y; wln q.img     endif     set color 255,255,255; set caret player.x,player.y; wln player.img     redraw     fwait 30 until keydown(KEY_ESCAPE) '----------- ' FUNCTIONS '----------- function Say(x,y,msg)     set color 255,0,0; set caret x,y; wln msg endfunc``` You are welcomed to try the game,  if you make some improvements on the game,  please share on this thread  so we can have fun together Thank you    johnno56 Senior Member Posts: 284 Threads: 30 Joined: Nov 2023 Reputation: 3 02-16-2024, 02:26 PM (This post was last modified: 02-16-2024, 02:27 PM by johnno56.) Certainly is a very good start... I have run the program several times and may have found a potential problem. I have noticed that if I hold down the jump key, so that the player stays at the top of the screen, the score will quite happily increase without collecting any "X"'s... for some reason, if the player's "y" position is less than 0, then the score increases... Quick fix for that may be to change line #43 to:  "elseif keydown(KEY_UP) and player.y > 0 then" I like the way the player has the ability to keep jumping... was than planned? The camera movement is very smooth. Nice job! I have added a simple enemy that will move backwards and forwards across the screen... at the moment a point is lost if it hits the player... but thinking about it... if the score drops then the game cannot finish... I didn't think that one through... lol It's almost 1:30am and I need to get some sleep. I will post my changes then.... Oh... and don't think I didn't spot the polygon hills... cool... See you tomorrow... Logic is the beginning of wisdom. aliensoldier Member Posts: 81 Threads: 16 Joined: Nov 2023 Reputation: 2 02-16-2024, 03:30 PM I think there is a small error in the game, before collecting all the items the game ends, I don't know if it was your intention johnno56 Senior Member Posts: 284 Threads: 30 Joined: Nov 2023 Reputation: 3 02-16-2024, 07:48 PM (This post was last modified: 02-16-2024, 07:50 PM by johnno56.) I think I may have found a fix for the premature "completed" problem... maybe I have added an "exist" field to "Q"'s array. Default = 0 (does not exist) When the required number of "fruit" is collected "q.exist" is set to 1 (does exist) Modified the "displaying of Q" only to happen if "Q" exists. I have played (tested) the game many times and the game runs as it should. (ignore the "enemy" sections... just tinkering... lol) Code:```'---------------- ' INITIALIZATION '---------------- '#win32 set window "Tiny Adventure",200,100,false,4 set redraw off player   = [] fruit    = [] q        = [] camera   = [] 'enemy    = [] 'Initial Value randomize clock() player.x=width(primary)/2;player.y=height(primary)-15;player.img="P" player.dx=2;player.dy=5;player.jump = 0 'enemy.x=width(primary);enemy.y=height(primary)-10;enemy.vel= -1;enemy.img="o" for i = 0 to 19; fruit[i] = [x:rnd(-80,280),y:rnd(50,80),img:"x"];next q = [x:rnd(-80,280),y:rnd(20,50),exist:0,img:"Q"] camera.x = 0 score = 0 '-------------- ' MAIN PROGRAM '-------------- do        set color 0,0,0 ; cls 'clear screen     Say(40,1,"Tiny Adventure")         'camera's position on top left while the initial player's position in the middle     camera.x = player.x - width(primary)/2     camera.y = player.y - height(primary)/2     'update mountain's position relative to the camera.x     mountain =[-100-camera.x,90,0-camera.x,50,100-camera.x,70,200-camera.x,50,300-camera.x,90]         'player's control        if keydown(KEY_LEFT) then         player.x = player.x - player.dx     elseif keydown(KEY_RIGHT) then         player.x = player.x + player.dx     elseif keydown(KEY_UP) and player.y > 32 then         player.y = player.y - player.dy; player.jump = 1 'jump     endif         set color 255, 128, 0     set caret 0, 0     wln width(player.img) 'player.y         'move enemy     'enemy.x=enemy.x+enemy.vel     'if enemy.x<=0   enemy.vel=1     'if enemy.x>=width(primary)  enemy.vel= -1         'collision detection between player and fruits     for i = 0 to 19         if (player.x>= fruit[i].x-10-camera.x and player.x <= fruit[i].x+10-camera.x) and         (player.y >= fruit[i].y-10-camera.y and player.y <= fruit[i].y+10-camera.y) then             score = score + 1             fruit[i].y = -10 'out of screen         endif     next         'collisiom between player and enemy     'if player.x>=enemy.x and player.x<=enemy.x+10 and     '    player.y>=enemy.y-10 and player.y<=enemy.y+10 and score>0 then     '    score = score - 1     '    enemy.x=width(primary)-10     'endif         'collision detection between player and "Q"     if (player.x>=q.x-10-camera.x and player.x<=q.x+10-camera.x) and     (player.y>=q.y-10-camera.y and player.y<=q.y+10-camera.y) and q.exist then         Say(40,24,"Completed")         'q.y = -10 'out of screen         q.exist = 0         'enemy.x = -10         redraw         do;wait 1;until keydown(KEY_RETURN)         end        endif         'if the player jumps then goes down slowly     if player.jump and player.y<=(height(primary)-15) then         player.y = player.y + 1         if player.y < 0 then player.y = 0 'don't fly to outer space :-)     endif        'boundaries     if player.x <5 then 'left boundary         player.x = player.x+5         Say(5,40,"Rrr")     elseif player.x >width(primary)-5 then 'right boundary         player.x = player.x-5         Say(width(primary)-35,40,"Rrr")              endif     'draw blue mountains , green fruits , white player and red "Q"     set color 255,255,255; set caret width(primary)/2,12; center "Score :"+score     set color 0,0,70 ; draw poly mountain,1     set color 0,150,0         foreach i in fruit             set caret fruit[i].x-camera.x,fruit[i].y             wln fruit[i].img         next     if score >= 20 then         q.exist = 1     endif     if q.exist         set color 150,0,0;set caret q.x-camera.x,q.y; wln q.img     endif     set color 255,255,255; set caret player.x,player.y; wln player.img     'set color 255,255,0; set caret enemy.x,enemy.y; wln enemy.img     redraw     fwait 30 until keydown(KEY_ESCAPE) '----------- ' FUNCTIONS '----------- function Say(x,y,msg)     set color 255,0,0; set caret x,y; wln msg endfunc``` I hope this helps... J (02-16-2024, 07:48 PM)johnno56 Wrote: I think I may have found a fix for the premature "completed" problem... maybe I have added an "exist" field to "Q"'s array. Default = 0 (does not exist) When the required number of "fruit" is collected "q.exist" is set to 1 (does exist) Modified the "displaying of Q" only to happen if "Q" exists. I have played (tested) the game many times and the game runs as it should. (ignore the "enemy" sections... just tinkering... lol) Code:```'---------------- ' INITIALIZATION '---------------- '#win32 set window "Tiny Adventure",200,100,false,4 set redraw off player   = [] fruit    = [] q        = [] camera   = [] 'enemy    = [] 'Initial Value randomize clock() player.x=width(primary)/2;player.y=height(primary)-15;player.img="P" player.dx=2;player.dy=5;player.jump = 0 'enemy.x=width(primary);enemy.y=height(primary)-10;enemy.vel= -1;enemy.img="o" for i = 0 to 19; fruit[i] = [x:rnd(-80,280),y:rnd(50,80),img:"x"];next q = [x:rnd(-80,280),y:rnd(20,50),exist:0,img:"Q"] camera.x = 0 score = 0 '-------------- ' MAIN PROGRAM '-------------- do       set color 0,0,0 ; cls 'clear screen     Say(40,1,"Tiny Adventure")        'camera's position on top left while the initial player's position in the middle     camera.x = player.x - width(primary)/2     camera.y = player.y - height(primary)/2     'update mountain's position relative to the camera.x     mountain =[-100-camera.x,90,0-camera.x,50,100-camera.x,70,200-camera.x,50,300-camera.x,90]         'player's control       if keydown(KEY_LEFT) then         player.x = player.x - player.dx     elseif keydown(KEY_RIGHT) then         player.x = player.x + player.dx     elseif keydown(KEY_UP) and player.y > 32 then         player.y = player.y - player.dy; player.jump = 1 'jump     endif         'set color 255, 128, 0     'set caret 0, 0     'wln player.y         'move enemy     'enemy.x=enemy.x+enemy.vel     'if enemy.x<=0   enemy.vel=1     'if enemy.x>=width(primary)  enemy.vel= -1        'collision detection between player and fruits     for i = 0 to 19         if (player.x>= fruit[i].x-10-camera.x and player.x <= fruit[i].x+10-camera.x) and         (player.y >= fruit[i].y-10-camera.y and player.y <= fruit[i].y+10-camera.y) then             score = score + 1             fruit[i].y = -10 'out of screen         endif     next         'collisiom between player and enemy     'if player.x>=enemy.x and player.x<=enemy.x+10 and     '    player.y>=enemy.y-10 and player.y<=enemy.y+10 and score>0 then     '    score = score - 1     '    enemy.x=width(primary)-10     'endif        'collision detection between player and "Q"     if (player.x>=q.x-10-camera.x and player.x<=q.x+10-camera.x) and     (player.y>=q.y-10-camera.y and player.y<=q.y+10-camera.y) and q.exist then         Say(40,24,"Completed")         'q.y = -10 'out of screen         q.exist = 0         'enemy.x = -10         redraw         do;wait 1;until keydown(KEY_RETURN)         end       endif        'if the player jumps then goes down slowly     if player.jump and player.y<=(height(primary)-15) then         player.y = player.y + 1         if player.y < 0 then player.y = 0 'don't fly to outer space :-)     endif       'boundaries     if player.x <5 then 'left boundary         player.x = player.x+5         Say(5,40,"Rrr")     elseif player.x >width(primary)-5 then 'right boundary         player.x = player.x-5         Say(width(primary)-35,40,"Rrr")              endif     'draw blue mountains , green fruits , white player and red "Q"     set color 255,255,255; set caret width(primary)/2,12; center "Score :"+score     set color 0,0,70 ; draw poly mountain,1     set color 0,150,0         foreach i in fruit             set caret fruit[i].x-camera.x,fruit[i].y             wln fruit[i].img         next     if score >= 20 then         q.exist = 1     endif     if q.exist         set color 150,0,0;set caret q.x-camera.x,q.y; wln q.img     endif     set color 255,255,255; set caret player.x,player.y; wln player.img     'set color 255,255,0; set caret enemy.x,enemy.y; wln enemy.img     redraw     fwait 30 until keydown(KEY_ESCAPE) '----------- ' FUNCTIONS '----------- function Say(x,y,msg)     set color 255,0,0; set caret x,y; wln msg endfunc``` I hope this helps... J Logic is the beginning of wisdom. Marcus Administrator Posts: 299 Threads: 36 Joined: Nov 2023 Reputation: 3 02-16-2024, 09:40 PM Love these small programs! Mind if I put it in the examples folder in the next release (with johnno's modification)? 1micha.elok Member Posts: 176 Threads: 18 Joined: Nov 2023 Reputation: 0 02-16-2024, 10:35 PM (This post was last modified: 02-16-2024, 10:50 PM by 1micha.elok.) @Marcus         You may put the Tiny Adventure in example folder in the next release.                        The Tiny Adventure is here because of your tips regarding how to use "camera" on Naalaa.                       I learn a lot from you and I'm glad to contribute to Naalaa    @Johnno         Thank you for spotting bugs and improving the Tiny Adventure's code. You are really helpful.                       The polygon hills are inspired by your "spaceship" on your "Space Race" game.                       At that time, I was amazed on how you used polygon to make a "spaceship".                       The "spaceship" is polygon too. It's nice, too. @aliensoldier    Thank you for spotting error, I believe the Tiny Adventure still need some enhancements. « Next Oldest | Next Newest »