Created
November 3, 2017 12:23
-
-
Save memish/f07b68d0d05757fe0a05fff100d9b56c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//STARTER CLASS | |
import javax.swing.JFrame; | |
public class Starter extends JFrame { | |
public Starter() | |
{ | |
add(new Board()); | |
setTitle("Board"); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
setSize(500,500); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
setResizable(false); | |
} | |
public static void main(String[] args) { | |
new Starter(); | |
} | |
} | |
//============================================== | |
//Board Class | |
//=========================================== | |
import java.awt.event.MouseListener; | |
import java.awt.event.MouseEvent; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Font; | |
import java.awt.FontMetrics; | |
import java.awt.Graphics; | |
import java.awt.Toolkit; | |
import java.awt.event.KeyAdapter; | |
import java.awt.event.KeyEvent; | |
import javax.swing.Timer; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.Random; | |
import javax.swing.ImageIcon; | |
import javax.swing.JPanel; | |
import javax.imageio.*; | |
import java.awt.image.*; | |
import java.io.*; | |
public class Board extends JPanel implements Runnable, MouseListener | |
{ | |
boolean ingame = true; | |
private Dimension d; | |
int BOARD_WIDTH=500; | |
int BOARD_HEIGHT=500; | |
int x = 0; | |
BufferedImage img; | |
String message = "Click Board to Start"; | |
private Thread animator; | |
Alien[][] a = new Alien[3][5]; | |
GameChar pl = new Player(BOARD_WIDTH/2, BOARD_HEIGHT-30); | |
public Board() | |
{ | |
addKeyListener(new TAdapter()); | |
addMouseListener(this); | |
setFocusable(true); | |
d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT); | |
setBackground(Color.black); | |
int ax = 10; | |
int ay = 10; | |
for(int r=0; r<a.length; r++){ | |
for(int c=0; c<a[0].length; c++){ | |
a[r][c] = new Alien(ax,ay); | |
ax += 30; | |
} | |
ax = 10; | |
ay += 30; | |
} | |
/* | |
try { | |
img = ImageIO.read(this.getClass().getResource("mount.jpg")); | |
} catch (IOException e) { | |
System.out.println("Image could not be read"); | |
// System.exit(1); | |
} | |
*/ | |
if (animator == null || !ingame) { | |
animator = new Thread(this); | |
animator.start(); | |
} | |
setDoubleBuffered(true); | |
} | |
public void paint(Graphics g) | |
{ | |
super.paint(g); | |
g.setColor(Color.white); | |
g.fillRect(0, 0, d.width, d.height); | |
//g.fillOval(x,y,r,r); | |
g.setColor(Color.red); | |
g.fillRect(pl.x, pl.y, 20, 20); | |
for(int r=0; r<a.length; r++){ | |
for(int c=0; c<a[0].length; c++){ | |
g.fillRect(a[r][c].x, a[r][c].y, 20, 20); | |
}} | |
Font small = new Font("Helvetica", Font.BOLD, 14); | |
FontMetrics metr = this.getFontMetrics(small); | |
g.setColor(Color.black); | |
g.setFont(small); | |
g.drawString(message, 10, d.height-60); | |
if (ingame) { | |
// g.drawImage(img,0,0,200,200 ,null); | |
} | |
Toolkit.getDefaultToolkit().sync(); | |
g.dispose(); | |
} | |
private class TAdapter extends KeyAdapter { | |
public void keyReleased(KeyEvent e) { | |
int key = e.getKeyCode(); | |
} | |
public void keyPressed(KeyEvent e) { | |
System.out.println( e.getKeyCode()); | |
message = "Key Pressed: " + e.getKeyCode(); | |
int key = e.getKeyCode(); | |
if(key==39){ | |
} | |
} | |
} | |
public void mousePressed(MouseEvent e) { | |
int x = e.getX(); | |
int y = e.getY(); | |
} | |
public void mouseReleased(MouseEvent e) { | |
} | |
public void mouseEntered(MouseEvent e) { | |
} | |
public void mouseExited(MouseEvent e) { | |
} | |
public void mouseClicked(MouseEvent e) { | |
} | |
public void run() { | |
long beforeTime, timeDiff, sleep; | |
beforeTime = System.currentTimeMillis(); | |
int animationDelay = 500; | |
long time = | |
System.currentTimeMillis(); | |
while (true) {//infinite loop | |
// spriteManager.update(); | |
repaint(); | |
try { | |
time += animationDelay; | |
Thread.sleep(Math.max(0,time - | |
System.currentTimeMillis())); | |
}catch (InterruptedException e) { | |
System.out.println(e); | |
}//end catch | |
}//end while loop | |
}//end of run | |
}//end of class | |
//========================================= | |
//GameChar | |
//======================================== | |
/** | |
* Write a description of class GameChar here. | |
* | |
* @author (your name) | |
* @version (a version number or a date) | |
*/ | |
public class GameChar | |
{ | |
int x; | |
int y; | |
public GameChar(int x, int y){ | |
this.x = x; | |
this.y = y; | |
} | |
} | |
//=================================== | |
//Alien Class | |
//=================================== | |
public class Alien extends GameChar | |
{ | |
boolean isVis; | |
public Alien(int x, int y){ | |
super(x,y); | |
isVis = true; | |
} | |
} | |
//========================= | |
//Player | |
//=========================== | |
public class Player extends GameChar | |
{ | |
public Player(int x, int y){ | |
super(x,y); | |
} | |
} | |
//==================================== | |
//Shot Class | |
//==================================== | |
public class Shot extends GameChar | |
{ | |
public Shot(int x, int y){ | |
super(x,y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment