mirror of
https://github.com/usatiuk/nand2tetris.git
synced 2025-10-29 00:27:49 +01:00
move interesting projects to the root
This commit is contained in:
200
Arkanoid/Ball.vm
Normal file
200
Arkanoid/Ball.vm
Normal file
@@ -0,0 +1,200 @@
|
||||
function Ball.new 0
|
||||
push constant 5
|
||||
call Memory.alloc 1
|
||||
pop pointer 0
|
||||
push argument 0
|
||||
pop this 0
|
||||
push argument 1
|
||||
pop this 1
|
||||
push argument 2
|
||||
pop this 2
|
||||
push argument 3
|
||||
pop this 3
|
||||
push argument 4
|
||||
pop this 4
|
||||
push pointer 0
|
||||
return
|
||||
function Ball.setPos 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Ball.erase 1
|
||||
pop temp 0
|
||||
push argument 1
|
||||
pop this 0
|
||||
push argument 2
|
||||
pop this 1
|
||||
push constant 0
|
||||
return
|
||||
function Ball.setSpeed 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push argument 1
|
||||
pop this 3
|
||||
push argument 2
|
||||
pop this 4
|
||||
push constant 0
|
||||
return
|
||||
function Ball.draw 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push constant 0
|
||||
not
|
||||
call Screen.setColor 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push this 1
|
||||
push this 2
|
||||
call Screen.drawCircle 3
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Ball.erase 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push constant 0
|
||||
call Screen.setColor 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push this 1
|
||||
push this 2
|
||||
call Screen.drawCircle 3
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Ball.update 5
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Ball.getLeft 1
|
||||
pop local 1
|
||||
push pointer 0
|
||||
call Ball.getRight 1
|
||||
pop local 2
|
||||
push pointer 0
|
||||
call Ball.getTop 1
|
||||
pop local 3
|
||||
push pointer 0
|
||||
call Ball.getBottom 1
|
||||
pop local 4
|
||||
push pointer 0
|
||||
call Ball.erase 1
|
||||
pop temp 0
|
||||
push local 1
|
||||
push constant 2
|
||||
lt
|
||||
push this 3
|
||||
and
|
||||
push constant 0
|
||||
lt
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push pointer 0
|
||||
call Ball.invertHspeed 1
|
||||
pop temp 0
|
||||
label IF_FALSE0
|
||||
push local 2
|
||||
push constant 508
|
||||
gt
|
||||
push this 3
|
||||
and
|
||||
push constant 0
|
||||
gt
|
||||
if-goto IF_TRUE1
|
||||
goto IF_FALSE1
|
||||
label IF_TRUE1
|
||||
push pointer 0
|
||||
call Ball.invertHspeed 1
|
||||
pop temp 0
|
||||
label IF_FALSE1
|
||||
push local 3
|
||||
push constant 2
|
||||
lt
|
||||
push this 4
|
||||
and
|
||||
push constant 0
|
||||
lt
|
||||
if-goto IF_TRUE2
|
||||
goto IF_FALSE2
|
||||
label IF_TRUE2
|
||||
push pointer 0
|
||||
call Ball.invertVspeed 1
|
||||
pop temp 0
|
||||
label IF_FALSE2
|
||||
push local 4
|
||||
push constant 250
|
||||
gt
|
||||
push this 4
|
||||
and
|
||||
push constant 0
|
||||
gt
|
||||
if-goto IF_TRUE3
|
||||
goto IF_FALSE3
|
||||
label IF_TRUE3
|
||||
push pointer 0
|
||||
call Ball.invertVspeed 1
|
||||
pop temp 0
|
||||
label IF_FALSE3
|
||||
push this 0
|
||||
push this 3
|
||||
add
|
||||
pop this 0
|
||||
push this 1
|
||||
push this 4
|
||||
add
|
||||
pop this 1
|
||||
push pointer 0
|
||||
call Ball.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Ball.invertVspeed 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
neg
|
||||
pop this 4
|
||||
push constant 0
|
||||
return
|
||||
function Ball.invertHspeed 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 3
|
||||
neg
|
||||
pop this 3
|
||||
push constant 0
|
||||
return
|
||||
function Ball.getLeft 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 0
|
||||
push this 2
|
||||
sub
|
||||
return
|
||||
function Ball.getRight 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 0
|
||||
push this 2
|
||||
add
|
||||
return
|
||||
function Ball.getTop 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 1
|
||||
push this 2
|
||||
sub
|
||||
return
|
||||
function Ball.getBottom 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 1
|
||||
push this 2
|
||||
add
|
||||
return
|
||||
function Ball.getVspeed 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
return
|
||||
581
Arkanoid/Game.vm
Normal file
581
Arkanoid/Game.vm
Normal file
@@ -0,0 +1,581 @@
|
||||
function Game.new 3
|
||||
push constant 6
|
||||
call Memory.alloc 1
|
||||
pop pointer 0
|
||||
push constant 0
|
||||
pop this 2
|
||||
push constant 8
|
||||
pop this 0
|
||||
push constant 3
|
||||
pop this 1
|
||||
push constant 250
|
||||
push constant 220
|
||||
push constant 50
|
||||
push constant 10
|
||||
call Rect.new 4
|
||||
pop this 3
|
||||
push constant 250
|
||||
push constant 200
|
||||
push constant 6
|
||||
push constant 1
|
||||
push constant 1
|
||||
call Ball.new 5
|
||||
pop this 4
|
||||
push this 0
|
||||
push this 1
|
||||
call Math.multiply 2
|
||||
call Array.new 1
|
||||
pop this 5
|
||||
label WHILE_EXP0
|
||||
push local 0
|
||||
push this 0
|
||||
lt
|
||||
not
|
||||
if-goto WHILE_END0
|
||||
label WHILE_EXP1
|
||||
push local 1
|
||||
push this 1
|
||||
lt
|
||||
not
|
||||
if-goto WHILE_END1
|
||||
push local 0
|
||||
push constant 3
|
||||
call Math.multiply 2
|
||||
push local 1
|
||||
add
|
||||
push this 5
|
||||
add
|
||||
push local 0
|
||||
push constant 60
|
||||
call Math.multiply 2
|
||||
push constant 20
|
||||
add
|
||||
push local 1
|
||||
push constant 30
|
||||
call Math.multiply 2
|
||||
push constant 20
|
||||
add
|
||||
push constant 50
|
||||
push constant 15
|
||||
call Rect.new 4
|
||||
pop temp 0
|
||||
pop pointer 1
|
||||
push temp 0
|
||||
pop that 0
|
||||
push local 0
|
||||
push constant 3
|
||||
call Math.multiply 2
|
||||
push local 1
|
||||
add
|
||||
push this 5
|
||||
add
|
||||
pop pointer 1
|
||||
push that 0
|
||||
pop local 2
|
||||
push local 2
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push local 1
|
||||
push constant 1
|
||||
add
|
||||
pop local 1
|
||||
goto WHILE_EXP1
|
||||
label WHILE_END1
|
||||
push constant 0
|
||||
pop local 1
|
||||
push local 0
|
||||
push constant 1
|
||||
add
|
||||
pop local 0
|
||||
goto WHILE_EXP0
|
||||
label WHILE_END0
|
||||
push pointer 0
|
||||
return
|
||||
function Game.loop 1
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
label WHILE_EXP0
|
||||
push constant 0
|
||||
not
|
||||
not
|
||||
if-goto WHILE_END0
|
||||
push constant 16
|
||||
call Sys.wait 1
|
||||
pop temp 0
|
||||
push this 2
|
||||
push constant 0
|
||||
eq
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push pointer 0
|
||||
call Game.movePaddle 1
|
||||
pop temp 0
|
||||
push this 4
|
||||
call Ball.update 1
|
||||
pop temp 0
|
||||
push pointer 0
|
||||
call Game.chackBallPaddleCol 1
|
||||
pop temp 0
|
||||
push pointer 0
|
||||
call Game.checkBallBricksCol 1
|
||||
pop temp 0
|
||||
push pointer 0
|
||||
call Game.checkGameOver 1
|
||||
pop temp 0
|
||||
goto IF_END0
|
||||
label IF_FALSE0
|
||||
call Keyboard.keyPressed 0
|
||||
pop local 0
|
||||
push local 0
|
||||
push constant 82
|
||||
eq
|
||||
if-goto IF_TRUE1
|
||||
goto IF_FALSE1
|
||||
label IF_TRUE1
|
||||
push pointer 0
|
||||
call Game.resetGame 1
|
||||
pop temp 0
|
||||
label IF_FALSE1
|
||||
label IF_END0
|
||||
goto WHILE_EXP0
|
||||
label WHILE_END0
|
||||
push constant 0
|
||||
return
|
||||
function Game.resetGame 3
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
call Screen.clearScreen 0
|
||||
pop temp 0
|
||||
push this 3
|
||||
push constant 250
|
||||
push constant 220
|
||||
call Rect.setPos 3
|
||||
pop temp 0
|
||||
push this 4
|
||||
push constant 250
|
||||
push constant 200
|
||||
call Ball.setPos 3
|
||||
pop temp 0
|
||||
push this 4
|
||||
push constant 1
|
||||
push constant 1
|
||||
call Ball.setSpeed 3
|
||||
pop temp 0
|
||||
push this 0
|
||||
push this 1
|
||||
call Math.multiply 2
|
||||
pop local 1
|
||||
label WHILE_EXP0
|
||||
push local 0
|
||||
push local 1
|
||||
lt
|
||||
not
|
||||
if-goto WHILE_END0
|
||||
push local 0
|
||||
push this 5
|
||||
add
|
||||
pop pointer 1
|
||||
push that 0
|
||||
pop local 2
|
||||
push local 2
|
||||
push constant 0
|
||||
call Rect.setHidden 2
|
||||
pop temp 0
|
||||
push local 2
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push local 0
|
||||
push constant 1
|
||||
add
|
||||
pop local 0
|
||||
goto WHILE_EXP0
|
||||
label WHILE_END0
|
||||
push constant 0
|
||||
pop this 2
|
||||
push constant 0
|
||||
return
|
||||
function Game.checkGameOver 1
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
call Ball.getBottom 1
|
||||
pop local 0
|
||||
push local 0
|
||||
push constant 240
|
||||
gt
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push constant 1
|
||||
pop this 2
|
||||
push constant 0
|
||||
push constant 0
|
||||
call Output.moveCursor 2
|
||||
pop temp 0
|
||||
call Output.println 0
|
||||
pop temp 0
|
||||
push constant 9
|
||||
call String.new 1
|
||||
push constant 103
|
||||
call String.appendChar 2
|
||||
push constant 97
|
||||
call String.appendChar 2
|
||||
push constant 109
|
||||
call String.appendChar 2
|
||||
push constant 101
|
||||
call String.appendChar 2
|
||||
push constant 32
|
||||
call String.appendChar 2
|
||||
push constant 111
|
||||
call String.appendChar 2
|
||||
push constant 118
|
||||
call String.appendChar 2
|
||||
push constant 101
|
||||
call String.appendChar 2
|
||||
push constant 114
|
||||
call String.appendChar 2
|
||||
call Output.printString 1
|
||||
pop temp 0
|
||||
call Output.println 0
|
||||
pop temp 0
|
||||
push constant 18
|
||||
call String.new 1
|
||||
push constant 112
|
||||
call String.appendChar 2
|
||||
push constant 114
|
||||
call String.appendChar 2
|
||||
push constant 101
|
||||
call String.appendChar 2
|
||||
push constant 115
|
||||
call String.appendChar 2
|
||||
push constant 115
|
||||
call String.appendChar 2
|
||||
push constant 32
|
||||
call String.appendChar 2
|
||||
push constant 114
|
||||
call String.appendChar 2
|
||||
push constant 32
|
||||
call String.appendChar 2
|
||||
push constant 116
|
||||
call String.appendChar 2
|
||||
push constant 111
|
||||
call String.appendChar 2
|
||||
push constant 32
|
||||
call String.appendChar 2
|
||||
push constant 114
|
||||
call String.appendChar 2
|
||||
push constant 101
|
||||
call String.appendChar 2
|
||||
push constant 115
|
||||
call String.appendChar 2
|
||||
push constant 116
|
||||
call String.appendChar 2
|
||||
push constant 97
|
||||
call String.appendChar 2
|
||||
push constant 114
|
||||
call String.appendChar 2
|
||||
push constant 116
|
||||
call String.appendChar 2
|
||||
call Output.printString 1
|
||||
pop temp 0
|
||||
label IF_FALSE0
|
||||
push constant 0
|
||||
return
|
||||
function Game.movePaddle 3
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
call Keyboard.keyPressed 0
|
||||
pop local 0
|
||||
push this 3
|
||||
call Rect.getLeft 1
|
||||
pop local 1
|
||||
push this 3
|
||||
call Rect.getRight 1
|
||||
pop local 2
|
||||
push local 0
|
||||
push constant 130
|
||||
eq
|
||||
push local 1
|
||||
push constant 0
|
||||
gt
|
||||
and
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push this 3
|
||||
push constant 2
|
||||
call Rect.moveLeft 2
|
||||
pop temp 0
|
||||
label IF_FALSE0
|
||||
push local 0
|
||||
push constant 132
|
||||
eq
|
||||
push local 2
|
||||
push constant 510
|
||||
lt
|
||||
and
|
||||
if-goto IF_TRUE1
|
||||
goto IF_FALSE1
|
||||
label IF_TRUE1
|
||||
push this 3
|
||||
push constant 2
|
||||
call Rect.moveRight 2
|
||||
pop temp 0
|
||||
label IF_FALSE1
|
||||
push this 3
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Game.chackBallPaddleCol 11
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 3
|
||||
call Rect.getLeft 1
|
||||
pop local 0
|
||||
push this 3
|
||||
call Rect.getRight 1
|
||||
pop local 1
|
||||
push this 3
|
||||
call Rect.getTop 1
|
||||
pop local 2
|
||||
push this 3
|
||||
call Rect.getBottom 1
|
||||
pop local 3
|
||||
push local 1
|
||||
push local 0
|
||||
add
|
||||
push constant 2
|
||||
call Math.divide 2
|
||||
pop local 4
|
||||
push this 4
|
||||
call Ball.getLeft 1
|
||||
pop local 5
|
||||
push this 4
|
||||
call Ball.getRight 1
|
||||
pop local 6
|
||||
push this 4
|
||||
call Ball.getTop 1
|
||||
pop local 7
|
||||
push this 4
|
||||
call Ball.getBottom 1
|
||||
pop local 8
|
||||
push local 6
|
||||
push local 5
|
||||
add
|
||||
push constant 2
|
||||
call Math.divide 2
|
||||
pop local 9
|
||||
push this 4
|
||||
call Ball.getVspeed 1
|
||||
pop local 10
|
||||
push local 5
|
||||
push local 0
|
||||
gt
|
||||
push local 6
|
||||
push local 1
|
||||
lt
|
||||
and
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push local 8
|
||||
push local 2
|
||||
gt
|
||||
push local 10
|
||||
push constant 0
|
||||
gt
|
||||
and
|
||||
if-goto IF_TRUE1
|
||||
goto IF_FALSE1
|
||||
label IF_TRUE1
|
||||
push local 9
|
||||
push local 4
|
||||
gt
|
||||
if-goto IF_TRUE2
|
||||
goto IF_FALSE2
|
||||
label IF_TRUE2
|
||||
push this 4
|
||||
push constant 1
|
||||
push constant 1
|
||||
neg
|
||||
call Ball.setSpeed 3
|
||||
pop temp 0
|
||||
goto IF_END2
|
||||
label IF_FALSE2
|
||||
push this 4
|
||||
push constant 1
|
||||
neg
|
||||
push constant 1
|
||||
neg
|
||||
call Ball.setSpeed 3
|
||||
pop temp 0
|
||||
label IF_END2
|
||||
label IF_FALSE1
|
||||
label IF_FALSE0
|
||||
push constant 0
|
||||
return
|
||||
function Game.checkBallBricksCol 21
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
call Ball.getLeft 1
|
||||
pop local 5
|
||||
push this 4
|
||||
call Ball.getRight 1
|
||||
pop local 6
|
||||
push this 4
|
||||
call Ball.getTop 1
|
||||
pop local 7
|
||||
push this 4
|
||||
call Ball.getBottom 1
|
||||
pop local 8
|
||||
push this 4
|
||||
call Ball.getVspeed 1
|
||||
pop local 9
|
||||
push this 0
|
||||
push this 1
|
||||
call Math.multiply 2
|
||||
pop local 19
|
||||
label WHILE_EXP0
|
||||
push local 18
|
||||
push local 19
|
||||
lt
|
||||
not
|
||||
if-goto WHILE_END0
|
||||
push local 18
|
||||
push this 5
|
||||
add
|
||||
pop pointer 1
|
||||
push that 0
|
||||
pop local 20
|
||||
push local 20
|
||||
call Rect.getLeft 1
|
||||
pop local 0
|
||||
push local 20
|
||||
call Rect.getRight 1
|
||||
pop local 1
|
||||
push local 20
|
||||
call Rect.getTop 1
|
||||
pop local 2
|
||||
push local 20
|
||||
call Rect.getBottom 1
|
||||
pop local 3
|
||||
push local 20
|
||||
call Rect.getHidden 1
|
||||
pop local 4
|
||||
push local 4
|
||||
push constant 1
|
||||
lt
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push local 20
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push local 5
|
||||
push local 0
|
||||
gt
|
||||
push local 6
|
||||
push local 1
|
||||
lt
|
||||
and
|
||||
if-goto IF_TRUE1
|
||||
goto IF_FALSE1
|
||||
label IF_TRUE1
|
||||
push local 8
|
||||
push local 2
|
||||
gt
|
||||
push local 7
|
||||
push local 3
|
||||
lt
|
||||
and
|
||||
if-goto IF_TRUE2
|
||||
goto IF_FALSE2
|
||||
label IF_TRUE2
|
||||
push local 20
|
||||
push constant 1
|
||||
call Rect.setHidden 2
|
||||
pop temp 0
|
||||
push local 20
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push local 6
|
||||
push local 0
|
||||
sub
|
||||
pop local 10
|
||||
push local 1
|
||||
push local 5
|
||||
sub
|
||||
pop local 11
|
||||
push local 8
|
||||
push local 2
|
||||
sub
|
||||
pop local 12
|
||||
push local 3
|
||||
push local 7
|
||||
sub
|
||||
pop local 13
|
||||
push local 10
|
||||
call Util.abs 1
|
||||
push local 11
|
||||
call Util.abs 1
|
||||
lt
|
||||
if-goto IF_TRUE3
|
||||
goto IF_FALSE3
|
||||
label IF_TRUE3
|
||||
push constant 1
|
||||
pop local 14
|
||||
push local 10
|
||||
pop local 16
|
||||
goto IF_END3
|
||||
label IF_FALSE3
|
||||
push local 11
|
||||
pop local 16
|
||||
label IF_END3
|
||||
push local 12
|
||||
call Util.abs 1
|
||||
push local 13
|
||||
call Util.abs 1
|
||||
lt
|
||||
if-goto IF_TRUE4
|
||||
goto IF_FALSE4
|
||||
label IF_TRUE4
|
||||
push constant 1
|
||||
pop local 15
|
||||
push local 12
|
||||
pop local 17
|
||||
goto IF_END4
|
||||
label IF_FALSE4
|
||||
push local 13
|
||||
pop local 17
|
||||
label IF_END4
|
||||
push local 16
|
||||
call Util.abs 1
|
||||
push local 17
|
||||
call Util.abs 1
|
||||
lt
|
||||
if-goto IF_TRUE5
|
||||
goto IF_FALSE5
|
||||
label IF_TRUE5
|
||||
push this 4
|
||||
call Ball.invertHspeed 1
|
||||
pop temp 0
|
||||
goto IF_END5
|
||||
label IF_FALSE5
|
||||
push this 4
|
||||
call Ball.invertVspeed 1
|
||||
pop temp 0
|
||||
label IF_END5
|
||||
label IF_FALSE2
|
||||
label IF_FALSE1
|
||||
label IF_FALSE0
|
||||
push local 18
|
||||
push constant 1
|
||||
add
|
||||
pop local 18
|
||||
goto WHILE_EXP0
|
||||
label WHILE_END0
|
||||
push constant 0
|
||||
return
|
||||
8
Arkanoid/Main.vm
Normal file
8
Arkanoid/Main.vm
Normal file
@@ -0,0 +1,8 @@
|
||||
function Main.main 1
|
||||
call Game.new 0
|
||||
pop local 0
|
||||
push local 0
|
||||
call Game.loop 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
173
Arkanoid/Rect.vm
Normal file
173
Arkanoid/Rect.vm
Normal file
@@ -0,0 +1,173 @@
|
||||
function Rect.new 0
|
||||
push constant 5
|
||||
call Memory.alloc 1
|
||||
pop pointer 0
|
||||
push argument 0
|
||||
pop this 0
|
||||
push argument 1
|
||||
pop this 1
|
||||
push argument 2
|
||||
pop this 2
|
||||
push argument 3
|
||||
pop this 3
|
||||
push constant 0
|
||||
pop this 4
|
||||
push pointer 0
|
||||
return
|
||||
function Rect.setPos 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push argument 1
|
||||
pop this 0
|
||||
push argument 2
|
||||
pop this 1
|
||||
push constant 0
|
||||
return
|
||||
function Rect.draw 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
push constant 1
|
||||
eq
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
label IF_FALSE0
|
||||
push constant 0
|
||||
not
|
||||
call Screen.setColor 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push this 1
|
||||
push this 0
|
||||
push this 2
|
||||
add
|
||||
push this 1
|
||||
push this 3
|
||||
add
|
||||
call Screen.drawRectangle 4
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.erase 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push constant 0
|
||||
call Screen.setColor 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push this 1
|
||||
push this 0
|
||||
push this 2
|
||||
add
|
||||
push this 1
|
||||
push this 3
|
||||
add
|
||||
call Screen.drawRectangle 4
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.moveLeft 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push argument 1
|
||||
sub
|
||||
pop this 0
|
||||
push pointer 0
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.moveRight 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push this 0
|
||||
push argument 1
|
||||
add
|
||||
pop this 0
|
||||
push pointer 0
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.moveUp 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push this 1
|
||||
push argument 1
|
||||
sub
|
||||
pop this 1
|
||||
push pointer 0
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.moveDown 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push pointer 0
|
||||
call Rect.erase 1
|
||||
pop temp 0
|
||||
push this 1
|
||||
push argument 1
|
||||
add
|
||||
pop this 1
|
||||
push pointer 0
|
||||
call Rect.draw 1
|
||||
pop temp 0
|
||||
push constant 0
|
||||
return
|
||||
function Rect.getLeft 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 0
|
||||
return
|
||||
function Rect.getRight 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 0
|
||||
push this 2
|
||||
add
|
||||
return
|
||||
function Rect.getTop 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 1
|
||||
return
|
||||
function Rect.getBottom 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 1
|
||||
push this 3
|
||||
add
|
||||
return
|
||||
function Rect.getHidden 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push this 4
|
||||
return
|
||||
function Rect.setHidden 0
|
||||
push argument 0
|
||||
pop pointer 0
|
||||
push argument 1
|
||||
pop this 4
|
||||
push constant 0
|
||||
return
|
||||
15
Arkanoid/Util.vm
Normal file
15
Arkanoid/Util.vm
Normal file
@@ -0,0 +1,15 @@
|
||||
function Util.abs 0
|
||||
push argument 0
|
||||
push constant 0
|
||||
lt
|
||||
if-goto IF_TRUE0
|
||||
goto IF_FALSE0
|
||||
label IF_TRUE0
|
||||
push argument 0
|
||||
neg
|
||||
return
|
||||
goto IF_END0
|
||||
label IF_FALSE0
|
||||
push argument 0
|
||||
return
|
||||
label IF_END0
|
||||
104
Arkanoid/source/Ball.jack
Normal file
104
Arkanoid/source/Ball.jack
Normal file
@@ -0,0 +1,104 @@
|
||||
class Ball {
|
||||
field int x,y, r, hspeed, vspeed;
|
||||
|
||||
constructor Ball new(int lx, int ly, int lr, int lhspeed, int lvspeed) {
|
||||
let x = lx;
|
||||
let y = ly;
|
||||
let r = lr;
|
||||
|
||||
let hspeed = lhspeed;
|
||||
let vspeed = lvspeed;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
method void setPos(int lx, int ly) {
|
||||
do erase();
|
||||
|
||||
let x = lx;
|
||||
let y = ly;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void setSpeed(int lhspeed, int lvspeed) {
|
||||
let hspeed = lhspeed;
|
||||
let vspeed = lvspeed;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void draw() {
|
||||
do Screen.setColor(true);
|
||||
do Screen.drawCircle(x,y,r);
|
||||
return;
|
||||
}
|
||||
|
||||
method void erase() {
|
||||
do Screen.setColor(false);
|
||||
do Screen.drawCircle(x,y,r);
|
||||
return;
|
||||
}
|
||||
|
||||
method void update() {
|
||||
var int keyPressed, bLeft, bRight, bTop, bBottom;
|
||||
|
||||
let bLeft = getLeft();
|
||||
let bRight = getRight();
|
||||
let bTop = getTop();
|
||||
let bBottom = getBottom();
|
||||
|
||||
do erase();
|
||||
|
||||
// 2 pixel margin on the sides just in case
|
||||
if (bLeft < 2 & hspeed < 0) {
|
||||
do invertHspeed();
|
||||
}
|
||||
if (bRight > 508 & hspeed > 0) {
|
||||
do invertHspeed();
|
||||
}
|
||||
if (bTop < 2 & vspeed < 0) {
|
||||
do invertVspeed();
|
||||
}
|
||||
if (bBottom > 250 & vspeed > 0) {
|
||||
do invertVspeed();
|
||||
}
|
||||
|
||||
let x = x + hspeed;
|
||||
let y = y + vspeed;
|
||||
|
||||
do draw();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void invertVspeed() {
|
||||
let vspeed = -vspeed;
|
||||
return;
|
||||
}
|
||||
|
||||
method void invertHspeed() {
|
||||
let hspeed = -hspeed;
|
||||
return;
|
||||
}
|
||||
|
||||
method int getLeft() {
|
||||
return x - r;
|
||||
}
|
||||
|
||||
method int getRight() {
|
||||
return x + r;
|
||||
}
|
||||
|
||||
method int getTop() {
|
||||
return y - r;
|
||||
}
|
||||
|
||||
method int getBottom() {
|
||||
return y + r;
|
||||
}
|
||||
|
||||
method int getVspeed() {
|
||||
return vspeed;
|
||||
}
|
||||
}
|
||||
224
Arkanoid/source/Game.jack
Normal file
224
Arkanoid/source/Game.jack
Normal file
@@ -0,0 +1,224 @@
|
||||
class Game {
|
||||
field int hbricks, vbricks;
|
||||
field int gameOver;
|
||||
|
||||
field Rect paddle;
|
||||
field Ball ball;
|
||||
field Array bricks;
|
||||
|
||||
constructor Game new() {
|
||||
var int i, j;
|
||||
var Rect curBrick;
|
||||
|
||||
let gameOver = 0;
|
||||
let hbricks = 8;
|
||||
let vbricks = 3;
|
||||
|
||||
let paddle = Rect.new(250,220,50,10);
|
||||
let ball = Ball.new(250, 200, 6, 1, 1);
|
||||
let bricks = Array.new(hbricks * vbricks);
|
||||
|
||||
while (i < hbricks) {
|
||||
while (j < vbricks) {
|
||||
let bricks[i * 3 + j] = Rect.new(i * 60 + 20, j * 30 + 20, 50, 15);
|
||||
let curBrick = bricks[i * 3 + j];
|
||||
do curBrick.draw();
|
||||
let j = j + 1;
|
||||
}
|
||||
let j = 0;
|
||||
let i = i + 1;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
method void loop() {
|
||||
var int keyPressed;
|
||||
|
||||
while(true) {
|
||||
// Approximately 60 updates per second
|
||||
do Sys.wait(16);
|
||||
|
||||
if (gameOver = 0) {
|
||||
do movePaddle();
|
||||
do ball.update();
|
||||
do chackBallPaddleCol();
|
||||
do checkBallBricksCol();
|
||||
|
||||
do checkGameOver();
|
||||
} else {
|
||||
let keyPressed = Keyboard.keyPressed();
|
||||
if (keyPressed = 82) {
|
||||
do resetGame();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
method void resetGame() {
|
||||
var int i, end;
|
||||
var Rect brick;
|
||||
|
||||
do Screen.clearScreen();
|
||||
|
||||
do paddle.setPos(250, 220);
|
||||
do ball.setPos(250, 200);
|
||||
do ball.setSpeed(1, 1);
|
||||
|
||||
let end = hbricks * vbricks;
|
||||
while (i < end) {
|
||||
let brick = bricks[i];
|
||||
|
||||
do brick.setHidden(0);
|
||||
do brick.draw();
|
||||
|
||||
let i = i + 1;
|
||||
}
|
||||
|
||||
let gameOver = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void checkGameOver() {
|
||||
var int bBottom;
|
||||
|
||||
let bBottom = ball.getBottom();
|
||||
|
||||
if (bBottom > 240) {
|
||||
let gameOver = 1;
|
||||
|
||||
do Output.moveCursor(0,0);
|
||||
do Output.println();
|
||||
do Output.printString("game over");
|
||||
do Output.println();
|
||||
do Output.printString("press r to restart");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void movePaddle() {
|
||||
var int keyPressed, paddleLeft, paddleRight;
|
||||
|
||||
let keyPressed = Keyboard.keyPressed();
|
||||
let paddleLeft = paddle.getLeft();
|
||||
let paddleRight = paddle.getRight();
|
||||
// Move left
|
||||
if((keyPressed = 130) & (paddleLeft > 0)) {
|
||||
do paddle.moveLeft(2);
|
||||
}
|
||||
// Move right
|
||||
if((keyPressed = 132) & (paddleRight < 510)) {
|
||||
do paddle.moveRight(2);
|
||||
}
|
||||
do paddle.draw();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void chackBallPaddleCol() {
|
||||
var int paddleLeft, paddleRight, paddleTop, paddleBottom, paddleMiddle;
|
||||
var int bLeft, bRight, bTop, bBottom, bMiddle, bVspeed;
|
||||
|
||||
let paddleLeft = paddle.getLeft();
|
||||
let paddleRight = paddle.getRight();
|
||||
let paddleTop = paddle.getTop();
|
||||
let paddleBottom = paddle.getBottom();
|
||||
let paddleMiddle = (paddleRight + paddleLeft) / 2;
|
||||
|
||||
let bLeft = ball.getLeft();
|
||||
let bRight = ball.getRight();
|
||||
let bTop = ball.getTop();
|
||||
let bBottom = ball.getBottom();
|
||||
let bMiddle = (bRight + bLeft) / 2;
|
||||
let bVspeed = ball.getVspeed();
|
||||
|
||||
if((bLeft > paddleLeft) & (bRight < paddleRight)) {
|
||||
if((bBottom > paddleTop) & (bVspeed > 0)) {
|
||||
if (bMiddle > paddleMiddle) {
|
||||
do ball.setSpeed(1, -1);
|
||||
} else {
|
||||
do ball.setSpeed(-1, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void checkBallBricksCol() {
|
||||
var int brLeft, brRight, brTop, brBottom, brHidden;
|
||||
var int bLeft, bRight, bTop, bBottom, bVspeed;
|
||||
var int overlapLeft, overlapRight, overlapTop, overlapBottom, fromLeft, fromTop;
|
||||
var int minOverlapX, minOverlapY;
|
||||
var int i, end;
|
||||
var Rect brick;
|
||||
|
||||
let bLeft = ball.getLeft();
|
||||
let bRight = ball.getRight();
|
||||
let bTop = ball.getTop();
|
||||
let bBottom = ball.getBottom();
|
||||
let bVspeed = ball.getVspeed();
|
||||
|
||||
let end = hbricks * vbricks;
|
||||
while (i < end) {
|
||||
let brick = bricks[i];
|
||||
|
||||
let brLeft = brick.getLeft();
|
||||
let brRight = brick.getRight();
|
||||
let brTop = brick.getTop();
|
||||
let brBottom = brick.getBottom();
|
||||
let brHidden = brick.getHidden();
|
||||
|
||||
if(brHidden < 1) {
|
||||
do brick.draw();
|
||||
|
||||
// Check if there's intersection at all first
|
||||
if((bLeft > brLeft) & (bRight < brRight)) {
|
||||
if((bBottom > brTop) & (bTop < brBottom)) {
|
||||
do brick.setHidden(1);
|
||||
do brick.draw();
|
||||
|
||||
// Now calculate the collision side
|
||||
// Algorithm basically copied from this video
|
||||
// https://youtu.be/_4K3tsKa1Uc?t=1820
|
||||
|
||||
let overlapLeft = bRight - brLeft;
|
||||
let overlapRight = brRight - bLeft;
|
||||
let overlapTop = bBottom - brTop;
|
||||
let overlapBottom = brBottom - bTop;
|
||||
|
||||
if(Util.abs(overlapLeft) < Util.abs(overlapRight)) {
|
||||
let fromLeft = 1;
|
||||
let minOverlapX = overlapLeft;
|
||||
} else {
|
||||
let minOverlapX = overlapRight;
|
||||
}
|
||||
|
||||
if(Util.abs(overlapTop) < Util.abs(overlapBottom)) {
|
||||
let fromTop = 1;
|
||||
let minOverlapY = overlapTop;
|
||||
} else {
|
||||
let minOverlapY = overlapBottom;
|
||||
}
|
||||
|
||||
if(Util.abs(minOverlapX) < Util.abs(minOverlapY)) {
|
||||
do ball.invertHspeed();
|
||||
} else {
|
||||
do ball.invertVspeed();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let i = i + 1;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
8
Arkanoid/source/Main.jack
Normal file
8
Arkanoid/source/Main.jack
Normal file
@@ -0,0 +1,8 @@
|
||||
class Main {
|
||||
function void main() {
|
||||
var Game game;
|
||||
let game = Game.new();
|
||||
do game.loop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
90
Arkanoid/source/Rect.jack
Normal file
90
Arkanoid/source/Rect.jack
Normal file
@@ -0,0 +1,90 @@
|
||||
class Rect {
|
||||
field int x, y, width, height, hidden;
|
||||
|
||||
constructor Rect new(int xl, int yl, int widthl, int heightl) {
|
||||
let x = xl;
|
||||
let y = yl;
|
||||
let width = widthl;
|
||||
let height = heightl;
|
||||
let hidden = 0;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
method void setPos(int lx, int ly) {
|
||||
do erase();
|
||||
let x = lx;
|
||||
let y = ly;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method void draw() {
|
||||
if (hidden = 1) {
|
||||
do erase();
|
||||
return;
|
||||
}
|
||||
do Screen.setColor(true);
|
||||
do Screen.drawRectangle(x,y,x+width,y+height);
|
||||
return;
|
||||
}
|
||||
|
||||
method void erase() {
|
||||
do Screen.setColor(false);
|
||||
do Screen.drawRectangle(x,y,x+width,y+height);
|
||||
return;
|
||||
}
|
||||
|
||||
method void moveLeft(int dist) {
|
||||
do erase();
|
||||
let x = x - dist;
|
||||
do draw();
|
||||
return;
|
||||
}
|
||||
|
||||
method void moveRight(int dist) {
|
||||
do erase();
|
||||
let x = x + dist;
|
||||
do draw();
|
||||
return;
|
||||
}
|
||||
|
||||
method void moveUp(int dist) {
|
||||
do erase();
|
||||
let y = y - dist;
|
||||
do draw();
|
||||
return;
|
||||
}
|
||||
|
||||
method void moveDown(int dist) {
|
||||
do erase();
|
||||
let y = y + dist;
|
||||
do draw();
|
||||
return;
|
||||
}
|
||||
|
||||
method int getLeft() {
|
||||
return x;
|
||||
}
|
||||
|
||||
method int getRight() {
|
||||
return x + width;
|
||||
}
|
||||
|
||||
method int getTop() {
|
||||
return y;
|
||||
}
|
||||
|
||||
method int getBottom() {
|
||||
return y + height;
|
||||
}
|
||||
|
||||
method int getHidden() {
|
||||
return hidden;
|
||||
}
|
||||
|
||||
method void setHidden(int lhidden) {
|
||||
let hidden = lhidden;
|
||||
return;
|
||||
}
|
||||
}
|
||||
9
Arkanoid/source/Util.jack
Normal file
9
Arkanoid/source/Util.jack
Normal file
@@ -0,0 +1,9 @@
|
||||
class Util {
|
||||
function int abs(int n) {
|
||||
if (n < 0) {
|
||||
return -n;
|
||||
} else {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user