Need Help With Pong.


Recommended Posts

I have microsoft visual studios . net 2003 edition for c# and I am wondering really i will post the code here like how to like make a scoreboard out of this really. I got the paddles to work make it bounce off of them but now I need them to like make it like a game. I mean is for it to score if one paddles go over the ither here is the code hopefully someone will help.

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace Keyboard

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.ComponentModel.IContainer components;

// Global Variables

int dx=0, dy=0, newX=0, newY=0,dx2=0,dy2=0,newX2=0,newY2=0, newX3=0,

newY3=0, bdx=0, bdy=0;

private System.Windows.Forms.Timer timer1;

private System.Windows.Forms.Label label2;

private System.Windows.Forms.Timer timer2;

private System.Windows.Forms.Label lblBall;

private System.Windows.Forms.Label Score1;

private System.Windows.Forms.Label Score3;

private System.Windows.Forms.Label Score2;

private System.Windows.Forms.Label Score4;

private System.Windows.Forms.Label label1;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.label1 = new System.Windows.Forms.Label();

this.timer1 = new System.Windows.Forms.Timer(this.components);

this.label2 = new System.Windows.Forms.Label();

this.timer2 = new System.Windows.Forms.Timer(this.components);

this.lblBall = new System.Windows.Forms.Label();

this.Score1 = new System.Windows.Forms.Label();

this.Score3 = new System.Windows.Forms.Label();

this.Score2 = new System.Windows.Forms.Label();

this.Score4 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// label1

//

this.label1.BackColor = System.Drawing.Color.Blue;

this.label1.Location = new System.Drawing.Point(104, 32);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(88, 8);

this.label1.TabIndex = 0;

//

// timer1

//

this.timer1.Enabled = true;

this.timer1.Interval = 1;

this.timer1.Tick += new System.EventHandler(this.timer1_Tick_1);

//

// label2

//

this.label2.BackColor = System.Drawing.Color.Blue;

this.label2.Location = new System.Drawing.Point(104, 224);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(88, 8);

this.label2.TabIndex = 1;

//

// timer2

//

this.timer2.Enabled = true;

this.timer2.Interval = 20;

this.timer2.Tick += new System.EventHandler(this.timer2_Tick);

//

// lblBall

//

this.lblBall.BackColor = System.Drawing.Color.White;

this.lblBall.Location = new System.Drawing.Point(152, 128);

this.lblBall.Name = "lblBall";

this.lblBall.Size = new System.Drawing.Size(8, 8);

this.lblBall.TabIndex = 2;

//

// Score1

//

this.Score1.BackColor = System.Drawing.Color.White;

this.Score1.Font = new System.Drawing.Font("Microsoft Sans Serif",

15.75F, System.Drawing.FontStyle.Regular,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.Score1.Location = new System.Drawing.Point(0, 0);

this.Score1.Name = "Score1";

this.Score1.Size = new System.Drawing.Size(32, 24);

this.Score1.TabIndex = 3;

this.Score1.Text = " 0";

//

// Score3

//

this.Score3.BackColor = System.Drawing.Color.White;

this.Score3.Font = new System.Drawing.Font("Microsoft Sans Serif",

15.75F, System.Drawing.FontStyle.Regular,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.Score3.Location = new System.Drawing.Point(0, 248);

this.Score3.Name = "Score3";

this.Score3.Size = new System.Drawing.Size(24, 24);

this.Score3.TabIndex = 4;

this.Score3.Text = "0";

//

// Score2

//

this.Score2.BackColor = System.Drawing.Color.White;

this.Score2.Font = new System.Drawing.Font("Microsoft Sans Serif",

15.75F, System.Drawing.FontStyle.Regular,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.Score2.Location = new System.Drawing.Point(264, 0);

this.Score2.Name = "Score2";

this.Score2.Size = new System.Drawing.Size(48, 24);

this.Score2.TabIndex = 5;

this.Score2.Text = "0";

//

// Score4

//

this.Score4.BackColor = System.Drawing.Color.White;

this.Score4.Font = new System.Drawing.Font("Microsoft Sans Serif",

15.75F, System.Drawing.FontStyle.Regular,

System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));

this.Score4.Location = new System.Drawing.Point(264, 248);

this.Score4.Name = "Score4";

this.Score4.Size = new System.Drawing.Size(32, 32);

this.Score4.TabIndex = 6;

this.Score4.Text = "0";

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.BackColor = System.Drawing.Color.Black;

this.ClientSize = new System.Drawing.Size(292, 273);

this.Controls.Add(this.Score4);

this.Controls.Add(this.Score2);

this.Controls.Add(this.Score3);

this.Controls.Add(this.Score1);

this.Controls.Add(this.lblBall);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Name = "Form1";

this.Text = "Pong";

this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);

this.KeyPress += new

System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);

this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[sTAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_KeyPress(object sender,

System.Windows.Forms.KeyPressEventArgs e)

{

//label.Text += Convert.ToString(e.KeyChar);

}

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

{

//label.Text = "Down: " + Convert.ToString(e.KeyCode);

switch(e.KeyCode)

{

case Keys.Right:

dx = 2;

dy = 0;

break;

case Keys.Left:

dx = -2;

dy = 0;

break;

}

switch(e.KeyCode)

{

case Keys.D:

dx2 = 2;

dy2 = 0;

break;

case Keys.A:

dx2 = -2;

dy2 = 0;

break;

}

switch(e.KeyCode)

{

case Keys.B:

bdx = 2;

bdy = 5;

break;

}

}

private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)

{

//label1.Text = "Up: " + Convert.ToString(e.KeyCode);

}

private void timer1_Tick_1(object sender, System.EventArgs e)

{

newX=label1.Location.X + dx;

if (newX >this.Width-label1.Width)

dx=-dx;

if (newX <0)

dx=-dx;

newY =label1.Location.Y + dy;

Point newLoc = new Point(newX,newY);

label1.Location = newLoc;

newX2=label2.Location.X + dx2;

newY2=label2.Location.Y + dy2;

if (newX2 >this.Width-label1.Width)

dx2=-dx2;

if (newX2 <0)

dx2=-dx2;

Point newLoc2 = new Point(newX2,newY2);

label2.Location = newLoc2;

}

private void timer2_Tick(object sender, System.EventArgs e)

{

newX3=lblBall.Location.X + bdx;

newY3=lblBall.Location.Y + bdy;

if (newX3 >this.Height-lblBall.Width)

bdx=-bdx;

if (newX3 <0)

bdx=-bdx;

if (newY3 >this.Height-lblBall.Width)

bdy=-bdy;

if (newY3 <0)

bdy=-bdy;

Point newLoc3 = new Point(newX3,newY3);

lblBall.Location = newLoc3;

if(lblBall.Bounds.IntersectsWith(label1.Bounds))

{

bdx=-bdx;

bdy=-bdy;

}

if(lblBall.Bounds.IntersectsWith(label2.Bounds))

{

bdx=-bdx;

bdy=-bdy;

}

}

}

}

copyright me or whatver.

Edit/Delete Message

Link to post
Share on other sites

you need to explain the game more.. does it score when the "ball" intersects with something..

you should have a varable (type int) for holding the score..

in your check for the action that would produce a score add to that varable

something like x++;

then update the board with this variabe

core4.Text = Convert.ToString(x);

Link to post
Share on other sites
Well before that code you told me. What it does now is bounces off the walls all four sides and paddeles as well. I am looking for a scorecborad idk how to do and to make it like when the ball hits the wall the other player gets a point and then you add one to the score.

here is an example of using buttions as a score board.. this took all of 2 min to do ..

I find that its better to use buttons and get the code working 100% before makeing it good looking..

you should notice stuff like this

this.button2.Enabled = false;

this make the button not a button..

if you could find a TTF that is more stencel like it would look like a Scoreboard.

I made the button flat but it would look more mac like if it stayed raised..

I also chaged the color which you can access to make flash when they say.. reach a high score..

with something like

button2.BackColor = System.Drawing.SystemColors.GrayText;

program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace thoughttester
{
static class Program
{


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new testing());
}
}
}

Form1.Designer.cs

namespace thoughttester
{
partial class testing
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button2 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.playerLabel2 = new System.Windows.Forms.Label();
this.playerLabel1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// button2
//
this.button2.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
this.button2.Enabled = false;
this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button2.Location = new System.Drawing.Point(0, 3);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(99, 32);
this.button2.TabIndex = 5;
this.button2.UseVisualStyleBackColor = false;
//
// panel1
//
this.panel1.Controls.Add(this.playerLabel2);
this.panel1.Controls.Add(this.playerLabel1);
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.button1);
this.panel1.Location = new System.Drawing.Point(23, 5);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(246, 56);
this.panel1.TabIndex = 6;
//
// playerLabel2
//
this.playerLabel2.AutoSize = true;
this.playerLabel2.Font = new System.Drawing.Font("Palatino Linotype", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.playerLabel2.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.playerLabel2.Location = new System.Drawing.Point(20, 38);
this.playerLabel2.Name = "playerLabel2";
this.playerLabel2.Size = new System.Drawing.Size(66, 16);
this.playerLabel2.TabIndex = 9;
this.playerLabel2.Text = "Player Two";
//
// playerLabel1
//
this.playerLabel1.AutoSize = true;
this.playerLabel1.Font = new System.Drawing.Font("Palatino Linotype", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.playerLabel1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.playerLabel1.Location = new System.Drawing.Point(166, 38);
this.playerLabel1.Name = "playerLabel1";
this.playerLabel1.Size = new System.Drawing.Size(64, 16);
this.playerLabel1.TabIndex = 8;
this.playerLabel1.Text = "Player One";
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
this.button1.Enabled = false;
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Location = new System.Drawing.Point(144, 3);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(99, 32);
this.button1.TabIndex = 7;
this.button1.UseVisualStyleBackColor = false;
//
// button3
//
this.button3.Location = new System.Drawing.Point(169, 174);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(100, 41);
this.button3.TabIndex = 7;
this.button3.Text = "Player One Score";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(23, 174);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(100, 41);
this.button4.TabIndex = 8;
this.button4.Text = "Player Two Score";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// testing
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.panel1);
this.Name = "testing";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button2;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label playerLabel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label playerLabel2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
}
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace thoughttester
{
public partial class testing : Form
{

private double playerOneScore = 0.0;
private double playerTwoScore = 0.0;


public testing()
{
InitializeComponent();
}

private void button3_Click(object sender, EventArgs e)
{
playerOneScore++;
button1.Text = Convert.ToString(playerOneScore);
}

private void button4_Click(object sender, EventArgs e)
{
playerTwoScore++;
button2.Text = Convert.ToString(playerTwoScore);
}





}
}

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...