[2006-05-07]打算编个超傻的AI,只会来回走动的,却发现已经接近24点

听从了James关于无聊的猎杀潜艇游戏的建议打算加入会动的敌人,提高游戏性加入“战争迷雾”可谓小菜一碟而给敌人加上个最弱智的AI也不算太高难利用我擅长的INI文件处理,不难办到不过以后的地图文件会大很多[0]type=1x0=1y0=8x1=2y1=8x2=3y2=8文件后部分会出现很多这样的AI Script读取信息的函数也已经编好,下一步就是在游戏界面中实现了,明天中午GOGOGO

void CMarineDlg::GetItemInfo(int stage)
{
char* path=new char[240];
char file[MAX_PATH],number[3];
GetCurrentDirectory(240,path);
strcpy(file,path);
strcat(file,"\\map\\map");
itoa(stage+1,number,10);
strcat(file,number);
strcat(file,".thi");
char temp[10],temp2[15],temp3[10];
int i,j;
for(i=0;i<256;i++){
itoa(i,temp,10);
m_OnGame.ItemInfo[i].type=GetPrivateProfileInt(temp,"type",0,file);
if(m_OnGame.ItemInfo[i].type==0){
m_OnGame.ItemInfo[i].bIsExisted=FALSE;
break;
}
else{
for(j=0;i<16;j++)
{
strcpy(temp2,"x");
itoa(j,temp3,10);
strcat(temp2,temp3);
m_OnGame.ItemInfo[i].step[j].x =GetPrivateProfileInt(temp,temp2,100,file);
strcpy(temp2,"y");
itoa(j,temp3,10);
strcat(temp2,temp3);
m_OnGame.ItemInfo[i].step[j].y =GetPrivateProfileInt(temp,temp2,100,file);
if(m_OnGame.ItemInfo[i].step[j].x==100 || m_OnGame.ItemInfo[i].step[j].y==100){
m_OnGame.ItemInfo[i].bIsExisted=FALSE;
break;
}
else{
m_OnGame.ItemInfo[i].bIsExisted=TRUE;
m_OnGame.ItemInfo[i].now.x=m_OnGame.ItemInfo[i].step[j].x;
m_OnGame.ItemInfo[i].now.y = m_OnGame.ItemInfo[i].step[j].y;
}
}
}
}
delete path;
return;
}
以下是一个简单的Java机器人待机动画示例,它会来回走动: ```java import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class RobotAnimation extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; private int x = 0; private int y = 100; private int dx = 5; private int dy = 0; private int width = 50; private int height = 50; private int legAngle = 0; private int armAngle = 0; private Timer timer; public RobotAnimation() { timer = new Timer(50, this); timer.start(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(x, y, width, height); g.setColor(Color.RED); g.drawLine(x, y, x + width, y); g.drawLine(x, y + height, x + width, y + height); g.drawLine(x, y, x, y + height); g.drawLine(x + width, y, x + width, y + height); g.drawLine(x, y + height / 2, x - 20, y + height / 2 - 20); g.drawLine(x, y + height / 2, x - 20, y + height / 2 + 20); g.drawLine(x + width, y + height / 2, x + width + 20, y + height / 2 - 20); g.drawLine(x + width, y + height / 2, x + width + 20, y + height / 2 + 20); g.drawLine(x + width / 2, y, x + width / 2, y - 20); g.drawLine(x + width / 2, y, x + width / 2 - 20, y - 20); g.drawLine(x + width / 2, y, x + width / 2 + 20, y - 20); g.drawLine(x + width / 2, y + height, x + width / 2, y + height + 20); g.drawLine(x + width / 2, y + height, x + width / 2 - 20, y + height + 20); g.drawLine(x + width / 2, y + height, x + width / 2 + 20, y + height + 20); g.drawLine(x + width / 2, y + height / 2, x + width / 2 - 20, y + height / 2 + legAngle); g.drawLine(x + width / 2, y + height / 2, x + width / 2 + 20, y + height / 2 - legAngle); g.drawLine(x + width / 2, y + height / 2, x + width / 2 - 20, y + height / 2 + armAngle); g.drawLine(x + width / 2, y + height / 2, x + width / 2 + 20, y + height / 2 - armAngle); } public void actionPerformed(ActionEvent e) { if (x + dx < 0 || x + dx > getWidth() - width) { dx = -dx; } x += dx; legAngle = (legAngle + 5) % 30; armAngle = (armAngle + 10) % 60; repaint(); } public static void main(String[] args) { JFrame frame = new JFrame("Robot Animation"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setLocationRelativeTo(null); frame.add(new RobotAnimation()); frame.setVisible(true); } } ``` 该程序创建了一个继承自JPanel的RobotAnimation类,它包含一个机器人的x和y坐标、宽度和高度、腿和臂的角度等属性。在构造函数中,程序创建了一个定时器,每50毫秒调用一次actionPerformed()方法。在paintComponent()方法中,程序使用Graphics对象绘制机器人的矩形身体、四条腿、两只手臂和两只眼睛。在actionPerformed()方法中,程序更新机器人的x坐标、腿和臂的角度,并调用repaint()方法重新绘制机器人。在main()方法中,程序创建了一个JFrame窗口,并将RobotAnimation对象添加到其中。运行程序后,您将看到一个机器人来回走动的待机动画。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值