保持更长时间会跳得更高(C# winforms)
Posted
技术标签:
【中文标题】保持更长时间会跳得更高(C# winforms)【英文标题】:hold longer will jump higher (C# winforms) 【发布时间】:2022-01-10 13:30:14 【问题描述】:我正在尝试在 winforms 中制作 2D 游戏。我希望我的角色能够进行二段跳,如果我长时间按住空格键(跳跃)我会跳得更高,或者如果我按住空格键(跳跃)少我会跳得更低(请注意,虽然按住更长的时间会跳得更高,但只能达到固定水平,而不是无限)。但我只能二段跳,只能跳1个固定距离,不能保持空间更长跳得更高或保持空间更短跳得更低,有人帮助我,下面是我的代码。
public partial class GamePlay_Page : Form
bool goRight, goLeft;
int gravity = 16;
int force;
bool jump;
int jumpTimes = 2;
public GamePlay_Page()
InitializeComponent();
private void GamePlay_Page_KeyDown(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.D)
goRight = true;
Trex.Image = Properties.Resources.running;
if (e.KeyCode == Keys.A)
goLeft = true;
Trex.Image = Properties.Resources.running2;
if (e.KeyCode == Keys.W && jumpTimes > 0)
jump = true;
force = gravity;
jumpTimes -= 1;
private void gameT(object sender, EventArgs e)
if (goRight == true && Trex.Right < 600)
Trex.Left += 5;
if (goLeft == true && Trex.Left > 10)
Trex.Left -= 5;
if (jump == true)
Trex.Top -= force;
force -= 1;
if (Trex.Top + Trex.Height >= backgroundAbove.Height)
Trex.Top = backgroundAbove.Height - Trex.Height;
else
Trex.Top += 3;
if (Trex.Top + Trex.Height == backgroundAbove.Height)
jumpTimes = 2;
private void GamePlay_Page_KeyUp(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.D) goRight = false;
if (e.KeyCode == Keys.A) goLeft = false;
【问题讨论】:
欢迎来到 ***。我已经做出了回答。如果您不介意,您可以点击“✔”将我的回复标记为已接受的答案。它还将帮助其他人解决类似的问题。 【参考方案1】:我不确定这是否是您的意思,并根据您的需要进行更改。
请看我的例子:
加一个stopwatch
来判断按压时间的长短,防止力度爆表。
我设置的是按3s以上给定力,否则给重力初始力。
根据你的需要改变它。
你完全可以把它改成乘以时间的力。
Stopwatch watch =new Stopwatch();
watch.Start();//start the timer
watch.Stop(); //stop the timer
watch.ElapsedMilliseconds// Show duration in milliseconds(Type is long)
watch.Reset();//reset the timer
将计时开始放入KeyDown
事件:
if (e.KeyCode == Keys.W && jumpTimes > 0)
//jump = true;
//force = gravity;
//jumpTimes -= 1;
watch.Start();
把计时结束并触发跳转到KeyUp
事件:
if (e.KeyCode == Keys.W && jumpTimes > 0)
watch.Stop();
jump = true;
jumpTimes -= 1;
if (watch.ElapsedMilliseconds > 300)//>3s
force = 30;
else
force = gravity;
watch.Reset();
所有代码:
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace GamePlay_Page
public partial class GamePlay_Page : Form
public GamePlay_Page ()
InitializeComponent();
bool goRight, goLeft;
int gravity = 16;
int force;
bool jump;
int jumpTimes = 2;
Stopwatch watch =new Stopwatch();
private void GamePlay_Page_Load (object sender, EventArgs e)
private void GamePlay_Page_KeyDown (object sender, KeyEventArgs e)
if (e.KeyCode == Keys.D)
goRight = true;
Trex.Image = Properties.Resources.ch;
if (e.KeyCode == Keys.A)
goLeft = true;
Trex.Image = Properties.Resources.ca;
if (e.KeyCode == Keys.W && jumpTimes > 0)
//jump = true;
//force = gravity;
//jumpTimes -= 1;
watch.Start();
private void GamePlay_Page_KeyUp (object sender, KeyEventArgs e)
if (e.KeyCode == Keys.W && jumpTimes > 0)
watch.Stop();
if (e.KeyCode == Keys.W && jumpTimes > 0)
watch.Stop();
jump = true;
jumpTimes -= 1;
if (watch.ElapsedMilliseconds > 300)//>3s
force = 30;
else
force = gravity;
watch.Reset();
if (e.KeyCode == Keys.D)
goRight = false;
if (e.KeyCode == Keys.A)
goLeft = false;
private void Timer1_Tick (object sender, EventArgs e)
if (goRight == true && Trex.Right < 600)
Trex.Left += 5;
if (goLeft == true && Trex.Left > 10)
Trex.Left -= 5;
if (jump == true)
Trex.Top -= force;
force -= 1;
if (Trex.Top + Trex.Height >= backgroundAbove.Height)
Trex.Top = backgroundAbove.Height - Trex.Height;
else
Trex.Top += 3;
if (Trex.Top + Trex.Height == backgroundAbove.Height)
jumpTimes = 2;
G.Text = gravity.ToString();
F.Text = force.ToString();
T.Text = Trex.Top.ToString();
L.Text = Trex.Left.ToString();
J.Text = jumpTimes.ToString();
W.Text = watch.ElapsedMilliseconds.ToString();
主页:
输出:
如果您对我的代码有任何疑问,请在下方评论,我会尽快跟进。
【讨论】:
以上是关于保持更长时间会跳得更高(C# winforms)的主要内容,如果未能解决你的问题,请参考以下文章
Unreal UMG按钮的按住,长时间会自动调用Release释放按钮。
Unreal UMG按钮的按住,长时间会自动调用Release释放按钮。
Unreal UMG按钮的按住,长时间会自动调用Release释放按钮。