Close

Code for the first game

A project log for The Moncky project

Creating a complete working retro 16-bit computer from scratch in the style of the C64, CPC 464 and alike using an FPGA

bigbatbigbat 07/04/2022 at 18:120 Comments

This is the Moncky code for the game I created as a proof of concept:

const CANON_WIDTH = 10
const CANON_HEIGHT = 10
const ALIEN_WIDTH = 4
const ALIEN_HEIGHT = 8
const BULLET_HEIGHT = 5

const LEFT_KEY = 0x16B
const RIGHT_KEY = 0x174
const SPACE = 0x29

var score : uint8 = 0
var canonX : uint16 = 160
var alienX : int16 = 4
var bulletX : int16 = 160
var bulletY : int16 = -1

fun drawScore() {
    call graphics_setCol(0xFF)
    call text_setCursor(0 0)
    call text_printString("score: ")
    call text_printHex(score)
}

fun drawCanon() {
    if ((canonX >= CANON_WIDTH) & (canonX <= (319 - CANON_WIDTH))) {
        call graphics_setCol(0xFF)
        call graphics_line((canonX - CANON_WIDTH) 199 canonX (199 - CANON_HEIGHT))
        call graphics_line(canonX (199 - CANON_HEIGHT) (canonX + CANON_WIDTH) 199)    }
}

fun drawAlien() {
    if ((alienX >= ALIEN_WIDTH) & (alienX <=(319 - ALIEN_WIDTH))) {
        call graphics_setCol(0x0F)
        call text_drawChar((alienX - ALIEN_WIDTH) 8 'O')
    }
}

fun drawBullet() {
    if ((bulletY >= BULLET_HEIGHT) & (bulletY < (199- BULLET_HEIGHT))) {
        call graphics_setCol(0xF0)
        call graphics_line(bulletX (bulletY - BULLET_HEIGHT) bulletX (bulletY + BULLET_HEIGHT))
    }
}

fun checkKeyCode(keycode: uint16): uint16 {
    if (keycode == LEFT_KEY) {
        let canonX = (canonX - 5)
        if (canonX < CANON_WIDTH) {
            let canonX = CANON_WIDTH
        }
    }
    if (keycode == RIGHT_KEY) {
        let canonX = (canonX + 5)
        if (canonX > (319 - CANON_WIDTH)) {
            let canonX = (319 - CANON_WIDTH)
        }
    }
    if (keycode == SPACE) {
        let bulletX = canonX
        let bulletY = ((199 - CANON_HEIGHT) - BULLET_HEIGHT)
    }
}

fun animateBullet() {
    if (bulletY > ((BULLET_HEIGHT + ALIEN_HEIGHT) + 8)) {
        let bulletY = (bulletY - 1)
    } else {
        if (bulletY > 0) {
            if ((bulletX >= (alienX - ALIEN_WIDTH)) & (bulletX <= (alienX + ALIEN_WIDTH))) {
                let score = (score + 1)
            }
            let bulletY = -1
        }
    }
}

fun animateAlien() {
    if (alienX >= ALIEN_WIDTH) {
        let alienX = (alienX + 1)
        if (alienX > (319 - ALIEN_WIDTH)) {
            let alienX = ALIEN_WIDTH
        }
    }
}

main
keycode : uint16
drawBuffer : uint8
{
    let drawBuffer = 0
    call graphics_setBgCol(0)
    while true {
        call graphics_setScreenBuffer((1 - drawBuffer))
        call graphics_setDrawBuffer(drawBuffer)
        call graphics_clrscr()
        call drawScore()
        call drawCanon()
        call drawAlien()
        call drawBullet()
        let keycode = call keyboard_readKeyCode()
        call checkKeyCode(keycode)
        call animateBullet()
        call animateAlien()
        let drawBuffer = (1 - drawBuffer)
        call util_enableInterrupts()
        call graphics_waitVertRetrace()
        call util_disableInterrupts()
    }
}

Discussions