idk
This commit is contained in:
1 parent
04009e61bf
commit
069c04cd71
9 files changed
+611
No files matched your search
Binary file not shown.
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 Yakko Majuri
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,5 @@
|
||||
# A Browser Snake Game Built With Vanilla ~~JS~~ Python!
|
||||
|
||||
### More details: [Sick Of JavaScript For The Web? Just Write Browser Python Instead](https://medium.com/@yakko.majuri/sick-of-javascript-just-use-browser-python-4b9679efe08b?source=friends_link&sk=40e664d45bfea34d35189c32cd5d0a51)
|
||||
|
||||
Want to improve the game? Feel free to submit a pull request!
|
||||
@@ -0,0 +1,179 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Brython Snake</title>
|
||||
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js">
|
||||
</script>
|
||||
<script type="text/javascript"
|
||||
src="https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.js">
|
||||
</script>
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
|
||||
|
||||
<style>
|
||||
|
||||
h1 {
|
||||
margin-top: 8%;
|
||||
color: rgb(83, 238, 36);
|
||||
}
|
||||
|
||||
h3 {
|
||||
color:rgb(190, 187, 187);
|
||||
}
|
||||
|
||||
h6 {
|
||||
color:rgb(117, 117, 117);
|
||||
}
|
||||
|
||||
a {
|
||||
color: rgb(83, 238, 36);
|
||||
}
|
||||
|
||||
button {
|
||||
display: block;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
background-color: transparent !important;
|
||||
border-color: rgb(155, 156, 155) !important;
|
||||
color: rgb(155, 156, 155) !important;
|
||||
font-size: 8px !important;
|
||||
padding: 3px !important;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
border-color: rgb(68, 241, 68) !important;
|
||||
color: rgb(194, 196, 194) !important;
|
||||
}
|
||||
|
||||
body {
|
||||
background-color: black;
|
||||
}
|
||||
|
||||
canvas {
|
||||
margin-top: 1%;
|
||||
padding-left: 0;
|
||||
padding-right: 0;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
display: block;
|
||||
border: 1px;
|
||||
border-style: solid;
|
||||
border-color: #535353;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
</head>
|
||||
|
||||
<body onload="brython()">
|
||||
|
||||
<h1 class="text-center">Snake built with <a href="https://brython.info">Python!</a></h1>
|
||||
<canvas id="game-board" width="400" height="400"></canvas>
|
||||
<br>
|
||||
<h3 id="score" class="text-center">Score: 0</h3>
|
||||
<br>
|
||||
<h6 id="high-score" class="text-center">High Score: 0</h6>
|
||||
<br>
|
||||
<div class="text-center">
|
||||
<button id="instructions-btn" class="btn btn-info">Instructions</button>
|
||||
</div>
|
||||
|
||||
|
||||
<script type="text/python">
|
||||
|
||||
from browser import document, html, window
|
||||
import random
|
||||
|
||||
score = 0
|
||||
high_score = 0
|
||||
|
||||
px = py = 10
|
||||
gs = tc = 20
|
||||
ax = ay = 15
|
||||
xv = yv = 0
|
||||
trail = []
|
||||
tail = 5
|
||||
|
||||
pre_pause = [0,0]
|
||||
paused = False
|
||||
|
||||
def game():
|
||||
global px, py, tc, gs, ax, ay, trail, tail, score
|
||||
px += xv
|
||||
py += yv
|
||||
if px < 0:
|
||||
px = tc-1
|
||||
if px > tc-1:
|
||||
px = 0
|
||||
if py < 0:
|
||||
py = tc-1
|
||||
if py > tc-1:
|
||||
py = 0
|
||||
ctx.fillStyle = "black"
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
||||
ctx.fillStyle = "lime"
|
||||
for i in range(len(trail)):
|
||||
ctx.fillRect(trail[i][0]*gs, trail[i][1]*gs, gs-2, gs-2)
|
||||
if trail[i][0] == px and trail[i][1] == py:
|
||||
score = score if paused else 0
|
||||
tail = tail if paused else 5
|
||||
trail.insert(0, [px, py])
|
||||
while len(trail) > tail:
|
||||
trail.pop()
|
||||
|
||||
if ax == px and ay == py:
|
||||
tail += 1
|
||||
ax = int(random.random()*tc)
|
||||
ay = int(random.random()*tc)
|
||||
score += 1
|
||||
update_score(score)
|
||||
ctx.fillStyle = "red"
|
||||
ctx.fillRect(ax*gs, ay*gs, gs-2, gs-2)
|
||||
|
||||
def update_score(new_score):
|
||||
global high_score
|
||||
document["score"].innerHTML = "Score: " + str(new_score)
|
||||
if new_score > high_score:
|
||||
document["high-score"].innerHTML = "High Score: " + str(new_score)
|
||||
high_score = new_score
|
||||
|
||||
def key_push(evt):
|
||||
global xv, yv, pre_pause, paused
|
||||
key = evt.keyCode
|
||||
if key == 37 and not paused:
|
||||
xv = -1
|
||||
yv = 0
|
||||
elif key == 38 and not paused:
|
||||
xv = 0
|
||||
yv = -1
|
||||
elif key == 39 and not paused:
|
||||
xv = 1
|
||||
yv = 0
|
||||
elif key == 40 and not paused:
|
||||
xv = 0
|
||||
yv = 1
|
||||
elif key == 32:
|
||||
temp = [xv, yv]
|
||||
xv = pre_pause[0]
|
||||
yv = pre_pause[1]
|
||||
pre_pause = [*temp]
|
||||
paused = not paused
|
||||
|
||||
def show_instructions(evt):
|
||||
window.alert("Use the arrow keys to move and press spacebar to pause the game.")
|
||||
|
||||
canvas = document["game-board"]
|
||||
ctx = canvas.getContext("2d")
|
||||
document.addEventListener("keydown", key_push)
|
||||
game_loop = window.setInterval(game, 1000/10)
|
||||
instructions_btn = document["instructions-btn"]
|
||||
instructions_btn.addEventListener("click", show_instructions)
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,202 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>JavaScript Snake</title>
|
||||
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
|
||||
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
|
||||
|
||||
<style>
|
||||
|
||||
h1 {
|
||||
margin-top: 8%;
|
||||
color: rgb(83, 238, 36);
|
||||
}
|
||||
|
||||
h3 {
|
||||
color: rgb(190, 187, 187);
|
||||
}
|
||||
|
||||
h6 {
|
||||
color: rgb(117, 117, 117);
|
||||
}
|
||||
|
||||
a {
|
||||
color: rgb(117, 117, 117);
|
||||
}
|
||||
|
||||
button {
|
||||
display: block;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
background-color: transparent !important;
|
||||
border-color: rgb(155, 156, 155) !important;
|
||||
color: rgb(155, 156, 155) !important;
|
||||
font-size: 8px !important;
|
||||
padding: 3px !important;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
border-color: rgb(68, 241, 68) !important;
|
||||
color: rgb(194, 196, 194) !important;
|
||||
}
|
||||
|
||||
body {
|
||||
background-color: black;
|
||||
}
|
||||
|
||||
canvas {
|
||||
margin-top: 1%;
|
||||
padding-left: 0;
|
||||
padding-right: 0;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
display: block;
|
||||
border: 1px;
|
||||
border-style: solid;
|
||||
border-color: #535353;
|
||||
}
|
||||
</style>
|
||||
|
||||
</head>
|
||||
|
||||
|
||||
<body>
|
||||
<h1 class="text-center">Snake built with JavaScript!</h1>
|
||||
<h6 class="text-center">Not that impressive... Check out <a href="brython-snake.html">snake built with Python - in browser!</a></h1>
|
||||
<canvas id="game-board" width="400" height="400"></canvas>
|
||||
<br>
|
||||
<h3 id="score" class="text-center">Score: 0</h3>
|
||||
<br>
|
||||
<h6 id="high-score" class="text-center">High Score: 0</h6>
|
||||
<br>
|
||||
<div class="text-center">
|
||||
<button id="instructions-btn" class="btn btn-info">Instructions</button>
|
||||
</div>
|
||||
<script>
|
||||
|
||||
/*
|
||||
Original Code Credits: Chris DeLeon of HomeTeam GameDev
|
||||
Modified by: Yakko Majuri
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
let canvas, ctx;
|
||||
|
||||
window.onload = function () {
|
||||
canvas = document.getElementById("game-board");
|
||||
ctx = canvas.getContext("2d");
|
||||
document.addEventListener("keydown", keyPush);
|
||||
setInterval(game, 1000 / 10);
|
||||
let instructionsBtn = document.getElementById("instructions-btn");
|
||||
instructionsBtn.addEventListener("click", showInstructions)
|
||||
}
|
||||
|
||||
let score = 0;
|
||||
let highScore = 0;
|
||||
|
||||
let px = 10;
|
||||
let py = 10;
|
||||
let gs = 20;
|
||||
let tc = 20;
|
||||
let ax = 15;
|
||||
let ay = 15;
|
||||
let xv = 0;
|
||||
let yv = 0;
|
||||
let trail = [];
|
||||
let tail = 5;
|
||||
|
||||
let paused = false
|
||||
let pre_pause = [0, 0]
|
||||
|
||||
|
||||
|
||||
function game() {
|
||||
px += xv;
|
||||
py += yv;
|
||||
if (px < 0) {
|
||||
px = tc - 1;
|
||||
}
|
||||
if (px > tc - 1) {
|
||||
px = 0;
|
||||
}
|
||||
if (py < 0) {
|
||||
py = tc - 1;
|
||||
}
|
||||
if (py > tc - 1) {
|
||||
py = 0;
|
||||
}
|
||||
ctx.fillStyle = "black";
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
|
||||
ctx.fillStyle = "lime";
|
||||
for (var i = 0; i < trail.length; ++i) {
|
||||
ctx.fillRect(trail[i].x * gs, trail[i].y * gs, gs - 2, gs - 2);
|
||||
if (trail[i].x == px && trail[i].y == py) {
|
||||
tail = paused ? tail : 5;
|
||||
score = paused ? score : 0;
|
||||
}
|
||||
}
|
||||
trail.push({ x: px, y: py });
|
||||
while (trail.length > tail) {
|
||||
trail.shift();
|
||||
}
|
||||
|
||||
if (ax == px && ay == py) {
|
||||
++score;
|
||||
++tail;
|
||||
ax = Math.floor(Math.random() * tc);
|
||||
ay = Math.floor(Math.random() * tc);
|
||||
}
|
||||
ctx.fillStyle = "red";
|
||||
ctx.fillRect(ax * gs, ay * gs, gs - 2, gs - 2);
|
||||
updateScore(score)
|
||||
}
|
||||
|
||||
function updateScore(newScore) {
|
||||
document.getElementById("score").innerHTML = "Score: " + String(newScore);
|
||||
if (newScore > highScore) {
|
||||
document.getElementById("high-score").innerHTML = "High Score: " + String(newScore);
|
||||
highScore = newScore;
|
||||
}
|
||||
}
|
||||
|
||||
function keyPush(evt) {
|
||||
if (!paused) {
|
||||
switch (evt.keyCode) {
|
||||
case 37:
|
||||
xv = -1; yv = 0;
|
||||
break;
|
||||
case 38:
|
||||
xv = 0; yv = -1;
|
||||
break;
|
||||
case 39:
|
||||
xv = 1; yv = 0;
|
||||
break;
|
||||
case 40:
|
||||
xv = 0; yv = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (evt.keyCode === 32) {
|
||||
let temp = [xv, yv];
|
||||
xv = pre_pause[0];
|
||||
yv = pre_pause[1];
|
||||
pre_pause = [...temp];
|
||||
paused = !paused;
|
||||
}
|
||||
}
|
||||
|
||||
function showInstructions() {
|
||||
window.alert("Use the arrow keys to move and press spacebar to pause the game.")
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
Original Code Credits: Chris DeLeon of HomeTeam GameDev
|
||||
Modified by: Yakko Majuri
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
let canvas, ctx;
|
||||
|
||||
window.onload = function () {
|
||||
canvas = document.getElementById("game-board");
|
||||
ctx = canvas.getContext("2d");
|
||||
document.addEventListener("keydown", keyPush);
|
||||
setInterval(game, 1000 / 15);
|
||||
let instructionsBtn = document.getElementById("instructions-btn");
|
||||
instructionsBtn.addEventListener("click", showInstructions)
|
||||
}
|
||||
|
||||
let score = 0;
|
||||
let highScore = 0;
|
||||
|
||||
let px = 10;
|
||||
let py = 10;
|
||||
let gs = 20;
|
||||
let tc = 20;
|
||||
let ax = 15;
|
||||
let ay = 15;
|
||||
let xv = 0;
|
||||
let yv = 0;
|
||||
let trail = [];
|
||||
let tail = 5;
|
||||
|
||||
let paused = false
|
||||
let pre_pause = [0, 0]
|
||||
|
||||
|
||||
|
||||
function game() {
|
||||
px += xv;
|
||||
py += yv;
|
||||
if (px < 0) {
|
||||
px = tc - 1;
|
||||
}
|
||||
if (px > tc - 1) {
|
||||
px = 0;
|
||||
}
|
||||
if (py < 0) {
|
||||
py = tc - 1;
|
||||
}
|
||||
if (py > tc - 1) {
|
||||
py = 0;
|
||||
}
|
||||
ctx.fillStyle = "black";
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
|
||||
ctx.fillStyle = "lime";
|
||||
for (var i = 0; i < trail.length; ++i) {
|
||||
ctx.fillRect(trail[i].x * gs, trail[i].y * gs, gs - 2, gs - 2);
|
||||
if (trail[i].x == px && trail[i].y == py) {
|
||||
tail = 5;
|
||||
|
||||
score = paused ? score : 0;
|
||||
}
|
||||
}
|
||||
trail.push({ x: px, y: py });
|
||||
while (trail.length > tail) {
|
||||
trail.shift();
|
||||
}
|
||||
|
||||
if (ax == px && ay == py) {
|
||||
++score;
|
||||
++tail;
|
||||
ax = Math.floor(Math.random() * tc);
|
||||
ay = Math.floor(Math.random() * tc);
|
||||
}
|
||||
ctx.fillStyle = "red";
|
||||
ctx.fillRect(ax * gs, ay * gs, gs - 2, gs - 2);
|
||||
updateScore(score)
|
||||
}
|
||||
|
||||
function updateScore(newScore) {
|
||||
document.getElementById("score").innerHTML = "Score: " + String(newScore);
|
||||
if (newScore > highScore) {
|
||||
document.getElementById("high-score").innerHTML = "High Score: " + String(newScore);
|
||||
highScore = newScore;
|
||||
}
|
||||
}
|
||||
|
||||
function keyPush(evt) {
|
||||
if (!paused) {
|
||||
switch (evt.keyCode) {
|
||||
case 37:
|
||||
xv = -1; yv = 0;
|
||||
break;
|
||||
case 38:
|
||||
xv = 0; yv = -1;
|
||||
break;
|
||||
case 39:
|
||||
xv = 1; yv = 0;
|
||||
break;
|
||||
case 40:
|
||||
xv = 0; yv = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (evt.keyCode === 32) {
|
||||
let temp = [xv, yv];
|
||||
xv = pre_pause[0];
|
||||
yv = pre_pause[1];
|
||||
pre_pause = [...temp];
|
||||
paused = !paused;
|
||||
}
|
||||
}
|
||||
|
||||
function showInstructions() {
|
||||
window.alert("Use the arrow keys to move and press spacebar to pause the game.")
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
from browser import document, html, window
|
||||
from javascript import Math
|
||||
|
||||
score = 0
|
||||
high_score = 0
|
||||
px = py = 10
|
||||
gs = tc = 20
|
||||
ax = ay = 15
|
||||
xv = yv = 0
|
||||
trail = []
|
||||
tail = 5
|
||||
pre_pause = [0,0]
|
||||
paused = False
|
||||
|
||||
def game():
|
||||
global px, py, tc, gs, ax, ay, trail, tail, score
|
||||
px += xv
|
||||
py += yv
|
||||
if px < 0:
|
||||
px = tc-1
|
||||
if px > tc-1:
|
||||
px = 0
|
||||
if py < 0:
|
||||
py = tc-1
|
||||
if py > tc-1:
|
||||
py = 0
|
||||
ctx.fillStyle = "black"
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
||||
ctx.fillStyle = "lime"
|
||||
for i in range(len(trail)):
|
||||
ctx.fillRect(trail[i][0]*gs, trail[i][1]*gs, gs-2, gs-2)
|
||||
if trail[i][0] == px and trail[i][1] == py:
|
||||
score = score if paused else 0
|
||||
tail = 5
|
||||
trail.insert(0, [px, py])
|
||||
while len(trail) > tail:
|
||||
trail.pop()
|
||||
|
||||
if ax == px and ay == py:
|
||||
tail += 1
|
||||
ax = Math.floor(Math.random()*tc)
|
||||
ay = Math.floor(Math.random()*tc)
|
||||
score += 1
|
||||
update_score(score)
|
||||
ctx.fillStyle = "red"
|
||||
ctx.fillRect(ax*gs, ay*gs, gs-2, gs-2)
|
||||
|
||||
def update_score(new_score):
|
||||
global high_score
|
||||
document["score"].innerHTML = "Score: " + str(new_score)
|
||||
if new_score > high_score:
|
||||
document["high-score"].innerHTML = "High Score: " + str(new_score)
|
||||
high_score = new_score
|
||||
|
||||
def key_push(evt):
|
||||
global xv, yv, pre_pause, paused
|
||||
key = evt.keyCode
|
||||
if key == 37 and not paused:
|
||||
xv = -1
|
||||
yv = 0
|
||||
elif key == 38 and not paused:
|
||||
xv = 0
|
||||
yv = -1
|
||||
elif key == 39 and not paused:
|
||||
xv = 1
|
||||
yv = 0
|
||||
elif key == 40 and not paused:
|
||||
xv = 0
|
||||
yv = 1
|
||||
elif key == 32:
|
||||
temp = [xv, yv]
|
||||
xv = pre_pause[0]
|
||||
yv = pre_pause[1]
|
||||
pre_pause = [*temp]
|
||||
paused = not paused
|
||||
|
||||
|
||||
def show_instructions(evt):
|
||||
window.alert("Use the arrow keys to move and press spacebar to pause the game.")
|
||||
|
||||
canvas = document["game-board"]
|
||||
ctx = canvas.getContext("2d")
|
||||
document.addEventListener("keydown", key_push)
|
||||
game_loop = window.setInterval(game, 1000/10)
|
||||
instructions_btn = document["instructions-btn"]
|
||||
instructions_btn.addEventListener("click", show_instructions)
|
||||
@@ -9,6 +9,7 @@
|
||||
<link rel="stylesheet" href="trkeey.css">
|
||||
<a href="idk.html">idk</a>
|
||||
<a href="/tetris_game-master/tetris_game-master/snak.html">snak</a>
|
||||
<a href="/brython-snake-master/brython-snake-master/brython.html">no snak</a>
|
||||
<a href="fun.html">bata stuff</a>
|
||||
<a href="miena.html">ai</a>
|
||||
<a href="mincrat.html">games</a>
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user