QB64.org Forum

Active Forums => Programs => Topic started by: FellippeHeitor on February 24, 2020, 02:37:10 pm

Title: Chrome's dinosaur game prototype
Post by: FellippeHeitor on February 24, 2020, 02:37:10 pm
This guy writes a prototype of the dino game you find in Chrome's offline page:


The original is written in javascript, code at https://github.com/TylerPottsDev/chrome-dino-replica/blob/master/main.js, and here's the translation of that code to QB64:

Jump with the spacebar or up arrow, crouch/duck with left shift key or down arrow.

Code: QB64 $NOPREFIX: [Select]
  1.  
  2. CONST true = -1, false = NOT true
  3.  
  4. TYPE Actor
  5.     index AS LONG
  6.  
  7.     x AS SINGLE
  8.     y AS SINGLE
  9.     w AS SINGLE
  10.     h AS SINGLE
  11.     c AS UNSIGNED LONG
  12.  
  13.     dy AS SINGLE
  14.     dx AS SINGLE
  15.     jumpForce AS SINGLE
  16.     originalHeight AS SINGLE
  17.     grounded AS BYTE
  18.     jumpTimer AS SINGLE
  19.  
  20.     state AS BYTE
  21.  
  22.  
  23. DIM canvas AS LONG
  24. canvas = NEWIMAGE(800, 300, 32)
  25. SCREEN canvas
  26.  
  27. DIM SHARED gravity AS SINGLE, gameSpeed AS SINGLE
  28. DIM SHARED initialSpawnTimer AS SINGLE, spawnTimer AS SINGLE
  29. DIM SHARED gameOver AS BYTE
  30. initialSpawnTimer = 200
  31.  
  32.     gravity = 1
  33.     gameSpeed = 3
  34.     spawnTimer = initialSpawnTimer
  35.     score = 0
  36.     gameOver = false
  37.  
  38.     DIM SHARED player AS Actor
  39.     player.x = 100
  40.     player.y = 0
  41.     player.w = 50
  42.     player.h = 50
  43.     player.originalHeight = player.h
  44.     player.c = RGB32(255, 0, 0)
  45.     player.jumpForce = 10
  46.     player.grounded = false
  47.  
  48.     REDIM SHARED obstacles(100) AS Actor
  49.  
  50.     DO
  51.         update
  52.         DISPLAY
  53.         LIMIT 60
  54.     LOOP UNTIL gameOver
  55.  
  56. SUB update
  57.     DIM i AS LONG
  58.     LINE (0, 0)-(WIDTH, HEIGHT), RGB32(255, 200), BF
  59.  
  60.     spawnTimer = spawnTimer - 1
  61.     IF spawnTimer <= 0 THEN
  62.         spawnObstacle
  63.         spawnTimer = initialSpawnTimer - gameSpeed * 8
  64.  
  65.         IF spawnTimer < 60 THEN
  66.             spawnTimer = 60
  67.         END IF
  68.     END IF
  69.  
  70.     FOR i = 1 TO UBOUND(obstacles)
  71.         IF obstacles(i).state = true THEN
  72.             IF obstacles(i).x + obstacles(i).w < 0 THEN
  73.                 obstacles(i).state = false
  74.                 CONTINUE
  75.             END IF
  76.  
  77.             IF player.x < obstacles(i).x + obstacles(i).w AND _
  78.                player.x + player.w > obstacles(i).x AND _
  79.                player.y < obstacles(i).y + obstacles(i).h AND _
  80.                player.y + player.h > obstacles(i).y THEN
  81.                 gameOver = true
  82.                 EXIT SUB
  83.             END IF
  84.         END IF
  85.         updateObstacle i
  86.     NEXT
  87.  
  88.     animate player
  89.     score = score + 1
  90.  
  91.     DIM t AS STRING
  92.     t = "Score:" + STR$(score)
  93.     PRINTSTRING (25, 0), t
  94.  
  95.     IF score > highScore THEN
  96.         highScore = score
  97.     END IF
  98.  
  99.     t = "Highscore:" + STR$(highScore)
  100.     PRINTSTRING (WIDTH - 25 - PRINTWIDTH(t), 0), t
  101.  
  102.     gameSpeed = gameSpeed + .003
  103.  
  104. SUB updateObstacle (i AS LONG)
  105.     obstacles(i).x = obstacles(i).x + obstacles(i).dx
  106.     drawit obstacles(i)
  107.     obstacles(i).dx = -gameSpeed
  108.  
  109. SUB spawnObstacle
  110.     DIM i AS LONG, kind AS LONG, size AS LONG
  111.     FOR i = 1 TO UBOUND(obstacles)
  112.         IF obstacles(i).state = false THEN
  113.             obstacles(i).index = i
  114.             obstacles(i).state = true
  115.             size = rand(20, 70)
  116.             kind = rand(0, 1)
  117.  
  118.             obstacles(i).x = WIDTH + size
  119.             obstacles(i).y = HEIGHT - size
  120.             obstacles(i).w = size
  121.             obstacles(i).h = size
  122.             obstacles(i).c = RGB32(83, 50, 194)
  123.  
  124.             IF kind = 0 THEN
  125.                 obstacles(i).y = obstacles(i).y - player.originalHeight + 10
  126.             END IF
  127.             EXIT SUB
  128.         END IF
  129.     NEXT
  130.  
  131. FUNCTION rand& (min AS LONG, max AS LONG)
  132.     rand& = ROUND(RND * (max - min) + min)
  133.  
  134. SUB animate (this AS Actor)
  135.     IF KEYDOWN(32) OR KEYDOWN(18432) THEN 'space or up arrow to jump
  136.         jump this
  137.     ELSE
  138.         this.jumpTimer = 0
  139.     END IF
  140.  
  141.     IF KEYDOWN(100304) OR KEYDOWN(20480) THEN 'left-shift or down arrow to duck
  142.         this.h = this.originalHeight / 2
  143.     ELSE
  144.         this.h = this.originalHeight
  145.     END IF
  146.  
  147.     this.y = this.y + this.dy
  148.  
  149.     'gravity
  150.     IF this.y + this.h < HEIGHT THEN
  151.         this.dy = this.dy + gravity
  152.         this.grounded = false
  153.     ELSE
  154.         this.dy = 0
  155.         this.grounded = true
  156.         this.y = HEIGHT - this.h
  157.     END IF
  158.  
  159.     drawit this
  160.  
  161. SUB jump (this AS Actor)
  162.     IF this.grounded AND this.jumpTimer = 0 THEN
  163.         this.jumpTimer = 1
  164.         this.dy = -this.jumpForce
  165.     ELSEIF this.jumpTimer > 0 AND this.jumpTimer < 15 THEN
  166.         this.jumpTimer = this.jumpTimer + 1
  167.         this.dy = -this.jumpForce - (this.jumpTimer / 50)
  168.     END IF
  169.  
  170. SUB drawit (this AS Actor)
  171.     LINE (this.x, this.y)-STEP(this.w, this.h), this.c, BF

I tried to remain as faithful to the original as possible with QB64. Here's a screenshot:

Title: Re: Chrome's dinosaur game prototype
Post by: bplus on February 24, 2020, 02:50:17 pm
Cute little game.

I was thinking of Dodge ball after codevsb12 collision inquiry.
Title: Re: Chrome's dinosaur game prototype
Post by: Petr on February 24, 2020, 03:45:45 pm
Nice game!

  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Chrome's dinosaur game prototype
Post by: TempodiBasic on February 24, 2020, 04:24:25 pm
fine !
But I like more you game platform....because it has background and more graphic output.
Title: Re: Chrome's dinosaur game prototype
Post by: Ashish on February 25, 2020, 07:54:59 am
Good one.... I use to play it my school computer lab when teacher is not there...
Title: Re: Chrome's dinosaur game prototype
Post by: johnno56 on February 25, 2020, 06:02:34 pm
Very nice. Simple but annoyingly addictive... Well done!