| C:\eclipse\workspace\SimplePong\src\PongCanvas.java |
1 /**
2 * PongCanvas contains a Simple Pong game engine
3 *
4 * Simple Pong is available for non-commercial use only!
5 *
6 * @author Ziga Hajdukovic
7 * @version 1.0
8 */
9
10 import javax.microedition.lcdui.*;
11
12 import com.nokia.mid.ui.FullCanvas;
13
14 class PongCanvas extends FullCanvas implements Runnable
15 {
16 private final PongMIDlet midlet;
17
18 int canvasWidth;
19 int canvasHeight;
20
21 boolean pongRunning;
22
23 public static final int BALL_SIZE = 8;
24 // ball position
25 int ball_x;
26 int ball_y;
27 // ball velocity vector
28 int ball_vx;
29 int ball_vy;
30
31 public static final int PADDLE_WIDTH = 8;
32 public static final int PADDLE_HEIGHT = 32;
33 // paddle position
34 int paddle_x;
35 int paddle_y;
36 // paddle velocity vector
37 int paddle_vy;
38
39 public static final int SOFTKEY_1 = -6;
40 public static final int SOFTKEY_2 = -7;
41
42 public static boolean command_up;
43 public static boolean command_down;
44
45 PongCanvas(PongMIDlet midlet)
46 {
47 this.midlet = midlet;
48
49 canvasWidth = this.getWidth();
50 canvasHeight = this.getHeight();
51
52 paddle_x = PADDLE_WIDTH;
53 paddle_y = canvasHeight / 2 - PADDLE_HEIGHT / 2;
54 paddle_vy = 2;
55
56 ball_x = canvasWidth / 2 - BALL_SIZE / 2;
57 ball_y = canvasHeight / 2 - BALL_SIZE / 2;
58 ball_vx = 3;
59 ball_vy = 2;
60
61 pongRunning = true;
62 }
63
64 public void run()
65 {
66 while(pongRunning)
67 {
68 // update ball
69 ball_x += ball_vx;
70 ball_y += ball_vy;
71
72 // detect and handle ball-wall collision
73 if ( (ball_y < 0) || ((ball_y + BALL_SIZE) > canvasHeight) )
74 {
75 ball_vy = -ball_vy;
76 }
77 if ( ball_x < 0 )
78 {
79 ball_vx = -ball_vx;
80 // TODO: right player scores a point!
81 }
82 if ( (ball_x + BALL_SIZE) > canvasWidth )
83 {
84 ball_vx = -ball_vx;
85 // TODO: left player scores a point!
86 }
87
88 // detect and handle for ball-paddle collision
89 if ( (ball_x < paddle_x + PADDLE_WIDTH) &&
90 (ball_y > paddle_y - BALL_SIZE) && (ball_y < paddle_y + PADDLE_HEIGHT) )
91 {
92 ball_vx = Math.abs(ball_vx);
93 }
94
95 if (command_up)
96 {
97 paddle_y -= paddle_vy;
98 if (paddle_y < 0)
99 paddle_y = 0;
100 }
101 if (command_down)
102 {
103 paddle_y += paddle_vy;
104 if (paddle_y + PADDLE_HEIGHT > canvasHeight)
105 paddle_y = canvasHeight - PADDLE_HEIGHT;
106 }
107
108 // TODO: update AI paddle
109
110 // 30 ms delay
111 try
112 {
113 Thread.sleep(30);
114 }
115 catch (Exception e)
116 {
117 e.printStackTrace();
118 }
119
120 // request a repaint()
121 repaint();
122 // force pending repaints
123 serviceRepaints();
124 }
125 }
126
127 public void paint(Graphics g)
128 {
129 // set background color
130 g.setColor(0x000000);
131 // fill background
132 g.fillRect(0, 0, canvasWidth, canvasHeight);
133
134 // set foreground color
135 g.setColor(0x00DD00);
136
137 // paint the ball
138 g.fillRect(ball_x, ball_y, BALL_SIZE, BALL_SIZE);
139
140 // paint the paddle
141 g.fillRect(paddle_x, paddle_y, PADDLE_WIDTH, PADDLE_HEIGHT);
142
143 // TODO: paint AI paddle
144
145 // TODO: paint score
146
147 // draw keyPressed debug info
148 //g.drawString(skey_debug,0,0,Graphics.TOP | Graphics.LEFT);
149 }
150
151 //String skey_debug = "";
152
153 public void keyPressed(int key)
154 {
155 //skey_debug = ""+key +" / "+ getGameAction(key);
156
157 // quit on right softkey press.
158 if(key == SOFTKEY_2)
159 {
160 midlet.exitRequested();
161 }
162
163 int game_action = getGameAction(key);
164
165 switch(game_action)
166 {
167 case UP:
168 command_up = true;
169 break;
170
171 case DOWN:
172 command_down = true;
173 break;
174 }
175 }
176
177 public void keyReleased(int key)
178 {
179 int game_action = getGameAction(key);
180
181 switch(game_action)
182 {
183 case UP:
184 command_up = false;
185 break;
186
187 case DOWN:
188 command_down = false;
189 break;
190 }
191 }
192 }
193