-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.java
123 lines (104 loc) · 1.93 KB
/
snake.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
/*<applet code="snake"
* archive = "snake.jar"
* width=1920 height=1080 ></applet>*/
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
public class snake extends Applet implements Runnable,KeyListener
{
int x,y,r,px,py;
char arrow;
Thread t;
public void init()
{
x = getWidth()/2;
px =x*2;
y = getHeight()/2;
py =y*2;
r=30;
t=new Thread(this);
t.start();
this.setBackground(Color.BLUE);
//this.setBackground(Color.RED);
}
public void paint(Graphics g)
{
g.fillOval(x,y,r,r);
g.setColor(Color.BLACK);
}
public void run()
{
while(true)
{
try
{
while(y>((py*3)/7))
{
addKeyListener(this);
this.setBackground(Color.ORANGE);
y=y-2;
repaint();
Thread.sleep(10);
}
while(x>((px*3)/7))
{
addKeyListener(this);
this.setBackground(Color.YELLOW);
x=x-2;
repaint();
Thread.sleep(10);
}
while(y<((py*4)/7))
{
addKeyListener(this);
this.setBackground(Color.RED);
y=y+2;
repaint();
Thread.sleep(10);
}
while(x<((px*4)/7))
{
addKeyListener(this);
this.setBackground(Color.GREEN);
x=x+2;
repaint();
Thread.sleep(10);
}
}
catch(Exception e)
{
System.out.println("ERROR");
}
}
}
public void keyPressed(KeyEvent ke)
{
}
public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
arrow = ke.getKeyChar();
System.out.println(arrow);
System.out.println(arrow);
if (arrow == '1')
{
System.out.print("w ");
}
if (arrow == '2')
{
System.out.print("a ");
}
if (arrow == '3')
{
System.out.print("s ");
}
if (arrow == '4')
{
System.out.print("d ");
}
}
}