CHR$: Your Character Charmer
Need to add a dash of flair or a pinch of pizzazz to your text? Look no further than the CHR$
function! This nifty command is your gateway to the world of characters, allowing you to access any character in the C64's character set by its numeric code. Think of it as your secret decoder ring for ASCII and PETSCII characters!
Syntax
CHR$(<character code>)
Where:
- <character code>
: The numeric code (0-255) representing the character you want to generate.
Applications
The CHR$
function is indispensable when:
- You want to display special characters like hearts (♥), arrows (←), or pi (π).
- You're working with control characters to manipulate the screen or cursor.
- You're creating games or interfaces that require custom character graphics.
Code Examples
1. Simple Character Display:
10 PRINT CHR$(65) :rem Output: A
20 PRINT CHR$(97) :rem Output: red spade
This demonstrates how CHR$
converts numeric codes (65 and 97) into their corresponding letters.
2. Creating a smily face ☺:
10 PRINT CHR$(147) : rem Clear screen
20 PRINT CHR$(119); CHR$(119)
30 PRINT CHR$(106); CHR$(107)
By combining the characters represented by codes 1 and 2, we create a simple smiley face.
3. Custom Character Graphics:
10 PRINT CHR$(147) : rem Clear screen
20 FOR I=1 TO 25
30 READ C :rem Read character codes from DATA
40 PRINT CHR$(C); :rem Print the custom character
50 NEXT I
60 DATA 110,163,109,110,163,109,13
70 DATA 109,29,29,29,29,110,13
80 data 29,109,29,29,110,13
90 data 29,29,109,110,13
100 end
This snippet reads character codes from a DATA statement and uses them to create a custom graphic (a heart).
CHR$ in the Wild: The Pixel Art Playground
Imagine you're designing a retro-style platformer game. With CHR$
, you can craft unique sprites and backgrounds, giving your game a distinct look and feel. It's like having a pixel art studio built right into your code!
Don't let your text be boring and predictable! With the CHR$
function, you have the power to unlock the full range of characters at your fingertips. So go ahead and sprinkle your code with a little magic—your Commodore 64 will love you for it!