C# ジャンプ処理について

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Hello.co.jp

C# ジャンプ処理について

#1

投稿記事 by Hello.co.jp » 8年前

C#でジャンプする動きをするプログラムを書いたんですが、毎回y座標が0になるところまでジャンプしてしまいます。

どうすれば毎回同じジャンプができますか?

コード:

using System;
using System.Diagnostics;
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 jump002
{
    public partial class Form1 : Form
    {
        Stopwatch sw = new Stopwatch();
        PictureBox Player = new PictureBox();
        int x, y;
        double time,time2,timer3,y_jump;
        Decimal Round;
        Timer timer1 = new Timer();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Set();
            TimerSet();
            PlayerSet();
        }

        void Set()
        {
            this.Width = 900;
            this.Height = 600;
            time = 0.70;
        }

        void TimerSet()
        {
            timer1.Enabled = true;
            timer1.Interval = 1;
            timer1.Tick += new EventHandler(Jump);
        }

        void PlayerSet()
        {
            x = 45;
            y = 450;
            Player.BackColor = Color.Red;
            Player.Height = 64;
            Player.Width = 64;
            Player.Location = new Point(x, y);
            Controls.Add(Player);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            sw.Restart();
        }

        void Jump(object sender,EventArgs e)
        {
            time2 = (double)sw.ElapsedTicks
                           / (double)Stopwatch.Frequency;
            Round = (Decimal)Math.Round(time2, 2, MidpointRounding.AwayFromZero);
            timer3 = time - (double)Round;

            y_jump = 980 * (timer3 * timer3);
            y = (int)y_jump;

            x++;

            Player.Location = new Point(x,y);
            Controls.Add(Player);
        }
    }
}

“C言語何でも質問掲示板” へ戻る