티스토리 뷰
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
}
}
}
}
'프로그래밍 > C#' 카테고리의 다른 글
C# 마우스 드래그해서 여러개 콘트롤 선택하기 (3) | 2018.12.18 |
---|---|
C# Tutorial - Drag & Drop controls (0) | 2018.12.18 |
C# 윈폼에서 WPF 폼 만드는 방법 (0) | 2018.12.06 |
C# 다름폼에서 폼 제어하는 방법 (1) | 2018.12.06 |
C#으로 사물감지 프로그램 만들기 (2) | 2018.12.06 |