LAB 3 PART 1
For this lab we were tasked with creating a simple game or using open source code and adding some of our own code to it. The lab is based on the 6502 assembler and is written in assembly language, for this task I found a classic game of snake that is written in assembly and it is a little complex but I was able to follow it and make a few changes.
Green highlight is my code and edits, Blue code is important to user input and movements
Source material: https://skilldrick.github.io/easy6502/#snake
THE CODE
; ___ _ __ ___ __ ___
; / __|_ _ __ _| |_____ / /| __|/ \_ )
; \__ \ ' \/ _` | / / -_) _ \__ \ () / /
; |___/_||_\__,_|_\_\___\___/___/\__/___|
; Change direction: W A S D
define appleL $00 ; screen location of apple, low byte
define appleH $01 ; screen location of apple, high byte
define snakeHeadL $10 ; screen location of snake head, low byte
define snakeHeadH $11 ; screen location of snake head, high byte
define snakeBodyStart $12 ; start of snake body byte pairs
define snakeDirection $02 ; direction (possible values are below)
define snakeLength $03 ; snake length, in bytes
define snakeLengthTwo $04 ; snake length, in bytes
; Directions (each using a separate bit)
define movingUp 1
define movingRight 2
define movingDown 4
define movingLeft 8
; ASCII values of keys controlling the snake
define ASCII_i $69
define ASCII_j $6A
define ASCII_k $6B
define ASCII_l $6C
; System variables
define sysRandom $fe
define sysLastKey $ff
jsr init
jsr loop
init:
jsr initSnake
jsr generateApplePosition
rts
initSnake:
lda #movingLeft ;start direction
sta snakeDirection
lda #4 ;start length (6 segments)
sta snakeLength
sta snakeLengthTwo
lda #$11
sta snakeHeadL
lda #$10
sta snakeBodyStart
lda #$0f
sta $14 ; body segment 1
lda #$04
sta snakeHeadH
sta $13 ; body segment 1
sta $15 ; body segment 2
rts
generateApplePosition:
;load a new random byte into $00
lda sysRandom
sta appleL
;load a new random number from 2 to 5 into $01
lda sysRandom
and #$03 ;mask out lowest 2 bits
clc
adc #2
sta appleH
rts
Comments
Post a Comment