FCゲーム再現 スパルタンX への投稿。
コンセプト
java初心者でも簡単に作れそうな物。
画像とか用意しなくてもいいもの。
それでいて見てて面白そうな物。
発覚しているバグ
- [重要]taskが完全に消えない どうでもいい文がループされる メモリが圧迫される可能性がある
- フォントが反映されず、デザインが崩れる。
学んだ事。
System.getProperty("line.separator")こいつはあまり使えない子。
"¥r¥n"を¥rと¥nで2つと判断します。
javaなのにプラットフォーム間で互換性が取れない。→フォント、改行コード
spaX.java Main Class
import java.awt.event.KeyListener;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.JTextArea;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
import TextShape.TextShape;
import TextShape.GameShape;
import TextShape.TitleShape;
public class SpaX extends JFrame implements KeyListener{
protected JTextArea tf;
protected TextShape ts;
protected int key;
protected Printer printer;
public void keyPressed(KeyEvent e) {
key = e.getKeyCode();
keyEv();
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args){
new SpaX();
}
SpaX(){
super("SpaX");
this.setBackground(new Color(0,0,0));
setSize(500,500);
tf = new JTextArea(40,50);
tf.setFont(new Font("Terminal",Font.PLAIN,15));
ts = new TitleShape();
add(tf);
tf.setBackground(new Color(0,0,0));
tf.setForeground(new Color(255,255,255));
tf.enable(false);
Timer printTimer = new Timer();
printer = new Printer();
printer.ta = tf;
printer.ts = ts;
printTimer.schedule(printer,0,100);
show();
addKeyListener(this);
}
protected GameShape gs;
protected void keyEv(){
switch(key){
case 32:
if(stat ==0){
ts = null;
gs = new GameShape();
ts = gs;
printer.ts = ts;
stat =1;
}else{
if(gs.pY <1){
gs.pY +=4;
}
}
break;
case 68:
gs.Player =0;
break;
case 65:
gs.Player =1;
System.out.print(gs.player);
break;
case 10:
System.out.print(gs.Player);
if(gs.Player <10){
gs.Player +=10;
if(gs.Player ==11){
gs.atk('L');
}else{
gs.atk('R');
}
}
break;
}
}
protected int lastImput =0;
protected int stat = 0;
class Printer extends TimerTask{
public JTextArea ta;
public TextShape ts;
public void run(){
ta.setText(ts.getString());
}
}
}
TextShape - SubClass package[TextShape]
package TextShape;
import java.awt.TextArea;
import java.util.TimerTask;
import java.util.Timer;
import TextShape.TitleShape.timeEv;
public class TextShape {
public char[][] strs;
public String string;
public static String delN(String str){
str = str.replace("\n\r", "!");
str = str.replace("\r\n", "!");
str = str.replace("\n", "!");
str = str.replace("\r", "!");
return str;
}
public TextShape(){
init();
}
private void init(){
strs = new char[25][51];
fill(' ');
}
public void overlap(TextShape ts){
int xx =0;
int yy=0;
for(char[] chrs:ts.strs){
for(char c :chrs){
if(c != ' '){
strs[xx][yy] = c;
}
yy++;
xx=0;
}
xx++;
}
}
public void overlap(TextShape ts,int x, int y){
int xx =x;
int yy=y;
for(char[] chrs:ts.strs){
for(char c :chrs){
if(c != ' '){
try{
strs[xx][yy] = c;
}catch(java.lang.ArrayIndexOutOfBoundsException e){
}
}
yy++;
xx=0;
}
xx++;
}
}
public void replace(char c,char d){
int xx =0;
int yy=0;
for(char[] chrs:strs){
for(char ch :chrs){
if(ch == c){
strs[xx][yy] = d;
}
yy++;
xx=0;
}
xx++;
}
}
public void fillLine(char c,int i){
int xx =0;
for(char ch:strs[i]){
strs[i][xx] = c;
xx++;
}
}
public void fillTate(char c,int i){
int yy=0;
for(char[] ch:strs){
strs[yy][i] = c;
yy++;
}
}
public void fill(char c){
int x =0;
int y =0;
for(char[]str:strs){
for(char ch:str){
strs[x][y] = c;
y++;
}
y=0;
x++;
}
}
public void print(String str,int x,int y){
int yy =y;
int xx=x;
//while()
}
public void clear(){
strs = new char[26][50];
}
public String getString(){
String ret = "";
for(char[] str:strs){
for(char str2:str){
ret += str2;
}
ret+=System.getProperty("line.separator");
}
return ret;
}
protected char br ='!';// System.getProperty("line.separator").charAt(0);
protected Timer time;
protected timeEv tsk;
public void loadString(String str){
int yy=0;
int xx=0;
str = TextShape.delN(str);
for( int i = 0; i < str.length(); i++ ){
char ch = str.charAt(i);
if(ch ==br){
yy++;
xx=0;
}else{
strs[yy][xx] = ch;
xx++;
}
}
}
}
spaX.java Main Class
import java.awt.event.KeyListener;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
import javax.swing.JTextArea;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
import TextShape.TextShape;
import TextShape.GameShape;
import TextShape.TitleShape;
public class SpaX extends JFrame implements KeyListener{
protected JTextArea tf;
protected TextShape ts;
protected int key;
protected Printer printer;
public void keyPressed(KeyEvent e) {
key = e.getKeyCode();
keyEv();
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public static void main(String[] args){
new SpaX();
}
SpaX(){
super("SpaX");
this.setBackground(new Color(0,0,0));
setSize(500,500);
tf = new JTextArea(40,50);
tf.setFont(new Font("Terminal",Font.PLAIN,15));
ts = new TitleShape();
add(tf);
tf.setBackground(new Color(0,0,0));
tf.setForeground(new Color(255,255,255));
tf.enable(false);
Timer printTimer = new Timer();
printer = new Printer();
printer.ta = tf;
printer.ts = ts;
printTimer.schedule(printer,0,100);
show();
addKeyListener(this);
}
protected GameShape gs;
protected void keyEv(){
switch(key){
case 32:
if(stat ==0){
ts = null;
gs = new GameShape();
ts = gs;
printer.ts = ts;
stat =1;
}else{
if(gs.pY <1){
gs.pY +=4;
}
}
break;
case 68:
gs.Player =0;
break;
case 65:
gs.Player =1;
System.out.print(gs.player);
break;
case 10:
System.out.print(gs.Player);
if(gs.Player <10){
gs.Player +=10;
if(gs.Player ==11){
gs.atk('L');
}else{
gs.atk('R');
}
}
break;
}
}
protected int lastImput =0;
protected int stat = 0;
class Printer extends TimerTask{
public JTextArea ta;
public TextShape ts;
public void run(){
ta.setText(ts.getString());
}
}
}
GameShape - SubClass package[TextShape]
package TextShape;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import TextShape.TitleShape.timeEv;
import java.util.*;
public class GameShape extends TextShape {
protected String string ;
protected void setStr(){
string=(" 1:"+score+" TOP-999999 2:000000"+System.getProperty("line.separator")+
" PLAYER ]]]]]]]]]] [ ]-[ ]-[ ]-[ ]-[ ] TIME"+System.getProperty("line.separator")+
" ENEMY ]]]]]]]]]] @-2 >-0 "+timer+System.getProperty("line.separator")+
"================================================="+System.getProperty("line.separator")+
"================================================="+System.getProperty("line.separator")+
" { } { } { } { } "+System.getProperty("line.separator")+
" { } { } { } { } "+System.getProperty("line.separator")+
"================================================="+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+System.getProperty("line.separator")+
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+System.getProperty("line.separator")+
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"+System.getProperty("line.separator")+
"| |*| |*| |*| |*| |*| |*| |*"+System.getProperty("line.separator")+
"================================================="+System.getProperty("line.separator"));
}
protected int score = 100;
public GameShape(){
strs = new char[25][51];
setStr();
string =TextShape.delN(string);
//loadString(string);
playerString[0] = playerR;
playerString[1] = playerL;
playerString[11] = playerLA;
playerString[10] = playerRA;
player =TextShape.delN(player);
paintPlayer();
time = new Timer();
tsk = new timeEv();
time.schedule(tsk, 0,100);
}
protected Timer time;
protected timeEv tsk;
public int Player;
public String player =
(" ~~"+System.getProperty("line.separator")+
" [==]"+System.getProperty("line.separator")+
" { | "+System.getProperty("line.separator")+
" U U"+System.getProperty("line.separator"));
public String playerL =
(" ~~"+System.getProperty("line.separator")+
" [==]"+System.getProperty("line.separator")+
" { | "+System.getProperty("line.separator")+
" U U"+System.getProperty("line.separator"));
public String playerR =
(" ~~"+System.getProperty("line.separator")+
" [==]"+System.getProperty("line.separator")+
" | } "+System.getProperty("line.separator")+
" U U"+System.getProperty("line.separator"));
public String playerLA =
(" ~~"+System.getProperty("line.separator")+
" [==]"+System.getProperty("line.separator")+
"aa={ | "+System.getProperty("line.separator")+
" U U"+System.getProperty("line.separator"));
public String playerRA =
(" ~~"+System.getProperty("line.separator")+
" [==]"+System.getProperty("line.separator")+
" | }=bb"+System.getProperty("line.separator")+
" U U"+System.getProperty("line.separator"));
public int pY;
public char playerV ='R';
protected String[] playerString = new String[30];
private void paintPlayer(){
loadString(string);
System.out.print("a");
player = playerString[Player];
player =TextShape.delN(player);
if(Player >=10){
Player -=10;
}
int xx =18;
int yy=(pY*-1) +14;
if(pY >=1){
pY-=1;
}
for( int i = 0; i < player.length(); i++ ){
player = TextShape.delN(player);
char ch = player.charAt(i);
if(ch ==br){
yy++;
xx=18;
}else{
strs[yy][xx] = ch;
xx++;
}
}
}
protected List<Enemy> enemy = new ArrayList<Enemy>();
protected int c=0;
protected String gameover ;
protected void go(){
gameover ="================================================="+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
"================================================="+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" You lose!"+System.getProperty("line.separator")+
" your score"+(score+timer)+" "+System.getProperty("line.separator")+
" but. this game dose not have goal!"+System.getProperty("line.separator")+
" Waiting for your high score."+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" see you again!"+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" Thanks for Playing!"+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
"================================================="+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
"================================================="+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator");
}
protected void enemyMove(){
if(0==c%3){
for(Enemy en:enemy){
if(en.to == 'L'){
en.x--;
}else{
en.x++;
}
if(en.x <21 && en.x >16){
this.time.cancel();
go();
gameover =TextShape.delN(gameover);
this.fill(' ');
//Thread.sleep(20);
loadString(gameover);
}
}
c++;
}else{
c =0;
}
}
public void atk(char c){
score -=5;
for(Enemy en :enemy ){
if(en.to != c){
if(c =='L'){
if(en.x >=14){
enemy.remove(en);
score+=100;
}
}else{
if(en.x <= 25){
enemy.remove(en);
score+=100;
}
}
}
}
}
protected int popEnemyRate =15;
protected void popEnemy(){
Random rand = new Random();
if(timer >1000){
popEnemyRate =10;
}
if(timer >2000){
popEnemyRate =5;
}
if(1==rand.nextInt()%popEnemyRate){
System.out.print("hi");
Enemy enem = new Enemy();
if(rand.nextBoolean()){
enem.to = 'L';
enem.x =40;
enem.str = enem.L;
System.out.print("L");
enemy.add(enem);
}else{
System.out.print("R");
enem.to ='R';
enem.str = enem.R;
enemy.add(enem);
}
}
}
protected void printEnemy(){
for(Enemy en:enemy){
int xxx =en.x;
int xx=xxx;
int yy=15;
if(pY >=1){
pY-=1;
}
for( int i = 0; i < en.str.length(); i++ ){
en.str = TextShape.delN(en.str);
char ch = en.str.charAt(i);
if(ch ==br){
yy++;
xx=xxx;
}else{
strs[yy][xx] = ch;
xx++;
}
}
}
}
protected int timer = 0;
class timeEv extends TimerTask{
public void run(){
paintPlayer();
setStr();
popEnemy();
enemyMove();
printEnemy();
timer +=3;
}
}
}
TitleShape - SubClass package[TextShape]
package TextShape;
import java.util.Timer;
import java.util.TimerTask;
public class TitleShape extends TextShape{
protected String string =" "+System.getProperty("line.separator")+
"================================================="+System.getProperty("line.separator")+
"******************************** #### ** #####***"+System.getProperty("line.separator")+
"******************************** ## ## "+System.getProperty("line.separator")+
" #### ##### ## ## ##"+System.getProperty("line.separator")+
" # # # # # #####"+System.getProperty("line.separator")+
" #### ##### #### ## ##"+System.getProperty("line.separator")+
" # # # # ## ##"+System.getProperty("line.separator")+
" #### # # # #### #####"+System.getProperty("line.separator")+
"*************************************************"+System.getProperty("line.separator")+
"================================================="+System.getProperty("line.separator")+
" 1 PLAYER GAME A "+System.getProperty("line.separator")+
" 1 PLAYER GAME B "+System.getProperty("line.separator")+
" 2 PLAYER GAME A "+System.getProperty("line.separator")+
" 2 PLAYER GAME B "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" PUSH SPACE "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" "+System.getProperty("line.separator")+
" Remake By 3up"+System.getProperty("line.separator")+
" Javanige Contest"+System.getProperty("line.separator");
protected Timer time;
protected timeEv tsk;
public void stopTimeEvt(){
time.cancel();
tsk =null;
f = false;
}
public TitleShape(){
string = TextShape.delN(string);
loadString(string);
time = new Timer();
tsk =new timeEv();
time.schedule(tsk,0,1000);
}
protected boolean f= true;
class timeEv extends TimerTask{
protected boolean b;
public void run(){
System.out.print(b);
if(b){
loadString(string);
b = false;
}else{
b = true;
fillLine(' ',16);
}
}
}
}
Enemy - SubClass package[TextShape]package TextShape;
public class Enemy {
public static String L =
" @@"+System.getProperty("line.separator")+
"a={ }"+System.getProperty("line.separator")+
" UU"+System.getProperty("line.separator");
public static String R =
" @@"+System.getProperty("line.separator")+
" { }=b"+System.getProperty("line.separator")+
" UU"+System.getProperty("line.separator");
public Enemy(){
}
public String str;
public char to;
public int x;
}
最終更新:2012年02月25日 04:43