Archived
Private
Public Access
1
0

Initial commit

This commit is contained in:
2022-09-04 12:45:01 +02:00
commit f4a01d6a69
11601 changed files with 4206660 additions and 0 deletions

3
Java/SnakeGame/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

7
Java/SnakeGame/.idea/discord.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="PROJECT_FILES" />
<option name="description" value="" />
</component>
</project>

6
Java/SnakeGame/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
Java/SnakeGame/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/SnakeGame.iml" filepath="$PROJECT_DIR$/SnakeGame.iml" />
</modules>
</component>
</project>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

1
Java/SnakeGame/data.save Normal file
View File

@@ -0,0 +1 @@
42

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: actions.Main

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: actions.Main

View File

@@ -0,0 +1,29 @@
package actions;
import clocks.GameClock;
import game.Snake;
public class Collision {
public static boolean collideSelf() {
for (int i = 0; i < Snake.tails.size(); i++) {
if (Snake.head.getX() == Snake.tails.get(i).getX() && Snake.head.getY() == Snake.tails.get(i).getY() && !Snake.tails.get(i).isWait()) return true;
}
return false;
}
public static boolean collideWall() {
return (Snake.head.getX() < 0 || Snake.head.getX() > 15 || Snake.head.getY() < 0 || Snake.head.getY() > 15);
}
public static void collidePickUp() {
if (Snake.head.getX() == Snake.pickUp.getX() && Snake.head.getY() == Snake.pickUp.getY()) {
Snake.pickUp.reset();
Snake.addTail();
Snake.score++;
if (GameClock.sleep > 1) GameClock.sleep--;
if (Snake.score > Snake.highscore) Snake.updateHighscore(Snake.score);
}
}
}

View File

@@ -0,0 +1,53 @@
package actions;
import clocks.GameClock;
import game.Dir;
import game.Snake;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyHandler implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_W:
if (!(Snake.head.getDir() == Dir.DOWN) && !Snake.waitToMove) {
Snake.head.setDir(Dir.UP);
Snake.waitToMove = true;
}
break;
case KeyEvent.VK_A:
if (!(Snake.head.getDir() == Dir.RIGHT) && !Snake.waitToMove) {
Snake.head.setDir(Dir.LEFT);
Snake.waitToMove = true;
}
break;
case KeyEvent.VK_S:
if (!(Snake.head.getDir() == Dir.UP) && !Snake.waitToMove) {
Snake.head.setDir(Dir.DOWN);
Snake.waitToMove = true;
}
break;
case KeyEvent.VK_D:
if (!(Snake.head.getDir() == Dir.LEFT) && !Snake.waitToMove) {
Snake.head.setDir(Dir.RIGHT);
Snake.waitToMove = true;
}
break;
case KeyEvent.VK_ESCAPE:
GameClock.running = !GameClock.running;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}

View File

@@ -0,0 +1,18 @@
package actions;
import clocks.GameClock;
import game.Snake;
import gui.GUI;
public class Main {
public static void main(String[] args) {
GUI gui = new GUI();
GameClock gc = new GameClock();
Snake.highscore = Snake.getHighscore();
gui.create();
gc.start();
}
}

View File

@@ -0,0 +1,35 @@
package clocks;
import actions.Collision;
import game.Snake;
public class GameClock extends Thread {
public static boolean running = true;
public static int sleep = 150;
public void start() {
try {
while (true) {
sleep(sleep);
if (!running) continue;
Snake.move();
Snake.waitToMove = false;
Collision.collidePickUp();
if (Collision.collideSelf()) {
Snake.tails.clear();
Snake.score = 0;
GameClock.sleep = 150;
}
if (Collision.collideWall()) {
Snake.tails.clear();
Snake.head.setX(7);
Snake.head.setY(7);
Snake.score = 0;
GameClock.sleep = 150;
}
}
}catch (Exception e) { e.printStackTrace(); }
}
}

View File

@@ -0,0 +1,10 @@
package game;
public enum Dir {
UP,
LEFT,
RIGHT,
DOWN;
}

View File

