티스토리 뷰

프로그래밍/C#

c# 핑퐁게임 소스코드

뽀로로친구에디 2018. 12. 17. 22:28

c# 핑퐁게임 소스코드 




C# 폼화면 꾸미기 

panel DOCK-DOCK NAME-PLAYGROUND


picturebox name-racket size 200,20 backcolor - black


picturebox name-ball size 30,30 backcolor -red 


timer 1 interval = 1 


핑퐁소스코드 

using System;

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 PingPong

{

    public partial class Form1 : Form

    {

        public int speed_left = 4;

        public int speed_top = 4;

        public int point = 0;


        public Form1()

        {

            InitializeComponent();

            timer1.Enabled = true;

            Cursor.Hide();  // Hide the cursor


            this.FormBorderStyle = FormBorderStyle.None;  //Remove any border

            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 Form1_KeyDown(object sender, KeyEventArgs e)

        {

            if(e.KeyCode == Keys.Escape) { this.Close(); }

        }


        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 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

            }

        }

    }

}



댓글
최근에 달린 댓글
글 보관함
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글