import java.awt.*;
import java.applet.Applet;
public class Blocks extends Applet implements Runnable
{
Dimension d;
Font largefont= new Font("Helvetica", Font.BOLD, 28);
Font smallfont= new Font("Helvetica", Font.BOLD, 10);
Color textcolor1=new Color(96,128,255);
Color textcolor2=new Color(255,160,64);
FontMetrics fmsmall, fmlarge;
Graphics goff;
Image ii;
Threadthethread;
boolean ingame=false;
final short xblocks=10;
final short yblocks=20;
final int blocksize=16;
final int width=xblocks*blocksize;
final int height=yblocks*blocksize;
short[][] screendata;
final short maxcolors=6;
Color[] blocks;
final int barwidth=8;
final Color barcolor=new Color(128,255,64);
final Color background=new Color(0,0,0);
int score;
short emptyline;
int objectx, objecty;
int objectx;
short objecttype;
short objectcolor;
int objectrotation;
int objectrotationd=0;
short objectptr;
short checkptr;
final short itemcount=7;
final short itemrotlen=8;
final short itemlen=itemrotlen*4;
short count;
final short maxcount=5;
short curcount;
boolean fast=false;
final short screendelay=40;
short screencount=40;
boolean showtitle=true;
int items[]={
0,0, -1,0, 0,-1, -1,-1, //四方形,正常状态
0,0, -1,0, 0,1, -1,1, //被旋转90度
0,0, 1,0, 0,1, 1,1,
0,0, 1,0, 0,-1, 1,-1,
0,0, 0,-1, 0,-2, 0,-3,
0,0, -1,0, -2,0, -3,0,
0,0, 0,1, 0,2, 0,3,
0,0, 1,0, 2,0, 3,0,
0,0, 1,0, 0,-1, -1,-1,
0,0, 0,-1, -1,0, -1,1,
0,0, -1,0, 0,1, 1,1,
0,0, 0,1, 1,0, 1,-1,
0,0, -1,0, 0,-1, 1,-1,
0,0, 0,1, -1,0, -1,-1,
0,0, 1,0, 0,1, -1,1,
0,0, 0,-1, 1,0, 1,1,
0,0, 1,0, -1,0, 0,-1,
0,0, 0,1, 0,-1, -1,0,
0,0, 0,1, -1,0, 1,0,
0,0, 1,0, 0,-1, 0,1,
0,0, 0,-1, 1,-1, 0,1,
0,0, -1,0, -1,-1, 1,0,
0,0, -1,1, 0,1, 0,-1,
0,0, -1,0, 1,0, 1,1,
0,0, 0,1, 0,-1, -1,-1,
0,0, 1,0, -1,0, -1,1,
0,0, 0,-1, 0,1, 1,1,
0,0, -1,0, 1,0, 1,-1,
};
int checks[]={
-1,1, 0,1, -1,1, 0,1,
-1,2, 0,2, -1,2, 0,2,
0,2, 1,2, 0,2, 1,2,
0,1, 1,1, 0,1, 1,1,
0,1, 0,1, 0,1, 0,1,
0,1, -1,1, -2,1, -3,1,
0,4, 0,4, 0,4, 0,4,
0,1, 1,1, 2,1, 3,1,
0,1, -1,0, 1,1, 0,1,
0,1, -1,2, 0,1, -1,2,
0,2, 1,2, -1,2, 0,2,
0,2, 1,1, 0,2, 1,1,
-1,1, 0,1, 1,0, 1,0,
-1,1, 0,2, 0,2, -1,1,
-1,2, 0,2, 1,1, 1,1,
0,1, 1,2, 0,1, 1,2,
-1,1, 0,1, 1,1, 1,1,
-1,1, 0,2, 0,2, -1,1,
-1,1, 0,2, 1,1, 1,1,
0,2, 1,1, 0,2, 1,1,
0,2, 1,0, 1,0, 0,2,
-1,1, 0,1, 1,1, 1,1,
-1,2, 0,2, 0,2, -1,2,
-1,1, 0,1, 1,2, 1,2,
-1,0, 0,2, 0,2, -1,0,
-1,2, 0,1, 1,1, 1,1,
0,2, 1,2, 1,2, 0,2,
-1,1, 0,1, 1,1, 1,1,
};
public string getAppletInfo()
{
return("Blocks - by Ali");
}
//初始化applet
public void init()
{
short i;
screendata=new short[xblocks][yblocks];
blocks=new Color[maxcolors+1];
//设置背景色
blocks[0]=background;
//设置方块颜色
blocks[1]=new Color(255,0,0);
blocks[2]=new Color(0,255,0);
blocks[3]=new Color(0,0,255);
blocks[4]=new Color(255,255,0);
blocks[5]=new Color(255,0,255);
blocks[6]=new Color(0,255,255);
Graphics g;
resize(width+2*barwidth,height+30);
d=size();
setBackground(background);
g=getGraphics();
g.setFont(smallfont);
fmsmall=g.getFontMetrics();
g.setFont(largefont);
fmlarge=g.getFontMetrics();
gameInit();
}
//初始化游戏
public boid gameInit()
{
short i,j;
for(i=0;i<xblocks;i++)
{
for(j=0;j<yblocks;j++)
{
screendata[i][j]=0;
}
}
score=0;
emptyline=-1;
newObject();
fast=false;
curcount=maxcount;
}
public void newObject()
{
short i;
int y;
objectx=xblocks/2-1;
objectdx=0;
objecty=0;
objecttype=(short)(Math.random()*itemcount);
if (objecttype>=itemcount)
objecttype=itemcount-1;
objectptr=(short)(objecttype*itemlen);
checkptr=(short)(Mat.random()*maxcolors+1);
if (objectcolor>maxcolors)
objectcolor=maxcolors;
objectrotation=0;
count=maxcount;
//判断游戏是否结束
for(i=0;i<4;i++)
{
y=items[objectptr+i*2+1];
if(y>=0&&screendata[objectx+items[objectptr+i*2]][y]!=0)
{
ingame=false;
showtitle=true;
}
}
}
//判断键盘是否被按下,并且处理事件
public boolean keyDown(Event e, int key)
{
if(ingame)
{
if(Key==Event.LEFT)
{
objectdx=-1;
}
else if (Key==Event.RIGHT)
{
objectdx=1;
}
else if (Key==Event.UP)
{
objectrotationd=1;
}
else if (Key==Event.DOWN)
{
fast=true;
}
else if (Key==Event.ESCAPE)
{
ingame=false;
}
}
else
{
if(Key==´s´||Key==´S´)
{
ingame=true;
gameInit();
}
}
return true;
}
//处理键盘抬起事件
public boolean keyUp(Event e, int key)
{
if(Key==Event.DOWN)
{
fast=false;
}
return true;
}
//paint()方法
public void paint(Graphics g)
{
Graphics gg;
if (goff==null&&d.width>0&&d.height>0)
{
ii=createImage(d.width, d.height);
goff=ii.getGraphics();
}
if(goff==null||ii==null)
return;
//设置背景色
goff.setColor(background);
goff.fillRect(0, 0, d.width, d.height);
//如果游戏已经开始
if(ingame)
//调用playGame()方法
playGame();
else//否则,显示其他信息
//显示初始信息
showIntro();
// 显示得分
showScore();
g.drawImage(ii, 0, 0, this);
}
//实现游戏
public void playGame()
{
boolean bottomreached=false;
boolean stillscrolling=false;
if(emptyline<0)
{
bottomreached=drawObject();
}
else
{
scrollDown();
stillscrolling=true;
}
drawBars();
drawBlocks();
if(stillscrolling||bottomreached)
{
checkFull();
}
}
//显示初始信息
public void showIntro()
{
String s;
drawBars();
drawBlocks();
if (showtitle)
{
goff.setFont(largefont);
s="俄罗斯方块"
goff.setColor(textcolor1);
goff.drawString(s, barwidth+(width-fmlarge.stringWidth(s))/2-2,height/2-22);
goff.setColor(Color.white);
goff.drawString(s, barwidth+(width-fmlarge.stringWidth(s))/2,height/2-20);
}
else
{
goff.setFont(smallfont);
s="请按下´S´键开始游戏";
goff.setColor(textcolor1);
goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2-1,height/2-31);
goff.setColor(Color.white);
goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2,height/2-30);
s="使用左右方向键控制运动";
goff.setColor(textcolor2);
goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2-1,height/2-11);
goff.setColor(Color.white);
goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2,height/2-10);
s="使用向下的方向键实现旋转";
goff.setColor(textcolor2);
goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2-1,height/2+9);
goff.setColor(Color.white);
goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2,height/2+10);
s="使用向下的方向键实现下降";
goff.setColor(textcolor2);
goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2-1,height/2+29);
goff.setColor(Color.white);
goff.drawString(s,barwidth+(width-fmsmall.stringWidth(s))/2,height/2+30);
}
screencount--;
if(screencount<=0)
{ screencount=screendelay; showtitle=!showtitle; }
}
public void drawBars()
{
goff.setColor(barcolor);
goff.fillRect(0,0,barwidth,blocksize*yblocks);
goff.fillRect(barwidth+blocksize*xblocks,0,barwidth,blocksize*yblocks);
goff.fillRect(0,blocksize*yblocks,xblocks*blocksize+2*barwidth,barwidth);
}
public boolean drawObject()
{
short i;
boolean bottomreached=false;
int x,y,checkx,checky;
//清除旧图形
for(i=0;i<4;i++)
{
x=objectx+items[objectptr+i*2+objectrotation*itemrotlen];
y=objecty+items[objectptr+i*2+objectrotation*itemrotlen+1];
checkx=objectx+checks[objectptr+i*2+objectrotation*itemrotlen];
checky=objecty+checks[objectptr+i*2+objectrotation*itemrotlen+1];
if(y>=0)
screendata[x][y]=0;
if(screendata[checks][checky]!=0)
bottomreached=true;
}
if(!bottomreached)
{
count--;
if(count<=0||fast)
{
objecty++;
count=curcount;
}
checkRotation();
objectdx=0;
objectrotationd=0;
}
//绘制新图形
for(i=0;i<4;i++)
{
x=objectx+items[objectptr+i*2+objectrotation*itemrotlen];
y=objecty+items[objectptr+i*2+objectrotation*itemrotlen+1];
if(y>=0)
screendata[x][y]=objectcolor;
if(y>=(yblocks-1))
bottomreached=true;
}
if (bottomreached)
{
score++;
newObject();
}
return bottomreached;
}
public void checkRotation()
{
int dummyx;
int dummyrot;
int x,y;
short i;
boolean cando=true;
dummyrot=(objectrotation+objectrotationd)%4;
dummyx=objectx+objectdx;
//确认该部分不会使屏幕旋转
for(i=0;i<4;i++)
{
x=dummyx+items[objectptr+i*2+dummyrot*itemrotlen];
if(x>=xblocks)
dummyx-=(x-xblocks+1);
else if(x<0)
dummyx-=x;
}
for(i=0;(i<4&&cando);i++)
{
x=dummyx+items[objectptr+i*2+dummyrot*itemrotlen];
y=objecty+items[objectptr+i*2+dummyrot*itemrotlen+1];
if(y>=0)
cando=cando&&(screendata[x][y]==0);
if(y>=yblocks||x<0||x>=xblocks)
cando=false;
}
if(cando)
{
objectrotation=dummyrot;
objectx=dummyx;
}
}
public void drawBlocks()
{
short x,y;
for(x=0;x<xblocks;x++)
{
for(y=0;y<yblocks;y++)
{
goff.setColor(blocks[screendata[x][y]]);
goff.drawRect(x*blocksize+barwidth,y*blocksize,blocksize-1,blocksize-1);
goff.fillRect(x*blocksize+barwidth+3,y*blocksize+3,blocksize-6,blocksize-6);
}
}
}
public void checkFull()
{
short x,y;
boolean found=false;
for(y=yblocks-1;(y>=0&&!found);y--)
{
found=true;
for(x=0;x<xblocks;x++)
{
if(screendata[x][y]==0)
found=false;
}
if(found)
{
score+=10;
//当用户的得分很多时,增加游戏速度
if(score>800)
curcount=1;
else if(score>600)
curcount=2;
else if(score>400)
curcount=3;
else if(score>200)
curcount=4;
for(x=0;x<xblocks;x++)
{
screendata[x][y]=0;
}
emptyline=y;
}
}
}
public void scrollDown()
{
short x,y;
for(y=emptyline;y>0;y--)
{
for(x=0;x<xblocks;x++)
{
screendata[x][y]=screendata[x][y-1];
}
}
for(x=0;x<xblocks;x++)
{
screendata[x][0]=0;
}
emptyline=-1;
}
public void showScore()
{
String s;
goff.setFont(smallfont);
goff.setColor(Color.white);
s="得分: "+score;
goff.drawString(s,width/2-40,(yblocks+1)*blocksize+10);
}
public void run()
{
long starttime;
Graphics g;
Thread.currentThread().setPriorit(Thread.MAX_PRIORITY);
g=getGraphics();
while(true)
{
starttime=System.currentTimeMillis();
try
{
paint(g);
starttime+=60;
Thread.sleep(Math.max(0,starttime-System.currentTimeMillis()));
}
catch(InterruptedException e)
{
break;}
}
}
public void start()
{
if(thethread==null) {
thethread=new Thread(this);
thethread.start();
}
}
public void stop()
{
if (thethread!=null) {
thethread.stop();
thethread=null;
}
}
}
用java编写的俄罗斯方块小游戏
时间: 2024-10-28 20:08:20
用java编写的俄罗斯方块小游戏的相关文章
java编写贪吃蛇小游戏_java
废话不多说,直接奉上代码: Frame.java package snake; import java.awt.Graphics; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import
Java编写猜数字小游戏_java
本文实例讲述了java实现的简单猜数字游戏代码.分享给大家供大家参考. 以下是Java语言写的一个猜数字小游戏引用片段: import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; //主函数 public class calssOne { public static void main(String[] args) { //shit+Ctrl+o int result; //随机产生一个
Java俄罗斯方块小游戏_java
去年就已经学了这个技术了,一直没去写,现在抽个时间写了个俄罗斯方块游戏. 只有简单的新游戏,暂停,继续,积分功能.简单的实现了俄罗斯的经典功能. 不介绍了,有兴趣的自己运行一下,后面贴出了图片. 代码: package cn.hncu; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.Act
音频-我用java写了一个小游戏,想插入背景音乐但不太会,求指教
问题描述 我用java写了一个小游戏,想插入背景音乐但不太会,求指教 package text; import java.io.*; import javax.sound.sampled.*; public class text { public static void main(String[] args) { // TODO Auto-generated method stub //创建一个实例 AePlayWave apw = new AePlayWave("G:\照片\Sleep Awa
js实现俄罗斯方块小游戏分享
这篇文章主要介绍了js实现俄罗斯方块小游戏分享,,需要的朋友可以参考下 代码如下: <!doctype html><html><head><style type="text/css"> body { background:#000; font:25px/25px 宋体;} #box { float:left;width:252px;border:#999 20px ridge;color:#9f9;text-shadow:2px 3px
c-C语言编写黑白棋小游戏= =BUG咋回事
问题描述 C语言编写黑白棋小游戏= =BUG咋回事 #includevoid black(char a[][10]);void white(char a[][10]);void victory(char a[][10]);//int play(char a[][10]);void table(char a[][10]);void turnblack(char a[][10]int rowint lineint iint mode);void turnwhite(char a[][10]int r
编译码 vhdl源程序-用VHDL语言编写打砖块小游戏,求程序啊!
问题描述 用VHDL语言编写打砖块小游戏,求程序啊! 界面下面有一个挡板,用来反弹小球,打掉的砖块会消掉,有背景音乐,求程序 解决方案 http://www.pudn.com/downloads575/sourcecode/game/detail2362465.html
Java完美实现2048小游戏_java
完美地模仿了2048游戏,是根据网友的一个2048改的. Block.java import javax.swing.*; import java.awt.*; public class Block extends JLabel { private int value; public Block() { value = 0;//初始化值为0 setFont(new Font("font", Font.PLAIN, 40));//设定字体 setBackground(Color.gray
打豆豆小游戏 用javascript编写的[打豆豆]小游戏_javascript技巧
闲来无事,花了两天时间,用javascript 写了一个打豆豆的游戏,至于怎么玩的就不细说了,到网上搜一下就知道了,很简单. 下面是源码,发布出来供大家学习交流. 由于本程序没有用到一个图片,所以界面在ie下豆豆都是方形的比较扎眼,建议用chrome浏览器运行,或者firefox 演示地址:http://www.imkoko.com/app/dadoudou.php 不多说,上代码 复制代码 代码如下: <!DOCTYPE html> <html> <head> <