@@ -0,0 +1,32 @@
package game;
public class Head {
private int x;
private int y;
private Dir dir = Dir.RIGHT;
public Head(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Dir getDir() {
return dir;
}
public void setDir(Dir dir) {
this.dir = dir;
}
}

View File

@@ -0,0 +1,28 @@
package game;
import java.util.concurrent.ThreadLocalRandom;
public class PickUp {
private int x;
private int y;
public PickUp() {
this.x = ThreadLocalRandom.current().nextInt(0, 15);
this.y = ThreadLocalRandom.current().nextInt(0, 15);
System.out.println("Spawned Fruit at " + this.x + ":" + this.y);
}
public void reset() {
this.x = ThreadLocalRandom.current().nextInt(0, 15);
this.y = ThreadLocalRandom.current().nextInt(0, 15);
System.out.println("Spawned Fruit at " + this.x + ":" + this.y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}

View File

@@ -0,0 +1,100 @@
package game;
import gui.GUI;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
public class Snake {
public static Head head = new Head(7, 7);
public static ArrayList<Tail> tails = new ArrayList<>();
public static boolean waitToMove = false;
public static PickUp pickUp = new PickUp();
public static int score = 0;
public static int highscore = 0;
public static void addTail() {
if (tails.size() < 1) {
tails.add(new Tail(head.getX(), head.getY()));
}else {
tails.add(new Tail(tails.get(tails.size() - 1).getX(), tails.get(tails.size() - 1).getY()));
}
}
public static void move() {
if (tails.size() >= 2) {
for (int i = tails.size() - 1; i >= 1; i--) {
if (tails.get(i).isWait()) {
tails.get(i).setWait(false);
}else {
tails.get(i).setX(tails.get(i - 1).getX());
tails.get(i).setY(tails.get(i - 1).getY());
}
}
}
if (tails.size() >= 1) {
if (tails.get(0).isWait()) {
tails.get(0).setWait(false);
}else {
tails.get(0).setX(head.getX());
tails.get(0).setY(head.getY());
}
}
switch (head.getDir()) {
case RIGHT:
head.setX(head.getX() + 1);
break;
case LEFT:
head.setX(head.getX() - 1);
break;
case UP:
head.setY(head.getY() - 1);
break;
case DOWN:
head.setY(head.getY() + 1);
break;
}
}
public static Point ptc(int x, int y) {
Point p = new Point(0, 0);
p.x = x * 32 + GUI.xOff;
p.y = y * 32 + GUI.yOff;
return p;
}
public static void updateHighscore(int value) {
try {
highscore = value;
File file = new File("data.save");
file.delete();
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(String.valueOf(highscore));
writer.close();
}catch (IOException e) { e.printStackTrace(); }
}
public static int getHighscore() {
try {
File file = new File("data.save");
if (!file.exists()) {
file.createNewFile();
return 0;
}
BufferedReader reader = new BufferedReader(new FileReader(file));
int output = Integer.parseInt(reader.readLine());
reader.close();
return output;
}catch (IOException e) { e.printStackTrace(); }
return 0;
}
}

View File

@@ -0,0 +1,32 @@
package game;
public class Tail {
private int x;
private int y;
private boolean wait = true;
public Tail(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isWait() {
return wait;
}
public void setWait(boolean wait) {
this.wait = wait;
}
}

View File

@@ -0,0 +1,56 @@
package gui;
import clocks.GameClock;
import game.Snake;
import javax.swing.*;
import java.awt.*;
public class Draw extends JLabel {
Point p;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, GUI.with, GUI.height);
g.setColor(new Color(51, 204, 51));
for (int i = 0; i < Snake.tails.size(); i++) {
p = Snake.ptc(Snake.tails.get(i).getX(), Snake.tails.get(i).getY());
g.fillRect(p.x, p.y, 32, 32);
}
g.setColor(new Color(0, 153, 0));
p = Snake.ptc(Snake.head.getX(), Snake.head.getY());
g.fillRect(p.x, p.y, 32, 32);
g.setColor(new Color(204, 51, 0));
p = Snake.ptc(Snake.pickUp.getX(), Snake.pickUp.getY());
g.fillRect(p.x, p.y, 32, 32);
g.setColor(Color.GRAY);
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
g.drawRect(x * 32 + GUI.xOff, y * 32 + GUI.yOff, 32, 32);
}
}
g.setColor(Color.BLACK);
g.drawRect(GUI.xOff, GUI.yOff, 512, 512);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Score: " + Snake.score, 5, 25);
g.drawString("Highscore: " + Snake.highscore, 5, 50);
if (!GameClock.running) {
g.drawString("Game Paused...", 5, 75);
}
repaint();
}
}

View File

@@ -0,0 +1,34 @@
package gui;
import actions.KeyHandler;
import javax.swing.*;
public class GUI {
JFrame jf;
Draw d;
public static int with = 800;
public static int height = 600;
public static int xOff = 170;
public static int yOff = 20;
public void create() {
jf = new JFrame("Snake");
jf.setSize(with, height);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setLayout(null);
jf.setResizable(false);
jf.addKeyListener(new KeyHandler());
d = new Draw();
d.setBounds(0, 0, with, height);
d.setVisible(true);
jf.add(d);
jf.requestFocus();
jf.setVisible(true);
}
}