Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-24-2006, 01:43 AM   #1
Joseph
ActionScripter
 
Joseph's Avatar
 
Join Date: Mar 2006
Posts: 3
Default [AS2] coding for basic pong game

Here is the coding for a basic pong game.

Just add the following code to frame1 of a new Flash document.

That is acutally all you have to do. ^_^


Code:

Code:
//import classes
import flash.display.*;
import flash.geom.*;
//screen settings
var screen:BitmapData = new BitmapData(Stage.width, Stage.height, false, 0);
var centreLine:Rectangle = new Rectangle(Stage.width / 2 - 1, 0, 2, Stage.height);
this.attachBitmap(screen, 1);
//ball settings
var ballRad:Number = 2;
var ball:Rectangle = new Rectangle(0, 0, ballRad * 2, ballRad * 2);
var velocity:Object = new Object();
reset();
//paddle settings
var paddleWidth:Number = 4;
var paddleHeight:Number = 50;
var playerPaddle:Rectangle = new Rectangle(Stage.width - 10 - paddleWidth / 2, Stage.height / 2 - paddleHeight / 2, paddleWidth, paddleHeight);
var computerPaddle:Rectangle = new Rectangle(0 + 10 - paddleWidth / 2, Stage.height / 2 - paddleHeight / 2, paddleWidth, paddleHeight);
//score settings
var playerScore:Number = 0;
var computerScore:Number = 0;
var playerScoreText:TextField = this.createTextField("playerScoreText", 2, Stage.width / 2 + 50, 20, 0, 0);
var computerScoreText:TextField = this.createTextField("computerScoreText", 3, Stage.width / 2 - 60, 20, 0, 0);
playerScoreText.autoSize = computerScoreText.autoSize = true;
playerScoreText.selectable = computerScoreText.selectable = false;
playerScoreText.textColor = computerScoreText.textColor = 0xFFFFFF;
playerScoreText.variable = "playerScore";
computerScoreText.variable = "computerScore";
onEnterFrame = function ():Void {
	//reset screen
	screen.fillRect(screen.rectangle, 0);
	screen.fillRect(centreLine, 0xFFFFFF);
	//player paddle movement
	if (Key.isDown(Key.UP)) {
		playerPaddle.y -= 5;
	}
	if (Key.isDown(Key.DOWN)) {
		playerPaddle.y += 5;
	}
	screen.fillRect(playerPaddle, 0xFFFFFF);
	//computer paddle movement
	if (velocity.x < 0) {
		if (ball.y < computerPaddle.y + paddleHeight / 2) {
			computerPaddle.y -= Math.abs(velocity.y) - 0.5;
		} else {
			computerPaddle.y += Math.abs(velocity.y) - 0.5;
		}
	}
	screen.fillRect(computerPaddle, 0xFFFFFF);
	//ball movement  
	ball.x += velocity.x;
	ball.y += velocity.y;
	if (ball.y > Stage.height - ballRad) {
		ball.y = Stage.height - ballRad;
		velocity.y *= -1.0;
	} else if (ball.y < 0 + ballRad) {
		ball.y = 0 + ballRad;
		velocity.y *= -1.0;
	}
	if (ball.x > Stage.width - ballRad) {
		reset();
		computerScore++;
	} else if (ball.x < 0) {
		reset();
		playerScore++;
	}
	if (playerPaddle.x - ball.x < Math.abs(velocity.x)) {
		if (ball.y > playerPaddle.y && ball.y < playerPaddle.y + paddleHeight) {
			ball.x = playerPaddle.x - ballRad - paddleWidth / 2;
			velocity.x *= -1.1;
		}
	} else if (ball.x - computerPaddle.x < Math.abs(velocity.x)) {
		if (ball.y > computerPaddle.y && ball.y < computerPaddle.y + paddleHeight) {
			ball.x = computerPaddle.x + paddleWidth / 2;
			velocity.x *= -1.1;
		}
	}
	screen.fillRect(ball, 0xFFFFFF);
};
function reset():Void {
	ball.x = Stage.width / 2 - ballRad;
	ball.y = Stage.height / 2 - ballRad;
	velocity.x = randNum(6, 3);
	velocity.y = randNum(6, 3);
	computerPaddle.y = playerPaddle.y = Stage.height / 2 - paddleHeight / 2;
}
function randNum(max:Number, min:Number):Number {
	return (Math.floor(min + Math.random() * (max - min)));
}
Joseph is offline   Reply With Quote
Old 03-24-2006, 02:21 AM   #2
triscuit
Registered User
 
Join Date: Mar 2006
Posts: 13
Default Cool

Cool, do you mind if I use this and just edit it to my own style?
triscuit is offline   Reply With Quote
Old 03-24-2006, 03:39 AM   #3
Joseph
ActionScripter
 
Joseph's Avatar
 
Join Date: Mar 2006
Posts: 3
Default

Quote:
Originally Posted by triscuit
Cool, do you mind if I use this and just edit it to my own style?
No, not at all. ^_^
Joseph is offline   Reply With Quote
Old 03-24-2006, 04:15 AM   #4
oldnewbie
Banned by AS.org Staff
 
Join Date: Mar 2002
Posts: 10,533
Default

The player's paddle shouldn't move off stage...
oldnewbie is offline   Reply With Quote
Old 01-19-2009, 02:10 AM   #5
Le Douche
Registered User
 
Join Date: Jan 2009
Posts: 1
Default Background

How would I go about eliminating the black background on this movie mini game? so there is no background at all?.....Ive looked through the code and I tried manipulating the color, but the background has no value for it, besides "false", can anyone help?
Le Douche is offline   Reply With Quote
Old 02-08-2012, 11:52 AM   #6
luisgjoni
Registered User
 
Join Date: Feb 2012
Posts: 1
Talking

Thankyou soo much. you have helped me finnish of my a level work by adding this to make a tennis version of ponk
luisgjoni is offline   Reply With Quote
Old 02-08-2012, 12:14 PM   #7
arkitx
Senior Member
 
Join Date: Jan 2011
Posts: 703
Default

Good Coding Joseph

@Le Douche: screen.fillRect(screen.rectangle, 0x00FF00);



arkitx
arkitx is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[AS2] flash game and audio un sync ultraman Gaming and Game Development 0 08-05-2008 06:35 PM
My super ball game (pong) Help Zettersten Gaming and Game Development 0 06-16-2008 06:03 PM
FlashGameLicense.com - virtual marketplace to connect game developers and buyers AdamSchroeder General Chat 0 12-18-2007 09:16 PM
[AS2] Tile game or not tile game? krolben Gaming and Game Development 4 07-28-2004 12:41 PM
[AS2] Locating bug in game MiniMurderdoll Gaming and Game Development 0 06-01-2004 10:27 AM


All times are GMT. The time now is 02:32 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.