Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1b65aa
commit f94a97e
Showing
13 changed files
with
389 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
canvas { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Snake</title> | ||
<link rel="stylesheet" type="text/css" href="css/snake.css"> | ||
</head> | ||
|
||
<body> | ||
<canvas id="bg"></canvas> | ||
|
||
<script src="js/snake.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
var canvas = document.getElementById('bg'); | ||
var draw2D = canvas.getContext('2d'); | ||
|
||
canvas.width = window.innerWidth; | ||
canvas.height = window.innerHeight; | ||
|
||
|
||
function drawBox(x, y, width, height, color){ | ||
draw2D.beginPath(); | ||
draw2D.rect(x, y, width, height); | ||
draw2D.fillStyle = color; | ||
draw2D.fill(); | ||
draw2D.closePath(); | ||
} | ||
|
||
|
||
function roundToGrid(numb) { | ||
return Math.round(numb/10) * 10; | ||
} | ||
|
||
function randomRange(min, max){ | ||
return Math.random() * (max - min) + min; | ||
} | ||
|
||
|
||
var snake = []; | ||
snake.push( {x:canvas.width/2, y:canvas.height/2} ); | ||
snake.push( {x:-10, y:-10} ); | ||
snake.push( {x:-10, y:-10} ); | ||
|
||
var xDir = 0; | ||
var yDir = -1; | ||
|
||
var food = {}; | ||
food.x = randomRange(0, canvas.width-10); | ||
food.y = randomRange(0, canvas.height-10); | ||
|
||
var moveTimer = 0; | ||
var moveTimerMax = 100; | ||
|
||
|
||
var lastFrameTime = Date.now(); | ||
|
||
window.requestAnimationFrame(update); | ||
|
||
function update () { | ||
var deltaTime = Date.now() - lastFrameTime; | ||
lastFrameTime = Date.now(); | ||
|
||
moveTimer += deltaTime; | ||
if (moveTimer > moveTimerMax) { | ||
moveTimer = 0; | ||
|
||
for(var i=snake.length-1; i > 0; i--){ | ||
snake[i].x = snake[i-1].x; | ||
snake[i].y = snake[i-1].y; | ||
} | ||
|
||
snake[0].x += xDir*10; | ||
snake[0].y += yDir*10; | ||
} | ||
|
||
render(); | ||
checkCollision(); | ||
|
||
window.requestAnimationFrame(update); | ||
} | ||
|
||
|
||
function render() { | ||
drawBox(0,0, canvas.width, canvas.height, '#000000'); | ||
|
||
food.x = roundToGrid(food.x); | ||
food.y = roundToGrid(food.y); | ||
drawBox(food.x, food.y, 10, 10, '#ffffff'); | ||
|
||
for(var i=0; i < snake.length; i++) { | ||
snake[i].x = roundToGrid(snake[i].x); | ||
snake[i].y = roundToGrid(snake[i].y); | ||
drawBox(snake[i].x, snake[i].y, 10, 10, '#ffffff'); | ||
} | ||
} | ||
|
||
|
||
function restart() { | ||
snake = []; | ||
snake.push( {x:canvas.width/2, y:canvas.height/2} ); | ||
snake.push( {x:-10, y:-10} ); | ||
snake.push( {x:-10, y:-10} ); | ||
|
||
xDir = 0; | ||
yDir = -1; | ||
moveTimer = 0; | ||
|
||
food.x = randomRange(0, canvas.width-10); | ||
food.y = randomRange(0, canvas.height-10); | ||
} | ||
|
||
|
||
function checkCollision() { | ||
for(var i=1; i < snake.length; i++) { | ||
if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){ | ||
restart(); | ||
break; | ||
} | ||
|
||
if(food.x == snake[i].x && food.y == snake[i].y){ | ||
food.x = randomRange(0, canvas.width-10); | ||
food.y = randomRange(0, canvas.height-10); | ||
} | ||
} | ||
|
||
if (snake[0].x < 0 || snake[0].y < 0){ | ||
restart(); | ||
} | ||
|
||
if (snake[0].x >= canvas.width || snake[0].y >= canvas.height){ | ||
restart(); | ||
} | ||
|
||
if(snake[0].x == food.x && snake[0].y == food.y) { | ||
snake.push( {x:-10, y:-10} ); | ||
|
||
food.x = randomRange(0, canvas.width-10); | ||
food.y = randomRange(0, canvas.height-10); | ||
} | ||
} | ||
|
||
|
||
window.addEventListener('keydown', function (evt) { | ||
|
||
if(evt.key == 'ArrowUp' && yDir != 1) { | ||
yDir = -1; | ||
xDir = 0; | ||
} | ||
if(evt.key == 'ArrowDown' && yDir != -1){ | ||
yDir = 1; | ||
xDir = 0; | ||
} | ||
if(evt.key == 'ArrowRight' && xDir != -1){ | ||
xDir = 1; | ||
yDir= 0; | ||
} | ||
if(evt.key == 'ArrowLeft' && xDir != 1){ | ||
xDir = -1; | ||
yDir= 0; | ||
} | ||
}, false); | ||
|
||
|
||
window.addEventListener('resize', function() { | ||
canvas.width = window.innerWidth; | ||
canvas.height = window.innerHeight; | ||
|
||
restart(); | ||
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--load bytes from code data | ||
function load_data() | ||
for i = 0, 255 do | ||
local byte = peek(0x5e00+i) | ||
for j = 8, 1, -1 do | ||
bits[i*8+j] = band(byte, 1) | ||
byte = shr(byte, 1) | ||
end | ||
end | ||
end | ||
|
||
|
||
--saving | ||
function save_data() | ||
for i = 0, 255 do | ||
local byte = 0 | ||
for j = 1, 8 do | ||
byte = shl(byte, 1) | ||
byte = bor(byte, bits[i*8+j]) | ||
end | ||
poke(0x5e00+i,byte) | ||
end | ||
end | ||
|
||
--convert integers to bits | ||
function int_to_bits(int, addr, num_bits) | ||
for i = 1, num_bits do | ||
bits[addr+num_bits-i] = band(int, 1) | ||
int = shr(int, 1) | ||
end | ||
end | ||
|
||
--convert bits back to integers | ||
function bits_to_int(addr, num_bits) | ||
local int = 0 | ||
for i = 0, num_bits-1 do | ||
int = shl(int, 1) | ||
int += bits[addr+i] | ||
end | ||
return int | ||
end | ||
|
||
|
||
--saving code to cartridges | ||
|
||
--cartridge_1: | ||
function _update60() | ||
if(btnp(?)) load("cartridge_2") | ||
end | ||
|
||
function _draw() | ||
?"1 " | ||
end | ||
|
||
|
||
--cartridge_2: | ||
function _update60() | ||
if(btnp(???)) load("cartridge_1") | ||
end | ||
|
||
function _draw() | ||
?"2 " | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function _init() | ||
--everything in here happens once as soon as your cart starts | ||
|
||
--create a new table named player, containing x and y value | ||
player = {x=64,y=64} | ||
end | ||
|
||
function _update() | ||
--everything in here happens 30 times per second | ||
|
||
--update the player's location if a dir button is pressed | ||
if btn(0) then player.x-=1 end | ||
if btn(1) then player.x+=1 end | ||
if btn(2) then player.y-=1 end | ||
if btn(3) then player.y+=1 end | ||
|
||
--play SFX 0 if player hits the �z� key or controller button | ||
if btn(4) then sfx(0) end | ||
|
||
end | ||
|
||
function _draw() | ||
--everything in here is drawn 30 times per second | ||
|
||
--clear the screen | ||
cls() | ||
|
||
--draw a 16x16 tile section of the map from index 0,0 (top left) | ||
--at point 0,0 on the screen (also top left) | ||
map(0,0,0,0,16,16) | ||
|
||
--draw sprite 1 at player x and y position | ||
spr(1,player.x,player.y) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FUTURE RYAN: DO NOT USE THE CODE THAT'S IN THE WORD DOCUMENT EDITED FOR THE MAGAZINE. ITS SPACES HAVE BEEN HALVED TO SAVE SPACE, AND WON'T WORK IN PYTHON. USE THE CODE IN THIS FOLDER. | ||
|
||
LOVE | ||
|
||
PAST RYAN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from math import cos, sin, radians | ||
|
||
# set game and screen sizes | ||
SIZE = 9 | ||
WIDTH = SIZE*45 - 5 | ||
HEIGHT = SIZE*45 - 5 | ||
|
||
# bomb range | ||
RANGE = 3 | ||
|
||
# constants for tile types | ||
GROUND = 0 | ||
WALL = 1 | ||
BRICK = 2 | ||
BOMB = 3 | ||
EXPLOSION = 4 | ||
# images for tile types | ||
images = ['ground','wall','brick','bomb','explosion'] | ||
|
||
# create player in top-left of the game | ||
player = Actor('player') | ||
player.mapx = 0 | ||
player.mapy = 0 | ||
|
||
# each position in the tilemap is a 'Tile' | ||
# with a type, an image and a timer | ||
class Tile(): | ||
def __init__(self, type): | ||
self.set(type) | ||
def set(self,type): | ||
self.timer = 0 | ||
self.t=type | ||
self.i=images[type] | ||
|
||
# populate the tilemap with some stuff | ||
tilemap = [[Tile(WALL) if x%2==1 and y%2==1 else Tile(GROUND) for y in range(10)] for x in range(10)] | ||
tilemap[3][2].set(BRICK) | ||
tilemap[4][7].set(BRICK) | ||
|
||
def on_key_down(): | ||
|
||
# store new temporary player position | ||
newx = player.mapx | ||
newy = player.mapy | ||
|
||
# update new position using keyboard | ||
if keyboard.left and player.mapx > 0: | ||
newx -= 1 | ||
elif keyboard.right and player.mapx < SIZE-1: | ||
newx += 1 | ||
elif keyboard.up and player.mapy > 0: | ||
newy -= 1 | ||
elif keyboard.down and player.mapy < SIZE-1: | ||
newy += 1 | ||
|
||
# move player to new position if allowed | ||
if tilemap[newx][newy].t in [GROUND,EXPLOSION]: | ||
player.mapx = newx | ||
player.mapy = newy | ||
|
||
# space key to place bomb | ||
if keyboard.space: | ||
tilemap[player.mapx][player.mapy].set(BOMB) | ||
tilemap[player.mapx][player.mapy].timer = 150 | ||
|
||
def update(): | ||
|
||
# iterate through each tile in tilemap | ||
for x in range(SIZE): | ||
for y in range(SIZE): | ||
|
||
tile = tilemap[x][y] | ||
|
||
# decrement timer | ||
if tile.timer > 0: | ||
tile.timer -= 1 | ||
|
||
# process tile types on timer finish | ||
if tile.timer <= 0: | ||
|
||
# explosions eventually become ground | ||
if tile.t == EXPLOSION: | ||
tile.set(GROUND) | ||
|
||
# bombs eventually create explosions | ||
if tile.t == BOMB: | ||
# bombs radiate out in all 4 directions | ||
for angle in range(0,360,90): | ||
cosa = int(cos(radians(angle))) | ||
sina = int(sin(radians(angle))) | ||
# RANGE determines bomb reach | ||
for ran in range(1,RANGE): | ||
xoffset = ran*cosa | ||
yoffset = ran*sina | ||
# only create explosions within the tilemap, and only on ground and brick tiles | ||
if x+xoffset in range(0,SIZE) and y+yoffset in range(0,SIZE) and tilemap[x+xoffset][y+yoffset].t in [GROUND,BRICK]: | ||
tilemap[x+xoffset][y+yoffset].set(EXPLOSION) | ||
tilemap[x+xoffset][y+yoffset].timer = 50 | ||
else: | ||
break | ||
|
||
# remove bomb | ||
tile.set(EXPLOSION) | ||
tile.timer = 50 | ||
|
||
def draw(): | ||
# draw the tilemap | ||
for x in range(SIZE): | ||
for y in range(SIZE): | ||
screen.blit( tilemap[x][y].i,(x*45,y*45) ) | ||
# draw the player | ||
screen.blit( player.image, (player.mapx*45,player.mapy*45) ) |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.