Web And Videogame Development

Papervision Pong

Posted: December 14th, 2008 | Author: | Filed under: Flash | No Comments »

Pong made with Papervision 3D. It is pretty wonky, but it served its purpose and allowed me a chance to become acclimated with Papervision. Use the up and down arrows to move your paddle on the left.

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package  {
 
	import flash.display.*;
	import flash.events.*;
	import flash.filters.BlurFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import org.papervision3d.core.Number3D;
	import org.papervision3d.cameras.*;
	import org.papervision3d.scenes.*;
	import org.papervision3d.objects.*;
	import org.papervision3d.materials.*;
 
	public class Main extends Sprite {
 
		private var container:Sprite;
		private var camera:Camera3D;
		private var scene:MovieScene3D;
 
		private var paddle_one:Cube;
		private var paddle_two:Cube;
 
		private var bg_plane:Plane;
		private var bg_net:Plane;
 
		private var ball:Sphere;
 
		// Ball X & Y velocities
		private var ballVelX:Number;
		private var ballVelY:Number;
 
		// Ball filter array
		private var filter:Array;
 
		// Paddle velocities
		private var paddle_one_vel:Number;
		private var paddle_two_vel:Number;
 
		// Score variables
		private var paddle_one_score:int;
		private var paddle_two_score:int;
 
		// Score TextField
		private var scores:TextField;
		private var mc_score:MovieClip;
		private var score_mat:MovieMaterial;
		private var score_plane:Plane;
 
		// Keyboard helper
		private var keypressed:String;
 
		public function Main() {
 
			// Create Container
			container = new Sprite();
			container.x = stage.stageWidth / 2;
			container.y = stage.stageHeight / 2;
			addChild(container);
 
			// Create new scene
			scene = new MovieScene3D(container);
 
			// Create camera
			camera = new Camera3D();
 
			// Create paddle one
			paddle_one = new Cube(new WireframeMaterial(0x000000), 50, 100, 300, 1, 1, 1);
			paddle_one.moveLeft(900);
			scene.addChild(paddle_one);
			paddle_one_vel = 30;
 
			// Create paddle two
			paddle_two = new Cube(new WireframeMaterial(0x000000), 50, 100, 300, 1, 1, 1);
			paddle_two.moveRight(900);
			scene.addChild(paddle_two);
			paddle_two_vel = 30;
 
			// Create ball
			ball = new Sphere(new WireframeMaterial(0xFF0000), 50, 8, 8);
			scene.addChild(ball);
			ballVelX = 12;
			ballVelY = 12;
 
			// Ball blur filter
			filter = new Array();
 
			// Create background
			bg_plane = new Plane(new ColorMaterial(0xDDDDFF), 2400, 1800);
			bg_plane.moveForward(200);
			scene.addChild(bg_plane);
 
			bg_net = new Plane(new ColorMaterial(0xFFFFFF), 50, 1300);
			bg_net.moveForward(199);
			scene.addChild(bg_net);
 
			// Create score textfield
			var tf:TextFormat = new TextFormat("Arial", 32, 0x000000, true);
			scores = new TextField();
			scores.defaultTextFormat = tf;
			scores.autoSize = "center";
			scores.x = stage.stageWidth / 2;
			scores.y = 0;
			scores.text = "0     0";
			addChild(scores);
 
			// Add event listeners
			//stage.focus = this;
			addEventListener(Event.ENTER_FRAME, loop3d);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
		}
 
		private function loop3d( e:Event ):void
		{
			camera.hover(0, ball.x/4000, ball.y/4000);
 
			// Check for keyboard presses
			if ( keypressed == "up" )
			{
				paddle_one.moveUp(paddle_one_vel);
			}
			else if ( keypressed == "down" )
			{
				paddle_one.moveDown(paddle_one_vel);
			}
 
			// Move ball around
			ball.moveRight(ballVelX);
			ball.moveDown(ballVelY);
 
			// Check if scored
			if ( ball.x > 1100 )
			{
				paddle_one_score += 1;
				resetBall();
			}
			else if ( ball.x < -1100 )
			{
				paddle_two_score += 1;
				resetBall();
			}
 
			// Y-Axis perimeter collision test
			if ( ball.y < -700 || ball.y > 700 )
			{
				ballVelY *= -1;
			}
 
 
			// Collision Tests
 
 
			if ( ball.hitTestObject(paddle_one) )
			{	
				ballVelX = Math.abs(ballVelX * ( Math.cos(ball.y / paddle_one.y) + 0.6 ));
				ballVelY = ballVelY * ( Math.sin(ball.y / paddle_one.y) + 0.3 );
 
				// Blur ball
				filter = [new BlurFilter((ballVelX-15)/3,(ballVelY-15)/3)];
				ball.container.filters = filter;
			}
			else if ( ball.hitTestObject(paddle_two) )
			{
				ballVelX = Math.abs(ballVelX * ( Math.cos(ball.y / paddle_two.y) + 0.6 ))*-1;
				ballVelY = ballVelY * ( Math.sin(ball.y / paddle_two.y) + 0.3);
 
				// Blur ball
				filter = [new BlurFilter((ballVelX-15)/3,(ballVelY-15)/3)];
				ball.container.filters = filter;
			}
 
			// Paddle two (aka CPU) AI
			if ( ball.x > 0 )
			{
				if ( paddle_two.y > ball.y )
				{
					paddle_two.moveDown(paddle_two_vel);
				}
				else if ( paddle_two.y < ball.y )
				{
					paddle_two.moveUp(paddle_two_vel);
				}
			}
 
			// Blur ball
 
 
			// Render scene
			scene.renderCamera(camera);
		}
 
		private function keyboardHandler( ke:KeyboardEvent ):void
		{	
			if ( ke.keyCode == 38 )
			{
				keypressed = "up";
				//paddle_one.moveUp(paddle_one_vel);
			}
			else if ( ke.keyCode == 40 )
			{
				keypressed = "down";
				//paddle_one.moveDown(paddle_one_vel);
			}
		}
 
		private function keyUp( ke:KeyboardEvent ):void
		{
			keypressed = "";
		}
 
		private function resetBall():void
		{
			// Reset ball position
			ball.x = 0;
			ball.y = 0;
 
			// Reset ball velocities
			ballVelX = 15;
			ballVelY = 15;
 
			// Remove blur
			ball.container.filters = [];
 
			// Update scores
			scores.text = new String(paddle_one_score + "     " + paddle_two_score);
		}
 
	}
 
}


Leave a Reply