initial commit

This commit is contained in:
Daniel Cortes
2020-05-22 01:28:24 -04:00
commit 0fd7a1343d
63 changed files with 2329 additions and 0 deletions

16
.idea/compiler.xml generated Executable file
View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="Games" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel target="1.8">
<module name="Games" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>

17
.idea/inspectionProfiles/Project_Default.xml generated Executable file
View File

@@ -0,0 +1,17 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="Duplicates" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="InfiniteLoopStatement" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
<inspection_tool class="WeakerAccess" enabled="false" level="WARNING" enabled_by_default="false">
<option name="SUGGEST_PACKAGE_LOCAL_FOR_MEMBERS" value="true" />
<option name="SUGGEST_PACKAGE_LOCAL_FOR_TOP_CLASSES" value="true" />
<option name="SUGGEST_PRIVATE_FOR_INNERS" value="false" />
</inspection_tool>
</profile>
</component>

13
.idea/libraries/Maven__org_jooq_jooq_3_9_5.xml generated Executable file
View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.jooq:jooq:3.9.5">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jooq/jooq/3.9.5/jooq-3.9.5.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jooq/jooq/3.9.5/jooq-3.9.5-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jooq/jooq/3.9.5/jooq-3.9.5-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.jooq:jooq-codegen:3.9.5">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jooq/jooq-codegen/3.9.5/jooq-codegen-3.9.5.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jooq/jooq-codegen/3.9.5/jooq-codegen-3.9.5-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jooq/jooq-codegen/3.9.5/jooq-codegen-3.9.5-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.jooq:jooq-meta:3.9.5">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jooq/jooq-meta/3.9.5/jooq-meta-3.9.5.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jooq/jooq-meta/3.9.5/jooq-meta-3.9.5-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jooq/jooq-meta/3.9.5/jooq-meta-3.9.5-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.xerial:sqlite-jdbc:3.20.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/xerial/sqlite-jdbc/3.20.0/sqlite-jdbc-3.20.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/xerial/sqlite-jdbc/3.20.0/sqlite-jdbc-3.20.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/xerial/sqlite-jdbc/3.20.0/sqlite-jdbc-3.20.0-sources.jar!/" />
</SOURCES>
</library>
</component>

13
.idea/misc.xml generated Executable file
View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Executable 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$/Games.iml" filepath="$PROJECT_DIR$/Games.iml" />
</modules>
</component>
</project>

124
.idea/uiDesigner.xml generated Executable file
View File

@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

1005
.idea/workspace.xml generated Executable file

File diff suppressed because it is too large Load Diff

19
Games.iml Executable file
View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.jooq:jooq:3.9.5" level="project" />
<orderEntry type="library" name="Maven: org.jooq:jooq-meta:3.9.5" level="project" />
<orderEntry type="library" name="Maven: org.jooq:jooq-codegen:3.9.5" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.xerial:sqlite-jdbc:3.20.0" level="project" />
</component>
</module>

49
pom.xml Executable file
View File

@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ryuuji</groupId>
<artifactId>Games</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.9.5</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>3.9.5</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>3.9.5</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.20.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

BIN
shot.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@@ -0,0 +1,8 @@
package flapy_bird;
public class App {
public static void main(String[] args) {
Game game = new Game();
game.run(30);
}
}

View File

@@ -0,0 +1,47 @@
package flapy_bird;
import java.awt.*;
public class Bird {
public int x;
public int y;
public int d;
public int velocity;
public int gravity;
public int acceleration;
public Color color;
public Bird(int x, int y, int d){
this.x = x;
this.y = y;
this.d = d;
this.color = new Color(255,230,0);
this.velocity = 0;
this.acceleration = 0;
this.gravity = 1;
}
public void update(){
this.velocity += gravity;
this.y += velocity;
velocity -= acceleration;
if(velocity >= 10){
velocity = 10;
}else if(velocity <= -20){
velocity = -20;
}
acceleration = 0;
}
public void fly(){
acceleration += 20;
}
public void draw(Graphics2D g2){
g2.setColor(color);
g2.fillOval(x,y,d,d);
}
}

View File

@@ -0,0 +1,79 @@
package flapy_bird;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class Game extends JPanel {
public Window window;
public Color background;
public Bird bird;
public ArrayList<Pipe> pipes;
public Game(){
this.background = new Color(0,0,0);
this.bird = new Bird(100,50,30);
this.pipes = new ArrayList<>();
this.pipes.add(new Pipe(Window.WIDTH));
this.pipes.add(new Pipe(Window.WIDTH+200));
this.pipes.add(new Pipe(Window.WIDTH+400));
this.window = new Window(this);
}
public void update(){
for(Pipe pipe:pipes){
pipe.update();
}
bird.update();
}
@Override
public void paintComponent(Graphics g){
Graphics2D g2 = setAntialising(g);
g2.setColor(background);
g2.fillRect(0,0,Window.WIDTH,Window.HEIGHT);
bird.draw(g2);
for(Pipe pipe:pipes){
pipe.draw(g2);
}
}
public Graphics2D setAntialising(Graphics g){
Graphics2D g2 = (Graphics2D) g;
RenderingHints hints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON
);
g2.setRenderingHints(hints);
return g2;
}
public void run(int TARGET_FPS){
long lastLoopTime = System.nanoTime();
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
long lastFpsTime = 0;
while (true) {
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
lastFpsTime += updateLength;
if (lastFpsTime >= 1000000000) {
lastFpsTime = 0;
}
this.update();
this.repaint();
long time = (lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000;
if (time > 0) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

View File

@@ -0,0 +1,34 @@
package flapy_bird;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.security.Key;
public class Keyboard implements KeyListener {
public boolean[] keys;
public Game game;
public Bird bird;
public Keyboard(Game game, Bird bird){
this.keys = new boolean[256];
this.game = game;
this.bird = bird;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
if(keys[KeyEvent.VK_SPACE])
bird.fly();
}
@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
}

View File

@@ -0,0 +1,35 @@
package flapy_bird;
import java.awt.*;
import java.util.concurrent.ThreadLocalRandom;
public class Pipe {
public int x;
public int width;
public int height;
public int gap;
public int y_gap;
public Color color;
public Pipe(int x){
this.width = 50;
this.height = Window.HEIGHT;
this.x = x;
this.gap = ThreadLocalRandom.current().nextInt(80,100);
this.y_gap = ThreadLocalRandom.current().nextInt(height/8, height/8*6);
this.color = new Color(100,250,100);
}
public void draw(Graphics2D g2){
g2.setColor(color);
g2.fillRect(x,0,width,height-y_gap);
g2.fillRect(x,height-y_gap/2,width,height);
}
public void update(){
this.x -= 8;
if(this.x + width < 0){
this.x = Window.WIDTH;
this.gap = ThreadLocalRandom.current().nextInt(80,100);
this.y_gap = ThreadLocalRandom.current().nextInt(height/8, height/8*6);
}
}
}

View File

@@ -0,0 +1,17 @@
package flapy_bird;
import javax.swing.*;
public class Window extends JFrame {
public static final int WIDTH = 600;
public static final int HEIGHT = 600;
public Window(Game game){
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.add(game);
this.setVisible(true);
this.addKeyListener(new Keyboard(game, game.bird));
}
}

View File

@@ -0,0 +1,52 @@
package game_of_life;
import javax.swing.*;
public class App {
private static JFrame frame;
private static Canvas canvas;
private final static int WIDTH = 600;
private final static int HEIGHT = 600;
public static void main(String[] args) {
frame = new JFrame();
canvas = new Canvas(WIDTH, HEIGHT);
frame.setSize(WIDTH+17, HEIGHT+40);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(canvas);
frame.setVisible(true);
run(30);
}
public static void run(int TARGET_FPS) {
long lastLoopTime = System.nanoTime();
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
long lastFpsTime = 0;
while (true) {
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
lastFpsTime += updateLength;
if (lastFpsTime >= 1000000000) {
lastFpsTime = 0;
}
canvas.update();
canvas.repaint();
long time = (lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000;
if(time>0) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

View File

@@ -0,0 +1,91 @@
package game_of_life;
import javax.swing.*;
import java.awt.*;
public class Canvas extends JPanel {
private int WIDHT;
private int HEIGHT;
private int x_cells;
private int y_cells;
private int size;
private Cell[][] cells;
private float probabilidad;
public Canvas(int width, int height) {
this.WIDHT = width;
this.HEIGHT = height;
this.size = 2;
this.x_cells = this.WIDHT / size;
this.y_cells = this.HEIGHT / size;
this.cells = new Cell[this.x_cells][this.y_cells];
this.probabilidad = 50f/100f;
setup();
}
public void setup() {
for (int x = 0; x < x_cells; x++) {
for (int y = 0; y < y_cells; y++) {
cells[x][y] = new Cell(x * size, y * size, size);
if(Math.random()>=probabilidad){
cells[x][y].alive = true;
}
}
}
}
public void update(){
countNears();
for (int x = 0; x < x_cells; x++) {
for (int y = 0; y < y_cells; y++) {
cells[x][y].update();
}
}
}
public void countNears() {
for (int x = 0; x < x_cells; x++) {
for (int y = 0; y < y_cells; y++) {
int count = 0;
if (x - 1 >= 0) {
if (y - 1 >= 0) {
count += cells[x - 1][y - 1].alive ? 1 : 0;
}
count += cells[x - 1][y].alive ? 1 : 0;
if (y + 1 < y_cells) {
count += cells[x - 1][y + 1].alive ? 1 : 0;
}
}
if (y - 1 >= 0) {
count += cells[x][y - 1].alive ? 1 : 0;
}
if (y + 1 < y_cells) {
count += cells[x][y + 1].alive ? 1 : 0;
}
if (x + 1 < x_cells) {
if (y - 1 >= 0) {
count += cells[x + 1][y - 1].alive ? 1 : 0;
}
count += cells[x + 1][y].alive ? 1 : 0;
if (y + 1 < y_cells) {
count += cells[x + 1][y + 1].alive ? 1 : 0;
}
}
cells[x][y].nears = count;
}
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(0xdfdfdf));
g2.fillRect(0,0,WIDHT,HEIGHT);
for (int x = 0; x < x_cells; x++) {
for (int y = 0; y < y_cells; y++) {
cells[x][y].draw(g2);
}
}
}
}

View File

@@ -0,0 +1,40 @@
package game_of_life;
import java.awt.*;
public class Cell {
public int x;
public int y;
public int size;
public boolean alive;
public int nears;
public Color color;
public Cell(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
this.color = new Color(0x247ba0);
}
public void draw(Graphics2D g2) {
if (alive) {
g2.setColor(color);
g2.fillRect(x, y, size, size);
}
g2.setColor(new Color(0x70c1b3));
g2.drawRect(x, y, size, size);
}
public void update() {
if (alive) {
if (nears < 2 || nears > 3) {
alive = false;
}
} else {
if (nears == 3){
alive = true;
}
}
}
}

64
src/main/java/print/App.java Executable file
View File

@@ -0,0 +1,64 @@
package print;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class App {
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static JFrame frame;
public static Canvas canvas;
public static void main(String[] args) {
frame = new JFrame();
canvas = new Canvas(WIDTH, HEIGHT);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(canvas);
frame.setVisible(true);
save();
}
public static void save() {
Container c = frame.getContentPane();
BufferedImage im = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
c.paint(im.getGraphics());
try {
ImageIO.write(im, "PNG", new File("shot.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
static void run(int fps) {
long lastLoopTime = System.nanoTime();
final int TARGET_FPS = fps;
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
long lastFpsTime = 0;
while (true) {
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
lastFpsTime += updateLength;
if (lastFpsTime >= 1000000000) {
lastFpsTime = 0;
}
canvas.setup();
canvas.repaint();
try {
Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

45
src/main/java/print/Canvas.java Executable file
View File

@@ -0,0 +1,45 @@
package print;
import javax.swing.*;
import java.awt.*;
public class Canvas extends JPanel {
public int HEIGHT;
public int WIDTH;
public int x_cells;
public int y_cells;
public int size;
public Line[][] lines;
public Canvas(int width, int height) {
super();
this.HEIGHT = height;
this.WIDTH = width;
this.size = 20;
this.x_cells = width / size;
this.y_cells = height / size;
this.lines = new Line[x_cells][y_cells];
setup();
}
public void setup() {
for (int x = 0; x < x_cells; x++) {
for (int y = 0; y < y_cells; y++) {
this.lines[x][y] = new Line(x,y,size,Math.random() <= 0.5);
}
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(new Color(0x011627));
g2.fillRect(0,0,WIDTH,HEIGHT);
for (int x = 0; x < x_cells; x++) {
for (int y = 0; y < y_cells; y++) {
this.lines[x][y].draw(g2);
}
}
}
}

46
src/main/java/print/Line.java Executable file
View File

@@ -0,0 +1,46 @@
package print;
import java.awt.*;
public class Line {
public int x1;
public int y1;
public int x2;
public int y2;
public int size;
public Color color;
public int width;
public Line(int x, int y, int size, boolean type){
this.size = size;
this.width = 5;
if(type){
this.x1 = x * size;
this.y1 = y * size;
this.x2 = x * size + size;
this.y2 = y * size + size;
}else{
this.x1 = x * size;
this.y1 = y * size + size;
this.x2 = x * size + size;
this.y2 = y * size;
}
setRandomColor();
}
public void setRandomColor(){
if(Math.random() >= 0.5){
color = new Color(0x2ec4b6);
}else{
color = new Color(0xe71d36);
}
}
public void draw(Graphics2D g2){
//setRandomColor();
g2.setStroke(new BasicStroke(width));
g2.setColor(this.color);
g2.drawLine(x1,y1,x2,y2);
}
}

View File

@@ -0,0 +1,25 @@
package skeleton;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class Canvas extends JPanel {
private ArrayList<GameObject> oList;
public Canvas(){
this.oList = new ArrayList<>();
}
@Override
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
for(GameObject go: oList){
go.draw(g2);
}
}
public void addGameObject(GameObject go){
this.oList.add(go);
}
}

View File

@@ -0,0 +1,8 @@
package skeleton;
interface Context {
void move(Movement movement);
void exit();
void reset();
}

View File

@@ -0,0 +1,33 @@
package skeleton;
import javax.swing.*;
public class Game implements Context{
public Canvas canvas;
public Game(){
canvas = new Canvas();
}
public Canvas getCanvas() {
return canvas;
}
@Override
public void move(Movement movement) {
System.out.println(movement);
}
@Override
public void exit() {
System.out.println("EXIT");
}
@Override
public void reset() {
System.out.println("RESET");
}
}

View File

@@ -0,0 +1,8 @@
package skeleton;
import java.awt.*;
interface GameObject {
void draw(Graphics2D g2);
void update();
}

View File

@@ -0,0 +1,48 @@
package skeleton;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Keyboard implements KeyListener{
public boolean[] keys;
public Context context;
public Keyboard(Context context){
this.keys = new boolean[256];
this.context = context;
}
@Override
public void keyTyped(KeyEvent e){
customKeyTyped(e);
}
@Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
if(keys[KeyEvent.VK_UP]) context.move(Movement.UP);
if(keys[KeyEvent.VK_RIGHT]) context.move(Movement.RIGHT);
if(keys[KeyEvent.VK_LEFT]) context.move(Movement.LEFT);
if(keys[KeyEvent.VK_DOWN]) context.move(Movement.DOWN);
if(keys[KeyEvent.VK_SPACE]) context.move(Movement.JUMP);
if(keys[KeyEvent.VK_ESCAPE]) context.exit();
if(keys[KeyEvent.VK_R]) context.reset();
customKeyPressed(e);
}
@Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
customKeyReleased(e);
}
public void customKeyPressed(KeyEvent e){
}
public void customKeyTyped(KeyEvent e){
}
public void customKeyReleased(KeyEvent e){
}
}

View File

@@ -0,0 +1,5 @@
package skeleton;
public enum Movement {
UP,RIGHT,DOWN,LEFT,JUMP;
}

View File

@@ -0,0 +1,13 @@
package skeleton;
public class Start implements Runnable {
public void run(){
Window window = new Window();
window.setGameState(new Game());
}
public static void main(String[] args) {
Thread t = new Thread(new Start());
t.start();
}
}

View File

@@ -0,0 +1,20 @@
package skeleton;
import javax.swing.*;
public class Window extends JFrame {
public static final int WIDTH = 600;
public static final int HEIGHT = 600;
public Window(){
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void setGameState(Game game){
this.add(game.getCanvas());
this.addKeyListener(new Keyboard(game));
}
}

54
src/main/java/snake/App.java Executable file
View File

@@ -0,0 +1,54 @@
package snake;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class App {
private static JFrame frame;
private static Canvas canvas;
public final static int WIDTH = 600;
public final static int HEIGHT = 600;
public static void main(String[] args) {
frame = new JFrame();
canvas = new Canvas(WIDTH, HEIGHT);
frame.setSize(WIDTH + 17, HEIGHT + 40);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(canvas);
frame.setVisible(true);
frame.addKeyListener(new Keyboard(canvas));
run(5);
}
public static void run(int TARGET_FPS) {
long lastLoopTime = System.nanoTime();
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
long lastFpsTime = 0;
while (true) {
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
lastFpsTime += updateLength;
if (lastFpsTime >= 1000000000) {
lastFpsTime = 0;
}
canvas.update();
canvas.repaint();
long time = (lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000;
if (time > 0) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

77
src/main/java/snake/Canvas.java Executable file
View File

@@ -0,0 +1,77 @@
package snake;
import javax.swing.*;
import java.awt.*;
public class Canvas extends JPanel {
public int width;
public int height;
public int x_cells;
public int y_cells;
public final int size = 40;
public Snake snake;
public Color background;
public Color foreground;
public Color fruit_color;
public Cell fruit;
public int movement;
public Canvas(int width, int height) {
this.width = width;
this.height = height;
this.x_cells = width / size;
this.y_cells = height / size;
this.background = new Color(0,0,0);
this.foreground = new Color(0xffffff);
this.fruit_color = new Color(200,60,0);
this.snake = new Snake(5,5,size);
this.fruit = new Cell((int)(Math.random()*x_cells),(int)(Math.random()*y_cells),size);
}
public void reset(){
this.snake = new Snake(5,5,size);
this.fruit = new Cell((int)(Math.random()*x_cells),(int)(Math.random()*y_cells),size);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(background);
g2.fillRect(0,0,width,height);
g2.setColor(fruit_color);
this.fruit.draw(g2);
this.snake.draw(g2);
g2.setColor(foreground);
for (int x = 0; x < x_cells; x++) {
for (int y = 0; y < y_cells; y++) {
g2.drawRect(x * size, y * size, size, size);
}
}
}
public void moveFruit(){
this.fruit.move((int)(Math.random()*x_cells),(int)(Math.random()*y_cells));
}
public void update() {
snake.move();
if(snake.touchItself()){
lost();
}
if(snake.body.getFirst().collide(fruit)){
moveFruit();
snake.add(snake.body.getLast());
}
}
public void lost(){
snake.color = fruit_color;
snake.alive = false;
}
}

86
src/main/java/snake/Cell.java Executable file
View File

@@ -0,0 +1,86 @@
package snake;
import java.awt.*;
public class Cell {
public int x;
public int y;
public int size;
public int face;
public Cell(int x, int y, int size) {
this.x = x * size;
this.y = y * size;
this.size = size;
this.face = 1;
}
public Cell(Cell parent) {
this.face = parent.face;
this.size = parent.size;
switch (this.face) {
case 0:
this.x = parent.x;
this.y = parent.y + size;
break;
case 1:
this.x = parent.x - size;
this.y = parent.y;
break;
case 2:
this.x = parent.x;
this.y = parent.y - size;
break;
case 3:
this.x = parent.x + size;
this.y = parent.y;
break;
}
}
public void draw(Graphics2D g2) {
g2.fillRect(x, y, size, size);
}
public boolean collide(Cell cell) {
return cell.x == this.x && cell.y == this.y;
}
public void move(int x, int y) {
this.x = x * size;
this.y = y * size;
}
public void move(int dir) {
if (dir == 0) {
this.y -= size;
if(this.y < 0){
this.y = App.HEIGHT;
}
this.face = 0;
}
if (dir == 1 ) {
this.x += size;
if(this.x > App.WIDTH){
this.x = 0;
}
this.face = 1;
}
if (dir == 2) {
this.y += size;
if(this.y > App.HEIGHT){
this.y = 0;
}
this.face = 2;
}
if (dir == 3) {
this.x -= size;
if(this.x < 0){
this.x = App.WIDTH;
}
this.face = 3;
}
}
}

View File

@@ -0,0 +1,32 @@
package snake;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Keyboard implements KeyListener {
public Canvas canvas;
public Snake snake;
public Keyboard(Canvas canvas){
this.canvas = canvas;
this.snake = canvas.snake;
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP && snake.face != 2) snake.face = 0;
if(e.getKeyCode() == KeyEvent.VK_RIGHT && snake.face != 3) snake.face = 1;
if(e.getKeyCode() == KeyEvent.VK_DOWN && snake.face != 0) snake.face = 2;
if(e.getKeyCode() == KeyEvent.VK_LEFT && snake.face != 1) snake.face = 3;
if(e.getKeyCode() == KeyEvent.VK_R) canvas.reset();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}

59
src/main/java/snake/Snake.java Executable file
View File

@@ -0,0 +1,59 @@
package snake;
import java.awt.*;
import java.util.LinkedList;
public class Snake {
public LinkedList<Cell> body;
public int size;
public Color color;
public int face;
public boolean alive;
public Snake(int x, int y, int size) {
this.body = new LinkedList<>();
this.size = size;
this.color = new Color(20, 200, 100);
for(int i = 0; i <= 3; i++)
this.body.add(new Cell(x-i, y, this.size));
this.alive = true;
this.face = 1;
}
public void move() {
if(alive) {
int lastFace = body.get(0).face;
for (int x = 0; x < body.size(); x++) {
if (x != 0) {
int aux = body.get(x).face;
body.get(x).move(lastFace);
lastFace = aux;
} else {
body.get(x).move(face);
}
}
}
}
public void add(Cell last){
body.add(new Cell(last));
}
public boolean touchItself(){
for(int x = 0; x < body.size(); x++){
for(int y = x + 1; y < body.size(); y++){
if(body.get(x).collide(body.get(y))){
return true;
}
}
}
return false;
}
public void draw(Graphics2D g2) {
g2.setColor(color);
for (Cell cell : body) {
cell.draw(g2);
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
target/classes/print/App.class Executable file

Binary file not shown.

BIN
target/classes/print/Canvas.class Executable file

Binary file not shown.

BIN
target/classes/print/Line.class Executable file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
target/classes/snake/App.class Executable file

Binary file not shown.

BIN
target/classes/snake/Canvas.class Executable file

Binary file not shown.

BIN
target/classes/snake/Cell.class Executable file

Binary file not shown.

Binary file not shown.

BIN
target/classes/snake/Snake.class Executable file

Binary file not shown.