2007年2月20日 星期二

吉里吉里TJS特訓99

特訓99是數年前在巴哈姆特kuso版看到的一個遊戲,規則很簡單:「閃避畫面上的99顆子彈,看你可以撐幾秒」自從看到了這個遊戲之後,每到一個可以寫遊戲的環境,我都把他當hello, world來寫了,幫助熟悉整個程式環境。(印像中寫過C++/DirectDraw, J2SE, J2ME on 手機, VisualBasic, Ruby/RPG Maker XP還有什麼忘了)

剛剛把試著用吉里吉里的TJS寫了一下,心得...喵的文件有夠不完整,要到處翻來翻趣,有的地方乾脆去template裡直接看他怎麼寫的比較快。寫出來就這樣囉:


不停的閃子彈閃到死吧 XD
  • 其實code寫的很差,我只是一直翻文件然後找出一個能寫出來的方法而已。很多地方應該可以再修正的。碰撞判定的公式其實也是錯的,可是我懶得改了,可以跑就好了啦,只是做個小範例而已 @@
  • 為了簡化範例,被擊中game over時就直接跳出遊戲了 (好我懶得做game over畫面了 XD)
  • 為了簡化範例所以沒有貼圖,都是用方塊表示。這樣只需要把程式複置貼上就可以跑囉


把以下程式複置貼到startup.tjs (原本的程式清掉) 即可

class MovingObject extends Layer{
var master;
var size;
var color;

function MovingObject(gm, window, parent) {
super.Layer(window, parent);
master = gm;
visible = true;
setSize(size, size);
fillRect(0, 0, size, size, color);
}
}

class Enemy extends MovingObject {
var vx, vy;
function Enemy(gm, window, parent) {
size = 6;
color = 0xffff0000;
super.MovingObject(gm, window, parent);
init();
}
function init() {
var side = Math.floor(Math.random()*4);
switch (side) {
case 0:
top = 0;
left = Math.floor(Math.random()*400);
vx = Math.floor(Math.random()*5)-2;
vy = Math.floor(Math.random()*2)+1;
break;
case 1:
top = 400;
left = Math.floor(Math.random()*400);
vx = Math.floor(Math.random()*5)-2;
vy = -Math.floor(Math.random()*2)-1;
break;
case 2:
left = 0;
top = Math.floor(Math.random()*400);
vy = Math.floor(Math.random()*5)-2;
vx = Math.floor(Math.random()*2)+1;
break;
default:
left = 400;
top = Math.floor(Math.random()*400);
vy = Math.floor(Math.random()*5)-2;
vx = -Math.floor(Math.random()*2)-1;
break;
}
}
function check_bound() {
if (top>-5&&top<405&&top>-5&&top<405)
return;
init();
}
function update() {
top += vy;
left += vx;
check_bound();
}
}

class Player extends MovingObject {
function Player(gm, window, parent) {
size = 10;
color = 0xff0000ff;
super.MovingObject(gm, window, parent);
top = left = 200;
}
function update() {
var speed = 2;
if (System.getKeyState(VK_DOWN))
top += speed;
if (System.getKeyState(VK_UP))
top -= speed;
if (System.getKeyState(VK_LEFT))
left -= speed;
if (System.getKeyState(VK_RIGHT))
left += speed;
}
}

class GameMaster {
var window;
var parent;
var fore;

var player;
var enemies;

var timer1;

function GameMaster(win, par) {
window = win;
parent = par;

fore = new Layer(win, par);

with (fore) {
.visible = true;
.setSize(400, 400);
}

timer1 = new Timer(onTimer1, "");
timer1.interval = 25;
timer1.enabled = true;

player = new Player(this, window, fore);

enemies = [];
for (var i=0; i<99; ++i) {
enemies.add(new Enemy(this, window, fore));
}

}
function onTimer1() {
player.update();
for (var i=0; i<enemies.count; ++i) {
var e = enemies[i];
e.update();
if (Math.abs(e.top-player.top)<10&&Math.abs(e.left-player.left)<10)
window.close();
}
}

}

class GameWindow extends Window {
var master;

function GameWindow() {

super.Window();

add(new Layer(this, null));
primaryLayer.setSize(400, 400);
setInnerSize(400, 400);

caption = "特訓99";
visible = true;

master = new GameMaster(this, primaryLayer);
}
function finalize() {
super.finalize();
}
function action(ev) { }
}
var win = new GameWindow();


解說篇....下次再說吧 XD

沒有留言: