Created
March 31, 2023 17:44
-
-
Save TheKodeToad/77748931789195678f241298d7dc7d16 to your computer and use it in GitHub Desktop.
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
#include <CobolScript.h> | |
program-id.JavaLua++ contains VisualBasic JavaScript; | |
working-storage section. | |
01 ScreenSize type CGFloat. | |
01 CellSize type CGFloat. | |
javaClass Snake { | |
dataType Apple; | |
dataType SnakeSegment; | |
dataType Direction; | |
usingBrowser(method directionHandler); | |
direction keyPressed value null; | |
} | |
procedure division. | |
begin | |
set ScreenSize to 500; | |
set CellSize to 25; | |
Apple ::generateApple { | |
x = randomInt(0, ScreenSize ÷ CellSize); | |
y = randomInt(0, ScreenSize ÷ CellSize); | |
return (x, y); | |
} | |
SnakeSegment init value [function] { | |
constructor(positionX, positionY) { | |
this.xPos = positionX; | |
this.yPos = positionY; | |
} | |
void updatePosition(nX, nY) { | |
this.xPos = nX; | |
this.yPos = nY; | |
} | |
} | |
createObject (snake, Apple, [ ]); | |
snake.push(new SnakeSegment(ScreenSize ÷ 2, ScreenSize ÷ 2)); | |
loop while snakeIsAlive | |
if ::Apple.eatenBy(snake.head) { | |
apple.respawn(); | |
snake.length += 1; | |
} | |
// Perform Movement | |
Direction nextDir = keyPressed || snake.currentDir; | |
xPos, yPos = snake.head.getPosition(); | |
if (nextDir.up) yPos -= CellSize; | |
if (nextDir.down) yPos += CellSize; | |
if (nextDir.left) xPos -= CellSize; | |
if (nextDir.right) xPos += CellSize; | |
// Collision with borders or snake segment | |
if yPos < 0 || yPos >= ScreenSize || | |
xPos < 0 || xPos >= ScreenSize || | |
snake.collidesWithSelf() { | |
goto notAlive; | |
else { | |
snake.updatePosition(xPos, yPos); | |
} | |
end loop; | |
notAlive: | |
print("Game Over") | |
// DirectionHandler (VisualCobolBasicScript++) | |
method directionHandler(event) { | |
switch (event.keyCode) { | |
case 37: keyPressed = Direction.Left; | |
case 38: keyPressed = Direction.Up; | |
case 39: keyPressed = Direction.Right; | |
case 40: keyPressed = Direction.Down; | |
} | |
} | |
// Using browser functionality for VisualBasicAction JavaScript | |
html <canvas id="gameCanvas" width="ScreenSize" height="ScreenSize"></canvas> | |
attachListener (window, 'keypress', directionHandler); | |
// Rendering in JavaCobol++Script | |
bgColor = "#000"; | |
canvas = getElementById("gameCanvas"); | |
ctx = canvas.getContext("2d"); | |
while (snakeIsAliveGoesTrue) { | |
msDelay = 200; | |
clearRect(0, 0, ScreenSize, ScreenSize); | |
snake.draw(); | |
apple.draw(); | |
wait(msDelay); | |
} | |
SnakeSegment::draw { | |
ctx.fillStyle = "#0F0"; | |
roundRectFill(this.xPos, this.yPos, CellSize, CellSize, 5); | |
} | |
Apple::draw { | |
ctx.fillStyle = "#F00"; | |
roundRectFill(this.x, this.y, CellSize, CellSize, 5); | |
} | |
function roundRectFill(x, y, width, height, cornerRadius) { | |
cornerRadius = cornerRadius || 0; | |
ctx.beginPath(); | |
ctx.moveTo(x + cornerRadius, y); | |
ctx.lineTo(x + width - cornerRadius, y); | |
ctx.arcTo(x + width, y, x + width, y + corner-radius, cornerRadius); | |
ctx.lineTo(x + width, y + height - cornerRadius); | |
ctx.arcTo(x + width, y + height, x + width - cornerRadius, y + height, | |
ctx.arcTo(x, y + height, x, y + height - cornerRadius, cornerRadius); | |
ctx.lineTo(x, y + cornerRadius); | |
ctx.arcTo(x, y, x + cornerRadius, y, cornerRadius); | |
ctx.closePath(); | |
ctx.fill(); | |
} | |
// End of rendering | |
// Shortening the LuaCobol++ | |
procedure UpdatePosition(segment, nX, nY) { | |
segment.x = nX; segment.y = nY; | |
} | |
procedure CheckCollision(headX, headY, segmentX, segmentY) { | |
if (segmentX == headX && segmentY == headY) { | |
return (true); | |
} | |
} | |
// Simplified movement | |
procedure UpdateMovement(snake, Dir, X, Y) { | |
if (Dir.up) Y -= CellSize; | |
if (Dir.down) Y += CellSize; | |
if (Dir.left) X -= CellSize; | |
if (Dir.right) X += CellSize; | |
} | |
// BeginMain JavaLuaCobol++ | |
while snakeIsAlive do | |
Apple.spawn(); | |
for i = snake.length; i >= 1; i-- { | |
SnakeSegment newPosition = snake[i - 1]; | |
UpdatePosition(snake[i], SnakeSegment.x, SnakeSegment.y); | |
} | |
UpdateMovement(snake, keyPressed, snake.head.x, snake.head.y); | |
if snake.collidesWithSelf() goto notAlive end loop; | |
end; | |
// EndMain |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment