Author Topic: Smooth Sprite Movement  (Read 3942 times)

0 Members and 1 Guest are viewing this topic.

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Smooth Sprite Movement
« on: August 04, 2019, 08:52:10 pm »
Hey everyone,
Heres a good starting point or example for anyone having problems with slow or jerky sprite movement.
Code: QB64: [Select]
  1. [/cod_TITLE "Space Invaders"
  2. SCREEN 13 '320x200
  3.  
  4. 'LOAD SPRITES
  5. DIM SHARED alien1%(1000)
  6. DIM SHARED alien2%(1000)
  7. DIM SHARED alien3%(1000)
  8.  
  9. DIM SHARED shield%(1000)
  10. DIM SHARED player%(1000)
  11.  
  12. TYPE Bullet
  13.     xPos AS INTEGER
  14.     yPos AS INTEGER
  15.     alive AS INTEGER
  16.  
  17. DIM SHARED bullets(100) AS Bullet
  18.  
  19. DIM SHARED deltaTime
  20. DIM SHARED playerSpeed
  21. DIM SHARED bulletSpeed
  22. DIM SHARED lastBulletTime
  23.  
  24.  
  25. CALL LoadSprite(alien1%())
  26. CALL LoadSprite(alien2%())
  27. CALL LoadSprite(alien3%())
  28.  
  29. CALL LoadSprite(shield%())
  30. CALL LoadSprite(player%())
  31.  
  32. playerSpeed = 100
  33. bulletSpeed = 100
  34.  
  35. FOR n = 1 TO 100
  36.     bullets(n).alive = 0
  37.  
  38. CALL DrawShields
  39.  
  40.     sTime = TIMER
  41.  
  42.     CALL MovePlayer
  43.     CALL SpawnBullet
  44.     CALL UpdateBullets
  45.  
  46.  
  47.     eTime = TIMER
  48.     deltaTime = eTime - sTime
  49.  
  50. SUB UpdateBullets
  51.     FOR n = 1 TO 100
  52.         IF bullets(n).alive = 1 THEN
  53.             LINE (bullets(n).xPos, bullets(n).yPos)-(bullets(n).xPos + 2, bullets(n).yPos + 4), _RGB(0, 0, 0), BF
  54.             bullets(n).yPos = bullets(n).yPos - (deltaTime * bulletSpeed)
  55.             LINE (bullets(n).xPos, bullets(n).yPos)-(bullets(n).xPos + 2, bullets(n).yPos + 4), _RGB(255, 0, 0), BF
  56.             _DISPLAY
  57.  
  58.             IF bullets(n).yPos < 0 THEN
  59.                 bullets(n).alive = 0
  60.                 LINE (bullets(n).xPos, bullets(n).yPos)-(bullets(n).xPos + 3, bullets(n).yPos + 5), _RGB(0, 0, 0), BF
  61.             END IF
  62.  
  63.         END IF
  64.     NEXT n
  65.  
  66. SUB SpawnBullet
  67.     IF _KEYDOWN(32) = -1 THEN
  68.         IF TIMER - lastBulletTime > 0.1 THEN
  69.             first = 101
  70.             FOR n = 100 TO 1 STEP -1
  71.                 IF NOT bullets(n).alive = 1 THEN
  72.                     IF n < first THEN
  73.                         first = n
  74.                     END IF
  75.                 END IF
  76.             NEXT n
  77.  
  78.             IF first < 101 THEN
  79.                 lastBulletTime = TIMER
  80.                 bullets(first).alive = 1
  81.                 bullets(first).xPos = pX + 5
  82.                 bullets(first).yPos = 170
  83.                 LINE (bullets(first).xPos, bullets(first).yPos)-(bullets(first).xPos + 2, bullets(first).yPos + 4), _RGB(255, 255, 255), BF
  84.             END IF
  85.         END IF
  86.     END IF
  87.  
  88. SUB MovePlayer
  89.     IF _KEYDOWN(100) = -1 THEN
  90.         pX = pX + (deltaTime * playerSpeed)
  91.     END IF
  92.     IF _KEYDOWN(97) = -1 THEN
  93.         pX = pX - (deltaTime * playerSpeed)
  94.     END IF
  95.  
  96.     IF pX < 20 THEN
  97.         pX = 20
  98.     END IF
  99.     IF pX > 300 THEN
  100.         pX = 300
  101.     END IF
  102.  
  103.     LINE (pX - 10, 175)-(pX + 100, 201), _RGB(0, 0, 0), BF
  104.     PUT (pX, 175), player%()
  105.     _DISPLAY
  106.  
  107. SUB DrawShields
  108.     FOR x = 0 TO 3
  109.         PUT (x * (320 / 4) + 30, 150), shield%()
  110.     NEXT x
  111.  
  112. SUB LoadSprite (sp%())
  113.     DIM sprite(4, 4)
  114.     scale = 2
  115.     FOR y = 0 TO 4
  116.         FOR x = 0 TO 4
  117.             READ sprite(x, y) 'Read the data
  118.         NEXT x
  119.     NEXT y
  120.  
  121.     FOR y = 0 TO 4 * scale STEP scale 'Factor in the scale
  122.         FOR x = 0 TO 4 * scale STEP scale
  123.             LINE (x, y)-(x + scale, y + scale), sprite(x / scale, y / scale), BF 'Draw each pixel
  124.         NEXT x
  125.     NEXT y
  126.  
  127.     GET (0, 0)-(10, 10), sp%()
  128.     CLS
  129.  
  130. 'Alien 1 Data
  131. DATA 0,0,15,0,0
  132. DATA 0,15,15,15,0
  133. DATA 0,15,15,15,0
  134. DATA 15,0,15,0,15
  135. DATA 0,15,0,15,0
  136.  
  137. 'Alien 2 Data
  138. DATA 0,0,15,0,0
  139. DATA 0,15,15,15,0
  140. DATA 0,15,0,15,0
  141. DATA 15,0,0,0,15
  142. DATA 15,0,0,0,15
  143.  
  144. 'Alien 3 Data
  145. DATA 15,0,0,0,15
  146. DATA 15,0,15,0,15
  147. DATA 0,15,0,15,0
  148. DATA 0,15,0,15,0
  149. DATA 0,0,15,0,0
  150.  
  151. 'Shield Data
  152. DATA 0,15,15,15,0
  153. DATA 15,15,15,15,15
  154. DATA 15,15,0,15,15
  155. DATA 15,0,0,0,15
  156. DATA 15,0,0,0,15
  157.  
  158. 'Player Data
  159. DATA 0,0,0,0,0
  160. DATA 0,0,15,0,0
  161. DATA 0,15,15,15,0
  162. DATA 15,15,15,15,15
  163. DATA 15,15,15,15,15
  164.  
  165. e]
  166.  
  167. Yes. It is for a space invaders clone im making.
  168.  
  169. Zep
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.