-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTest_Game.java
123 lines (89 loc) · 3.01 KB
/
Test_Game.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* This is the kick-off class for a little hack where a GUI pretends to be a console game.
* Also creates World (model), which gets passed into Controller.
* Test_Game also world as view.
* @author gliebchen
* @version 1.0
* @since 2016-03-16
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test_Game {
boolean gameRunning = true;
private final int maxRow = 20;
private final int maxCol = 49;
private JFrame mainFrame;
private JLabel gameDisplay;
private World worldy;
private Controller beardyGuy;
public Test_Game(){
prepareGame();
}
public static void main(String[] args){
Test_Game game = new Test_Game();
game.showKeyListenerDemo();
}
private void prepareGame(){
mainFrame = new JFrame("Gernot's Game Hack");
mainFrame.setSize(500,800);
//mainFrame.setLayout(new GridLayout(3, 1));
gameDisplay = new JLabel("",JLabel.CENTER );
gameDisplay.setForeground(Color.YELLOW);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
mainFrame.getContentPane().setBackground(Color.BLACK);
mainFrame.add(gameDisplay);
mainFrame.setVisible(true);
}
private void showKeyListenerDemo(){
worldy = new World(maxRow, maxCol);
beardyGuy = new Controller(worldy);
mainFrame.addKeyListener(new CustomKeyListener());
mainFrame.setVisible(true);
while(gameRunning){
try {
Thread.sleep(50); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
beardyGuy.moveEverything();
displayMap();
}
}
private void displayMap(){
String output = new String();
char[][] map = worldy.getMap();
output = "<html><pre>C:\\game ><br>";
for(int rowCounter=maxRow;rowCounter>-1;rowCounter--){
output = output+String.valueOf(map[rowCounter])+"<br>";
}
output = output+"Score: "+String.valueOf(beardyGuy.getScoreAndLives()[0])
+"<br>Lives: "+String.valueOf(beardyGuy.getScoreAndLives()[1]) + "</pre></html>";
gameDisplay.setText(output);
}
class CustomKeyListener implements KeyListener{
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
gameRunning=false;
}if(e.getKeyCode() == KeyEvent.VK_UP){
beardyGuy.userInput('u');
}if(e.getKeyCode() == KeyEvent.VK_DOWN){
beardyGuy.userInput('d');
}if(e.getKeyCode() == KeyEvent.VK_LEFT){
beardyGuy.userInput('l');
}if(e.getKeyCode() == KeyEvent.VK_RIGHT){
beardyGuy.userInput('r');
}if(e.getKeyCode() == KeyEvent.VK_SPACE){
beardyGuy.shoot();
}
}
public void keyReleased(KeyEvent e) {
}
}
}