用C#创建Pong-从墙和球拍弹起球
大家好,
作为有抱负的游戏开发人员,我决定使用C#语言在Visual Studio中重新创建经典的虚拟网球游戏Pong.
As an aspiring game developer, I have decided to recreate the classic virtual tennis game Pong in Visual Studio using the C# language.
问题是,除了VB.NET语言之外,我几乎没有编程经验,更不用说比基于文本的程序更复杂的东西了,所以我发现这项任务很难理解.
The problem with this is that I have very little programming experience outside of the VB.NET language, let alone anything more complicated than text-based programs so I'm finding this task understandably difficult.
话虽如此,我已经设法创建了一个使用鼠标控制的球拍,并为球创建了碰撞.那是我感到困惑的地方,因为当球与球拍接触时,我尝试过的所有动作似乎都不会产生反弹效果, 从而使游戏几乎无法玩.有人对我如何根据当前代码解决此问题有任何建议吗?
With that said, I have managed to create a paddle controlled using the mouse as well as created the collision for the ball. That's where I got confused, as nothing I have tried seems to create a bouncing effect when the ball comes in contact with the paddle, thus making the game virtually unplayable. Does anyone have any suggestions on to how I would tackle this issue based on my current code?
注意:我知道能够使用箭头键移动球似乎是不必要的,但这是我想到测试桨板碰撞的唯一方法.
NOTE: I know being able to move the ball using the arrow keys seems unnecessary but it's the only way I can think of testing the paddle collision.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Pong_v0._1
{
public partial class fm_main : Form
{
public fm_main()
{
InitializeComponent();
MouseMove += new MouseEventHandler(p_control);
KeyDown += new KeyEventHandler(b_control);
}
void p_control(object sender, MouseEventArgs e)
{
pb_p_player.Top = e.Y ;
this.Refresh();
}
void b_control(object sender, KeyEventArgs e)
{
int x = pb_ball.Location.X;
int y = pb_ball.Location.Y;
if (e.KeyCode == Keys.Right) x += 10;
else if (e.KeyCode == Keys.Left) x -= 10;
else if (e.KeyCode == Keys.Up) y -= 10;
else if (e.KeyCode == Keys.Down) y += 10;
pb_ball.Location = new Point(x, y);
if (pb_ball.Bounds.IntersectsWith(pb_p_player.Bounds))
{
x += 10;
pb_ball.Location = new Point(x, y);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
刺客,
谢谢您在这里发布.
对于您的问题,请尝试以下代码.
For your question, please try the following code.
namespace ping_pong_game
{
public partial class Form1 : Form
{
public int speed_left = 4; //speed of ball
public int speed_top = 4;
public int point = 0; //score point
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
Cursor.Hide(); //hide the cursor
this.FormBorderStyle = FormBorderStyle.None; //remove any boder
this.TopMost = true; //bring the form to the front
this.Bounds = Screen.PrimaryScreen.Bounds; //make it fullscreen
racket.Top = playground.Bottom - (playground.Bottom / 10); //set the position of racket
}
private void timer1_Tick(object sender, EventArgs e)
{
racket.Left = Cursor.Position.X - (racket.Width / 2); //set the center of the racket to the position of the cursor
ball.Left += speed_left; //move the ball
ball.Top += speed_top;
if (ball.Bottom >= racket.Top && ball.Bottom <= racket.Bottom && ball.Left >= racket.Left && ball.Right <= racket.Right) //racket collision
{
speed_top += 2;
speed_left += 2;
speed_top = -speed_top;// change the direction
point += 1;
}
if (ball.Left<=playground.Left)
{
speed_left = -speed_left;
}
if (ball.Right>=playground.Right)
{
speed_left = -speed_left;
}
if (ball.Top<=playground.Top)
{
speed_top = -speed_top;
}
if (ball.Bottom>=playground.Bottom)
{
timer1.Enabled = false; //ball is out ->stop the game
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close(); //press escape to quit
}
}
}
}
我使用git来显示游戏.
I use a git to show the game.
操场和球是pictureBox.拍子就是面板.
playground and ball is pictureBox. racket is panel.
我希望这会有所帮助.
最好的问候,
温迪