Skip to content

Instantly share code, notes, and snippets.

@Samielleuch
Created May 18, 2019 03:52
Show Gist options
  • Save Samielleuch/a429ff92e1819f9b3dd0f2f6f8336c48 to your computer and use it in GitHub Desktop.
Save Samielleuch/a429ff92e1819f9b3dd0f2f6f8336c48 to your computer and use it in GitHub Desktop.
added Menu Screen
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
<?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>
<?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>
<?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>
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
);
}
}
}
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);
}
}
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());
}
}
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);
}
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();
}
}
import java.awt.*;
import java.awt.event.KeyEvent;
public class MenuState extends GameState {
private Background bg;
//to keep track of the selected option
private int currentChoice = 0 ;
private String[] options= {
"Start",
"Help",
"Quit"
};
private Color titleColor;
private Font titleFont;
private Font font;
@Override
public void init() {
}
@Override
public void update() {
bg.update();
}
@Override
public void draw(Graphics2D g) {
//draw background
bg.draw(g);
//draw title
g.setColor(titleColor);
g.setFont(titleFont);
g.drawString("Asma game ",80,70);
// draw menu options
g.setFont(font);
for (int i = 0; i < options.length ; i++) {
if (i == currentChoice ) {
g.setColor(Color.BLACK);
}else {
g.setColor(Color.RED);
}
// draw them binethom 15 pixel
g.drawString(options[i],145,140 + i * 15 );
}
}
private void select() {
if(currentChoice == 0 ){
//start
}
if(currentChoice == 1 ){
//Help
}
if(currentChoice == 2 ){
//quit
System.exit(0);
}
}
@Override
public void keyPressed(int k) {
if (k == KeyEvent.VK_ENTER) {
select();
}
if (k == KeyEvent.VK_UP) {
//move up
currentChoice--;
//if we reach the top end go to the last
if (currentChoice == -1){
currentChoice = options.length -1 ;
}
}
if (k == KeyEvent.VK_DOWN) {
//move down
currentChoice++;
//if we reach the end end go to the top
if (currentChoice == options.length ){
currentChoice = 0 ;
}
}
}
@Override
public void keyReleased(int k) {
}
public MenuState(GameStateManager gsm) {
this.gsm = gsm;
try {
bg = new Background("/Resources/Background/menubg.gif",1);
//moves to the left
bg.setVector(-0.1,0);
titleColor = new Color(128,0,0);
titleFont = new Font ("Century Gothic",Font.PLAIN , 28 );
font = new Font ("Arial",Font.PLAIN , 12 );
}catch(Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment