C\#탱크 대전 게임 실현
머리말
그 프로그램 은 주로 원시 탱크 대전 게임 을 간단하게 복원 했다.현재 프로그램 은 자동 으로 적의 탱크 를 생 성 할 수 있 고 적 은 무 작위 로 총알 을 발사 할 수 있 으 며 우리 측 탱크 도 총알 을 발사 하면 서 이동 할 수 있다.유일한 부족 한 점 은 아직 완전한 통관 설정 과 장애 설정 이 없다 는 것 이다.
인터페이스 효과 도
그림 1
부분 코드
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace MyTankWar
{
public partial class FormMain : Form
{
//game state
private GameState _gameState = GameState.Close;
//my tank
private Tank _myTank = new Tank(Side.Me);
//list set of enemy's tank
private List<Tank> _listEnemyTank = new List<Tank>();
//list set of bullets
private List<Bullet> _listBullet = new List<Bullet>();
// import dynamic link library
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetAsyncKeyState(int keyCode);
public FormMain()
{
InitializeComponent();
}
//'begin game' button function
private void BeginToolStripMenuItem_Click(object sender, EventArgs e)
{
_gameState = GameState.Open;
//enable timer
timer1.Enabled = true;
timer2.Enabled = true;
timer3.Enabled = true;
timer4.Enabled = true;
}
//'end game' button function
private void EndToolStripMenuItem_Click(object sender, EventArgs e)
{
_gameState = GameState.Close;
//unenable timer
timer1.Enabled = false;
timer2.Enabled = false;
timer3.Enabled = false;
timer4.Enabled = false;
}
//'pictureBox1'-> Paint function
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//draw my tanks
_myTank.DrawMe(e.Graphics);
//draw enemy's tanks
for (int i = 0; i <= _listEnemyTank.Count - 1; i++)
_listEnemyTank[i].DrawMe(e.Graphics);
//draw each bullet
foreach (Bullet myBullet in _listBullet)
{
myBullet.DrawMe(e.Graphics);
}
}
//'FormMain'-> KeyDown function
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
//if game started
if (_gameState == GameState.Open)
{
//if press 'Space' key, my tank start shooting
if (e.KeyCode == Keys.Space)
{
Bullet myBullet = _myTank.Fire();
_listBullet.Add(myBullet);
}
//refresh interface
pictureBox1.Invalidate();
}
}
//timer1' Tick function, used for generating a enemy's tank
private void timer1_Tick(object sender, EventArgs e)
{
//if game started
if (_gameState == GameState.Open)
{
//generate a enemy's tank
Tank enemyTank = new Tank(Side.Enemy);
//add enenmy's tank
_listEnemyTank.Add(enemyTank);
//refresh pictureBox1
pictureBox1.Invalidate();
}
}
//timer2' Tick function, used for controlling the direction of a enemy's tank
private void timer2_Tick(object sender, EventArgs e)
{
// if game started
if (_gameState == GameState.Open)
{
//define a random object
Random myRand = new Random(DateTime.Now.Second);
for (int i = 0; i <= _listEnemyTank.Count - 1; i++)
{
//generate a random number
int newDirection = myRand.Next(1, 10);
//move the tank
if (newDirection <= 4)
_listEnemyTank[i].Move((Direction)newDirection);
else
_listEnemyTank[i].Move(_listEnemyTank[i]._Direction);
}
//make bullet move
foreach (Bullet Bullet in _listBullet)
Bullet.Move();
//refresh pictureBox1
pictureBox1.Invalidate();
}
}
//timer3' Tick function, used for shooting of a enemy's tank
private void timer3_Tick(object sender, EventArgs e)
{
// if game started
if (_gameState == GameState.Open)
{
//define a random class
Random myRand = new Random(DateTime.Now.Second);
//make bullet move
foreach (Tank enemyTank in _listEnemyTank)
{
//generate a random number whose value is between 1 to 4
int fireFlag = myRand.Next(1, 10);
//if enemy's tank can shoot
if (fireFlag <= 4)
{
Bullet enemyBullet = enemyTank.Fire();
_listBullet.Add(enemyBullet);
}
}
}
}
//timer4' Tick function, used for spying the key pressing state to make my tank move while shooting
private void timer4_Tick(object sender, EventArgs e)
{
if (_gameState == GameState.Open)
{
bool keyDown = (((ushort)GetAsyncKeyState((int)Keys.Down)) & 0xffff) != 0;
if (keyDown == true)
_myTank.Move(Direction.Down);
bool keyUp = (((ushort)GetAsyncKeyState((int)Keys.Up)) & 0xffff) != 0;
if (keyUp == true)
_myTank.Move(Direction.Up);
bool keyLeft = (((ushort)GetAsyncKeyState((int)Keys.Left)) & 0xffff) != 0;
if (keyLeft == true)
_myTank.Move(Direction.Left);
bool keyRight = (((ushort)GetAsyncKeyState((int)Keys.Right)) & 0xffff) != 0;
if (keyRight == true)
_myTank.Move(Direction.Right);
//refresh pictureBox1
pictureBox1.Invalidate();
}
}
}
}
다운로드:전체 파일이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.