Created
May 18, 2019 03:52
-
-
Save Samielleuch/a429ff92e1819f9b3dd0f2f6f8336c48 to your computer and use it in GitHub Desktop.
added Menu Screen
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Encoding" addBOMForNewFiles="with NO BOM" /> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK"> | |
<output url="file://$PROJECT_DIR$/out" /> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/Game.iml" filepath="$PROJECT_DIR$/Game.iml" /> | |
</modules> | |
</component> | |
</project> |
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
<?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> |
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
import javax.imageio.ImageIO; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
public class Background { | |
private BufferedImage image ; | |
private double x; | |
private double y; | |
private double dx; | |
private double dy; | |
public Background(String s , double ms) { | |
try { | |
//importing resources | |
image = ImageIO.read( | |
getClass().getResourceAsStream(s) | |
); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void setPosition(double x, double y ) { | |
this.x = x%GamePanel.WIDTH; | |
this.y = y%GamePanel.HEIGHT; | |
} | |
public void setVector(double dx, double dy ) { | |
this.dx = dx; | |
this.dy = dy; | |
} | |
public void update() { | |
x+=dx; | |
y+=dy; | |
} | |
public void draw (Graphics2D g){ | |
//draw the background | |
g.drawImage(image,(int)x ,(int)y,null ); | |
//keep redrawing new image if moves away | |
if(x < 0) { | |
g.drawImage( | |
image, | |
(int)x + GamePanel.WIDTH, | |
(int)y, | |
null | |
); | |
} | |
if(x > 0) { | |
g.drawImage( | |
image, | |
(int)x - GamePanel.WIDTH, | |
(int)y, | |
null | |
); | |
} | |
} | |
} |
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
import java.awt.Dimension; | |
import java.awt.Toolkit; | |
import javax.swing.JFrame; | |
public class Game { | |
public static void main(String[] args) { | |
JFrame window = new JFrame("Simple Graphics Program"); | |
window.setContentPane(new GamePanel()); | |
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
window.setResizable(false); | |
window.pack(); | |
window.setLocationRelativeTo(null); | |
window.setVisible(true); | |
} | |
} |
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
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
import java.awt.image.BufferedImage; | |
public class GamePanel extends JPanel implements Runnable, KeyListener { | |
private static final long serialVersionUID = 1L; | |
// dimensions | |
public static final int WIDTH = 320; | |
public static final int HEIGHT = 240; | |
public static final int SCALE = 2; | |
// game thread | |
private Thread thread; | |
private boolean running; | |
private int FPS = 60; | |
private long targetTime = 1000 / FPS; | |
//image | |
private BufferedImage image; | |
private Graphics2D g; | |
// game state manager | |
private GameStateManager gsm; | |
public GamePanel() { | |
super(); | |
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); | |
setFocusable(true); | |
requestFocus(); | |
} | |
public void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
} | |
//when its loaded | |
public void addNotify() { | |
super.addNotify(); | |
if (thread == null) { | |
thread = new Thread(this); | |
addKeyListener(this); | |
thread.start(); | |
} | |
} | |
private void init() { | |
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); | |
g = (Graphics2D) image.getGraphics(); | |
running = true; | |
// create the game state manager : | |
gsm = new GameStateManager(); | |
} | |
@Override | |
public void run() { | |
// on initialise | |
init(); | |
//timers for the game | |
long start; | |
long elapsed; | |
long wait; | |
//gameLoop | |
while (running) { | |
start = System.nanoTime(); | |
update(); | |
draw(); | |
drawToScreen(); | |
elapsed = System.nanoTime() - start; | |
wait = targetTime - elapsed / 1000000; | |
if (wait < 0) wait = 10; | |
try { | |
Thread.sleep(wait); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private void draw() { | |
gsm.draw(g); | |
} | |
private void update() { | |
gsm.update(); | |
} | |
private void drawToScreen() { | |
Graphics g2 = getGraphics(); | |
//draw the canvas | |
g2.drawImage(image, 0, 0, | |
WIDTH * SCALE, HEIGHT * SCALE, null); | |
g2.dispose(); | |
} | |
@Override | |
public void keyTyped(KeyEvent e) { | |
} | |
@Override | |
public void keyPressed(KeyEvent key) { | |
gsm.keyPressed(key.getKeyCode()); | |
} | |
@Override | |
public void keyReleased(KeyEvent key) { | |
gsm.keyReleased(key.getKeyCode()); | |
} | |
} |
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
public abstract class GameState { | |
//needs refrence so it can change state | |
protected GameStateManager gsm ; | |
public abstract void init(); | |
public abstract void update(); | |
public abstract void draw(java.awt.Graphics2D g); | |
public abstract void keyPressed(int k); | |
public abstract void keyReleased(int k); | |
} |
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
import java.awt.*; | |
import java.util.ArrayList; | |
public class GameStateManager { | |
private ArrayList<GameState> gameStates; | |
private int currentState ; | |
public static final int MENUSTATE= 0 ; | |
public static final int LEVEL1STATE=1; | |
public GameStateManager() { | |
gameStates = new ArrayList<GameState>(); | |
currentState = MENUSTATE ; | |
gameStates.add(new MenuState(this)); | |
} | |
public void setGameState(int state) { | |
currentState = state ; | |
gameStates.get(currentState).init(); | |
} | |
public void draw(java.awt.Graphics2D g){ | |
gameStates.get(currentState).draw(g); | |
} | |
public void keyPressed(int k ) { | |
gameStates.get(currentState).keyPressed(k); | |
} | |
public void keyReleased(int k ) { | |
gameStates.get(currentState).keyReleased(k); | |
} | |
public void update() { | |
gameStates.get(currentState).update(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment