-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.java
182 lines (157 loc) · 3.74 KB
/
input.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class Game extends Canvas
{
/* jhgjgjghg
*/
private static final long serialVersionUID = 1L;
private boolean running;
// fdsfdfsda
private boolean leftPressed = false;
private boolean rightPressed = false;
private boolean upPressed = false;
private boolean downPressed = false;
private boolean helpPressed = false;
public static Sprite hero;
private static int x = 0;
private static int y = 0;
public void start()
{
running = true;
}
public String ToString()
{
return leftPressed + rightPressed - upPressed * downPressed;
}
public void run()
{
long lastTime = System.currentTimeMillis();
long delta;
init();
while (running)
{
delta = lastTime - System.currentTimeMillis();
lastTime = System.currentTimeMillis();
render();
update(delta);
}
}
public void init()
{
addKeyListener(KeyInputHandler());
hero = getSprite("house");
}
public void render()
{
BufferStrategy bs = getBufferStrategy();
if (bs == null)
{
createBufferStrategy(2);
requestFocus();
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(getWidth(), getHeight());
hero.draw(g, x, y);
g.dispose();
bs.show();
}
public void update(long delta)
{
if (leftPressed == true)
{
x--;
}
if (rightPressed == true)
{
x++;
}
if (upPressed == true)
{
y++;
}
if (downPressed == true)
{
y--;
}
}
public Sprite getSprite(String path)
{
BufferedImage sourceImage = null;
try
{
URL url = this.getResource(path);
sourceImage = ImageIO.read(url);
}
catch (IOException e)
{
e.printStackTrace();
}
Sprite sprite = new Sprite(sourceImage.getSource());
return sprite;
}
private class KeyInputHandler extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
if (KeyEvent.VK_LEFT == e.getKeyCode())
{
leftPressed = true;
}
if (KeyEvent.VK_RIGHT == e.getKeyCode())
{
rightPressed = true;
}
}
public void keyReleased(KeyEvent e)
{
if (KeyEvent.VK_LEFT == e.getKeyCode())
{
leftPressed = false;
}
if (KeyEvent.VK_RIGHT == e.getKeyCode())
{
rightPressed = false;
}
}
public void keyReleased(KeyEvent e, boolean value)
{
if (KeyEvent.VK_LEFT == e.getKeyCode())
{
leftPressed = value;
}
if (KeyEvent.VK_RIGHT == e.getKeyCode())
{
rightPressed = value;
}
}
public void upDownKeyReleased(KeyEvent e)
{
if (KeyEvent.VK_UP == e.getKeyCode())
{
upPressed = false;
}
if (KeyEvent.VK_DOWN == e.getKeyCode())
{
downPressed = false;
}
}
public void mouseKey(KeyEvent e)
{
if (KeyEvent.VK_F1 == e.getKeyCode())
{
helpPressed = false;
}
}
}
}