Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanlambie committed Apr 24, 2019
1 parent d1b65aa commit f94a97e
Show file tree
Hide file tree
Showing 13 changed files with 389 additions and 0 deletions.
5 changes: 5 additions & 0 deletions MakeSnake/css/snake.css
@@ -0,0 +1,5 @@
canvas {
position: fixed;
top: 0;
left: 0;
}
14 changes: 14 additions & 0 deletions MakeSnake/index.html
@@ -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>
156 changes: 156 additions & 0 deletions MakeSnake/js/snake.js
@@ -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);
63 changes: 63 additions & 0 deletions pico-8-beyond-limits/beyond-limits.txt
@@ -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
34 changes: 34 additions & 0 deletions pico-8-getting-started/getstarted.txt
@@ -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
5 changes: 5 additions & 0 deletions source-code-bomberman/README-RYAN.txt
@@ -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
112 changes: 112 additions & 0 deletions source-code-bomberman/bombs.py
@@ -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) )
Binary file added source-code-bomberman/images/bomb.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source-code-bomberman/images/brick.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source-code-bomberman/images/explosion.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source-code-bomberman/images/ground.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source-code-bomberman/images/player.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source-code-bomberman/images/wall.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f94a97e

Please sign in to comment.