Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 20:37 23 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : USB Gamepad - Snake game

Author Message
ManiB
Senior Member

Joined: 12/10/2019
Location: Germany
Posts: 123
Posted: 09:57pm 27 Feb 2025
Copy link to clipboard 
Print this post

Hello, is my control of a gamepad in a simple SNAKE game okay?

' Snake game for PicoMITE with USB gamepad control
CLS
nothing$ = DEVICE(gamepad 3, raw) ' Baseline value with no buttons pressed

' Field and game variables
DIM field(20, 20)
snakeX = 10
snakeY = 10
DIM snake(100)
snakeLen = 1
snake(0) = snakeX : snake(1) = snakeY
direction = 1 ' 1=right, 2=down, 3=left, 4=up
foodX = INT(RND * 18) + 1
foodY = INT(RND * 18) + 1
score = 0
gameOver = 0

' Main game loop
DO
 b$ = DEVICE(gamepad 3, raw)
 IF LEN(b$) >= 2 THEN
   ' Check gamepad input based on raw byte values
   xAxis% = ASC(MID$(b$, 1, 1)) ' Byte 0: X-Axis (LEFT/RIGHT)
   yAxis% = ASC(MID$(b$, 2, 1)) ' Byte 1: Y-Axis (UP/DOWN)
   
   ' Map analog values to directions
   IF xAxis% = &HFF AND direction <> 3 THEN direction = 1 ' RIGHT (255)
   IF xAxis% = &H00 AND direction <> 1 THEN direction = 3 ' LEFT (0)
   IF yAxis% = &HFF AND direction <> 4 THEN direction = 2 ' DOWN (255)
   IF yAxis% = &H00 AND direction <> 2 THEN direction = 4 ' UP (0)
 ENDIF

 ' Move the snake
 oldX = snakeX : oldY = snakeY
 IF direction = 1 THEN snakeX = snakeX + 1
 IF direction = 2 THEN snakeY = snakeY + 1
 IF direction = 3 THEN snakeX = snakeX - 1
 IF direction = 4 THEN snakeY = snakeY - 1

 ' Check collision with walls
 IF snakeX < 0 OR snakeX > 19 OR snakeY < 0 OR snakeY > 19 THEN gameOver = 1

 ' Check collision with self
 FOR i = 0 TO snakeLen * 2 - 2 STEP 2
   IF snakeX = snake(i) AND snakeY = snake(i + 1) THEN gameOver = 1
 NEXT i

 ' Check if food is eaten
 IF snakeX = foodX AND snakeY = foodY THEN
   score = score + 1
   snakeLen = snakeLen + 1
   foodX = INT(RND * 18) + 1
   foodY = INT(RND * 18) + 1
 ENDIF

 ' Update snake positions
 FOR i = snakeLen * 2 - 2 TO 2 STEP -2
   snake(i) = snake(i - 2)
   snake(i + 1) = snake(i - 1)
 NEXT i
 snake(0) = snakeX
 snake(1) = snakeY

 ' Draw the game field
 CLS
 PRINT "Score: "; score
 FOR y = 0 TO 19
   FOR x = 0 TO 19
     found = 0
     FOR i = 0 TO snakeLen * 2 - 2 STEP 2
       IF snake(i) = x AND snake(i + 1) = y THEN
         PRINT "@";
         found = 1
         EXIT FOR
       ENDIF
     NEXT i
     IF found = 0 THEN
       IF x = foodX AND y = foodY THEN PRINT "*"; ELSE PRINT ".";
       ENDIF
     ENDIF
   NEXT x
   PRINT
 NEXT y

 ' Check for game over
 IF gameOver THEN
   PRINT "Game Over! Score: "; score
   EXIT DO
 ENDIF

 PAUSE 200 ' Game speed
LOOP

END
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2417
Posted: 11:24pm 27 Feb 2025
Copy link to clipboard 
Print this post

I trying to use inkey$
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025