📄 playertanksprite.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
public class PlayerTankSprite : SpriteDrawing
{
int formWidth, formHeight;
int myTankWidth, myTankHeight;
int direction;
int myTankX, myTankY;
Image[] myTankImg;
Form _tankGame2008;
static int speed = 10;
public PlayerTankSprite(Image[] myTankImg, int myTankX, int myTankY, Form TankGame2008)
: base(myTankImg, myTankX, myTankY, TankGame2008)
{
this.myTankX = myTankX;
this.myTankY = myTankY;
this.myTankImg = myTankImg;
myTankWidth = 50;
myTankHeight = 50;
formWidth = 640;
formHeight = 448;
this._tankGame2008 = TankGame2008;
setVisible(true);
setActive(false);
}
public override void updatePos(int SpriteDirection)
{
base.updatePos(SpriteDirection);
switch (direction)
{
case 0:
myTankX = this.getX() - speed;
if (myTankX < 0)
{
myTankX = 0;
}
break;
case 1:
myTankX = this.getX() + speed;
if (myTankX > formWidth - myTankWidth)
{
myTankX = formWidth - myTankWidth;
}
break;
case 2:
myTankY = this.getY() - speed;
if (myTankY < 0)
{
myTankY = 0;
}
break;
case 3:
myTankY = this.getY() + speed;
if (myTankY > formHeight - myTankHeight)
{
myTankY = formHeight - myTankHeight;
}
break;
}
this.setPos(myTankX, myTankY);
}
public int getTankDirection()
{
return direction;
}
public void keyDown(int press)
{
//在TankGame2008中的KeyDown事件中调用此方法,直接传按下的键的KeyValue给此方法.
int pressed = press;
if (pressed == 32)
{
ShellSprite s = ((TankGame2008.TankGame2008)this._tankGame2008).myShell;
s.setShellDirection(this.getTankDirection());
if (s.getVisible() == false)
{
//setActive setVisible放到这,玩家一个子弹没了才能发另一个子弹
s.setActive(true);
s.setVisible(true);
if (this.getTankDirection() == 0) //left
{
s.setPos(this.getX(), this.getY() + this.getHeight() / 2);
}
if (this.getTankDirection() == 1) //right
{
s.setPos(this.getX() + this.getWidth(), this.getY() + this.getHeight() / 2);
}
if (this.getTankDirection() == 2)
{
s.setPos(this.getX() + this.getWidth() / 2, this.getY());
}
if (this.getTankDirection() == 3)
{
s.setPos(this.getX() + this.getWidth() / 2, this.getY() + this.getHeight());
}
}
}
if (pressed == 37)
{
this.direction = 0;
this.setActive(true);
}
if (pressed == 38)
{
this.direction = 2;
this.setActive(true);
}
if (pressed == 39)
{
this.direction = 1;
this.setActive(true);
}
if(pressed == 40)
{
this.direction = 3;
this.setActive(true);
}
}
public void keyReleased()
{
this.setActive(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -