Mastering Sprite Movement on the Commodore 64: A Step-by-Step Guide
Are you ready to breathe life into your Commodore 64 game? In my previous installment, we met our protagonist, Dave, but now it's time to make him move across the screen. Buckle up as we dive into the intricacies of sprite movement using the C64 keyboard and joystick.
Understanding Sprite Movement Commands
To manipulate the position of our sprite, we employ the POKE commands, specifically:
POKE 53248, X
POKE 53249, Y
Where X and Y denote the coordinates on the screen, ranging from 0 to 255. But what if we want to position our sprite beyond this range? Enter the ninth bit of the horizontal position, managed by a separate register.
Mastering Advanced Sprite Positioning
Suppose we aim to move our sprite to coordinate X = 299. By leveraging the Windows 10 calculator in programmer mode, we deduce that the ninth bit is 1, while the remaining 8 bits correspond to 43. To achieve this, we manipulate two registers:
POKE 53248, 43 // First 8 bits of horizontal coordinate
POKE 53264, 1 // Ninth bit of horizontal coordinate
With this technique, our sprite effortlessly glides to its intended position.
Crafting the Basic Program for Sprite Movement
To streamline sprite movement, we've crafted a basic program compatible with both keyboard and joystick inputs. Here's a glimpse:
Just use your first program in which we created the Diamond Dave sprite and add these lines:
2000 rem initial position
2010 x = 150: y = 130
2020 print: print "move the sprite with a joystick in port 2"
2030 print "or via keyboard: q w, a s, n v, m b"
2040 print "He's alive..."
2050 print "Sprite movement isn't too hard..."
2060 print "Can't wait to see the final game"
2070 print
The sprite is positioned at the X and Y coordinates through the lines ranging from 2100 to 2210.
2100 rem position sprite
2110 if ox = x and oy = y then goto 2500
2120 if x < 24 then x = 24
2130 if x > 296 then x = 296
2140 if y < 50 then y = 50
2150 if y > 208 then y = 208
2160 if x > 255 then poke 53264, peek(53264) or 1: rem position x (high)
2170 if x <= 255 then poke 53264, peek(53264) and 254: rem position x (high)
2180 poke 53248, x and 255: rem position x (low)
2190 poke 53249, y: rem position y
2200 print "x ="; str$(x); ", y ="; str$(y); " "; chr$(145)
2210 ox = x: oy = y
the last line shows the actual x and y position on screen and is for informational purposes only.
From setting initial positions to interpreting user inputs, every line is meticulously designed to ensure smooth sprite navigation.
Now let's add some movement via keyboard and mouse
2500 rem main keyboard movement cycle
2510 get c$
2520 if c$ = "q" then y = y - 5: rem up
2540 if c$ = "a" then y = y + 5: rem down
2560 if c$ = "n" then x = x - 5: rem left
2580 if c$ = "m" then x = x + 5: rem right
2600 if c$ = "x" then end
2700 rem move the sprite with joystick in port 2
2710 a = peek(56320) or 16
2720 if a = 126 then y = y - 5: rem up
2730 if a = 122 then y = y - 3: x = x - 3: rem up left
2740 if a = 118 then y = y - 3: x = x + 3: rem up right
2750 if a = 123 then x = x - 5: rem left
2760 if a = 119 then x = x + 5: rem right
2770 if a = 125 then y = y + 5: rem down
2780 if a = 121 then y = y + 3: x = x - 3: rem down left
2790 if a = 117 then y = y + 3: x = x + 3: rem down right
2800 goto 2100: rem cycle until x is pressed
Testing Your Sprite Movement Program
Excited to witness your sprite in action? Simply paste the entire program into the VICE emulator or typing it in your real Commodore 64 or TheC64Maxi, ensuring the correct sequence of lines. Then, execute the program using the RUN command and marvel as your sprite responds to keyboard and joystick inputs.
Conclusion
With our comprehensive guide, you're now equipped to master sprite movement on the Commodore 64. Whether you're a seasoned programmer or a curious enthusiast, the thrill of bringing your game to life knows no bounds.
Experiment with different sprites, and unleash your creativity. The world of C64 game development awaits, and with Dave leading the charge, the adventure has only just begun. Happy coding!
Get Diamond Dave (C64)
Diamond Dave (C64)
A tribute to the 40th anniversary of Boulder Dash
Status | In development |
Author | TOBYTRONIC |
Genre | Puzzle |
Tags | 8-Bit, Arcade, Commodore 64, Exploration, Pixel Art, Retro |
More posts
- Journey into Game Development: Meeting Our Protagonist DaveFeb 02, 2024
- Building Diamond Dave: A Retro Game Developer's ToolkitFeb 02, 2024
- 10 PRINT "HELLO WORLD!"Feb 02, 2024
Leave a comment
Log in with itch.io to leave a comment.