弹球游戏java代码 java弹球游戏代码流程图

[求助]急求弹球游戏代码已使用TC2.0 IDE测试,正常运行:
引用:
#include "graphics.h"
#include "math.h"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ESC 0x011b
int x,y,x1,y1,key,t1,t2,keyx,keylong,keymove,over=0,i,tt=0,fast;
int ballx[9]={53,103,263,120,236,390,410,450,600};
int bally[9]={158,239,120,113,30,313,120,263,231},ballz[9]={0};
float ls;
win()
{float lx,ly,ll;
for(i=0;i9;i)
{
if(ballz==1)/*如果球体已经消失就不判断*/
continue;
lx=(float)(ballx-x);
ly=(float)bally-y;
ll=lx*lx ly*ly;
ls=sqrt(ll);
if((int)ls17)/*判断两球是否碰到*/
{tt;/*记录消失几个球*/
fast-=50;/*速度加快*/
if(i%2==0)/*移动加快*/
keymove =5;
else
{
setcolor(0);
rectangle(keyx,460,keyx keylong,470);
keylong-=10;/*长条变短*/
setcolor(YELLOW);
rectangle(keyx,460,keyx keylong,470);
}
ballz=1;
setcolor(0);
circle(ballx,bally,10);
if(ybally)
if(xballx)
{t1=-1;t2=-1;}
else
{t1=1;t2=-1;}
else
if(xballx)
{t1=-1;t2=1;}
else
{t1=1;t2=1;}
}
}
}
fungo()
{ x=100,y=50,keyx=200,keylong=100,keymove=15,fast=600;
for(i=0;i9;i)
{
setcolor(i 1);
circle(ballx,bally,10);
}
sleep(1);
t1=t2=1;
while(1)
{
while(!kbhit())
{
setcolor(YELLOW);
rectangle(keyx,460,keyx keylong,470);
x1=x;
y1=y;
x =t1;
y =t2;
setcolor(4);
circle(x,y,6);
delay(fast);
setcolor(0);
circle(x,y,6);
win();
if(tt==9)/*胜利*/
{
cleardevice();
over=1;
setcolor(RED);
settextstyle(0,0,4);
setusercharsize(3,1,3,1);
outtextxy(200,200," YOU WIN! ");
break;
}
if(y480)/*失败*/
{
cleardevice();
over=1;
setcolor(RED);
settextstyle(0,0,4);
setusercharsize(3,1,3,1);
outtextxy(200,200,"GAME OVER");
break;
}
else if(x==11y==11)/*判断碰到东西*/
{t1=1;t2=1;}
else if(x==629y==11)
{t1=-1;t2=1;}
else if(x==11)
{
if(yy1)
{t1=1;t2=-1;}
else
{t1=1;t2=1;}
}
else if(y==11)
{
if(xx1)
{t1=-1;t2=1;}
else
{t1=1;t2=1;}
}
else if(x==629)
{
if(y1y)
{t1=-1;t2=-1;}
else
{t1=-1;t2=1;}
}
else if(y==449)
{
if(x=keyxx=keyx keylong)
{
if(x(keyx keylong/4))
{t1=-1;t2=-1;}
else if(x=(keyx keylong/4)x(keyx keylong/2))
{t1=-1;t2=-2;}
else if(x=(keyx keylong/2)x(keyx keylong/4*3))
{t1=1;t2=-2;}
else
{t1=1;t2=-1;}
}
}
}
if(over)
break;
key=bioskey(0);
setcolor(0);
rectangle(keyx,460,keyx keylong,470);
if(key==ESC)
{cleardevice();break;}
else if(key==LEFT)
keyx-=keymove;
else if(key==RIGHT)
keyx =keymove;
}
}
main()
{int gd=DETECT,gr;
initgraph(gd,gr,"c:\\tc");
fungo();
getch();
sleep(1);
closegraph();
}
谁帮我一下修改一个简单的java程序?关于多线程的弹球程序 。主要问题:编译运行以后就是不能出现小球.此种情况不应该使用多线程,要是开发像雷电一样射击类的游戏,岂不是线程多如牛毛?
用C/C++语言写弹球游戏#includeiostream using namespace std; int main() { int m; float i,h1,h2; cout"请输入皮球初始高度:"endl; cinh1; cout"请输入皮球弹跳次数:"endl; cinm; h2=h1; for(i=1;i=m;i) { h2=h2 h1; h1=h1/2; } cout"第"m"次的反弹高度是:"h1endl; cout"经过路程是:"h2endl; return 0; }
java弹球游戏import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CopyOfBallCrash implements Runnable {
public static void main(final String[] args) {
new Thread(new CopyOfBallCrash()).start();
}
private final intwidth= 400;
private final intheight= 700;
private intmouse_X, mouse_Y;
private final BufferedImage offscreen = new BufferedImage(width, height,
BufferedImage.TYPE_4BYTE_ABGR);
private final JPanelpanel= new JPanel();
private final Shapeball= new Shape(100, 100, 1, 1, 20);
private final Shaperect= new Shape(0, 100, 20);
public CopyOfBallCrash() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(new Dimension(width, height));
ball.setBounds(width, height);
rect.setBounds(width, height);
frame.setContentPane(panel);
panel.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(final MouseEvent e) {}
@Override
public void mouseMoved(final MouseEvent e) {
mouse_X = e.getX();
mouse_Y = e.getY();
}
});
frame.pack();
frame.setVisible(true);
}
public void paint(final Graphics g) {
final Graphics2D g2d = offscreen.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.blue);
rect.drawRect(g2d, mouse_X);
g2d.setColor(Color.red);
ball.drawOval(g2d, rect);
if (Shape.isLose) g2d.drawString("你输了!!!", 100, 300);
g2d.dispose();
g.drawImage(offscreen, 0, 0, null);
}
@Override
public void run() {
while (true)
paint(panel.getGraphics());
}
}
class Shape {
public intwidth, height;
public intx, y, vx, vy, r, w, h;
public static boolean isLose;
public Shape(final int x, final int y, final int vx, final int vy, final int r) {
super();
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.r = r;
}
public Shape(final int x, final int w, final int h) {
super();
this.x = x;
this.w = w;
this.h = h;
}
public final void setBounds(final int width, final int height) {
this.width = width;
this.height = height;
}
public final void drawOval(final Graphics2D g2d, final Shape shape) {
if (yh = height) {
isLose = true;
return;
}
if (xvx = 0 || xvxw = width) vx = -vx;
if (yvy = 0) vy = -vy;
if (isCrashOutside(shape.x, shape.y, shape.w, shape.h)yw = shape.y)
vy = -vy;
x= vx;
y= vy;
g2d.fillOval(x, y, r, r);
}
public final void drawRect(final Graphics2D g2d, final int mouseX) {
y = height - h;
if (xwwidthxmouseX) x;
if (x0xmouseX) x--;
g2d.fillRect(x, y, w, h);
}
public final boolean isCrashOutside(final int x, final int y, final int w,
final int h) {
return (this.xx ? this.x = wx : x = rthis.x)
(this.yy ? this.y = hy : y = rthis.y);
}
}
Java程序解惑,希望大家帮帮忙 , 打砖块游戏不能用键盘控制 。开始和移动都不能 。运行是用application吗?import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ballApplet extends Applet implements Runnable {
Dimension d;
Font bFont = new Font("Helvetica", Font.BOLD, 24);
Font sFont = new Font("Helvetica", Font.BOLD, 14);
FontMetrics fmSmall, fmBig;
Graphics goff;
Image img;
Thread mThread;
boolean ingame = false;
int ballx, bally, batpos;
int batdpos = 0;
int balldx = 0, balldy = 0, dxval;
int iScroe, ballNum;
boolean[] showbrick;
int brickNumOfLine;
final int bWidth = 5;
final int batW = 20;
final int ballsize = 5;
final int batH = 5;
final int scoreH = 25;
final int brickW = 15;
final int brickH = 8;
final int space = 1;
final int backcol = 0x102040;
final int line = 4;
final int startline = 32;
public void init() {
Graphics g;
d=size();
setBackground(new Color(backcol));
brickNumOfLine = (d.width-2*bWidth)/(brickWspace);
d.width = brickNumOfLine*(brickW space) (2*bWidth);
g = getGraphics();
g.setFont(sFont);
fmSmall= g.getFontMetrics();
g.setFont(bFont);
fmBig = g.getFontMetrics();
showbrick = new boolean[brickNumOfLine*line];
this.addKeyListener(new BallGameKeyListener());
GameInit();
}
public void GameInit() {
batpos = (d.width - batW) / 2;
ballx = (d.width - ballsize) / 2;
bally = (d.height - ballsize - scoreH - 2 * bWidth);
iScroe = 0;
ballNum = 3;
dxval = 2;
if (Math.random()0.5)
balldx = dxval;
else
balldx = -dxval;
balldy = -dxval;
batdpos = 0;
InitBricks();
}
public void InitBricks() {
int i;
for (i = 0; iline * brickNumOfLine; i)
showbrick[i] = true;
}
// public boolean keyDown(Event e, int key) {
//if (ingame) {
//if (key == Event.LEFT)
//batdpos = -4;
//if (key == Event.RIGHT)
//batdpos = 4;
//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.LEFT || key == Event.RIGHT)
//batdpos = 0;
//return true;
// }
public void paint(Graphics g) {
if (goff == nulld.width0d.height0) {
img = createImage(d.width, d.height);
goff = img.getGraphics();
}
if (goff == null || img == null)
return;
goff.setColor(new Color(backcol));
goff.fillRect(0, 0, d.width, d.height);
if (ingame)
PlayGame();
else
ShowIntroScreen();
g.drawImage(img, 0, 0, this);
}
public void PlayGame() {
MoveBall();
CheckBat();
CheckBricks();
DrawPlayField();
DrawBricks();
ShowScore();
}
public void ShowIntroScreen() {
MoveBall();
CheckBat();
CheckBricks();
BatDummyMove();
DrawPlayField();
DrawBricks();
ShowScore();
goff.setFont(bFont);
goff.setColor(new Color(96, 128, 255));
String s = "弹球游戏";
goff.drawString(s, (d.width - fmBig.stringWidth(s)) / 2, (d.height
- scoreH - bWidth) / 2 - 20);
goff.setFont(sFont);
goff.setColor(new Color(128, 255, 32));
s = "请按下'S'键开始游戏";
goff.drawString(s, (d.width - fmSmall.stringWidth(s)) / 2, (d.height
- scoreH - bWidth) / 230);
goff.setColor(new Color(255, 160, 64));
s = "使用方向键控制球拍移动";
goff.drawString(s, (d.width - fmSmall.stringWidth(s)) / 2, (d.height
- scoreH - bWidth) / 250);
}
public void DrawBricks() {
int i, j;
boolean nobricks = true;
int colorDelta = 255 / (line - 1);
for (j = 0; jline; j) {
for (i = 0; ibrickNumOfLine; i) {
if (showbrick[j * brickNumOfLinei]) {
nobricks = false;
goff.setColor(new Color(255, j * colorDelta, 255 - j
* colorDelta));
goff.fillRect(bWidthi * (brickWspace), startlinej
* (brickHspace), brickW, brickH);
}
}
}
if (nobricks) {
InitBricks();
if (ingame)
iScroe= 100;
}
}
public void DrawPlayField() {
goff.setColor(Color.white);
goff.fillRect(0, 0, d.width, bWidth);
goff.fillRect(0, 0, bWidth, d.height);
goff.fillRect(d.width - bWidth, 0, bWidth, d.height);
goff.fillRect(0, d.height - bWidth, d.width, bWidth);
goff.fillRect(batpos, d.height - 2 * bWidth - scoreH, batW, batH);
goff.fillRect(ballx, bally, ballsize, ballsize);
}
public void ShowScore() {
goff.setFont(sFont);
goff.setColor(Color.white);
goff.drawString("得分:"iScroe, 40, d.height - 10);
String s = "生命:"ballNum;
goff
.drawString(s, d.width - 40 - fmSmall.stringWidth(s),
d.height - 10);
}
public void MoveBall() {
ballx= balldx;
bally= balldy;
if (bally = bWidth) {
balldy = -balldy;
bally = bWidth;
}
if (bally = (d.height - ballsize - scoreH)) {
if (ingame) {
ballNum--;
if (ballNum = 0)
ingame = false;
}
ballx = batpos(batW - ballsize) / 2;
bally = startlineline * (brickHspace);
balldy = dxval;
balldx = 0;
}
if (ballx = (d.width - bWidth - ballsize)) {
balldx = -balldx;
ballx = d.width - bWidth - ballsize;
}
if (ballx = bWidth) {
balldx = -balldx;
ballx = bWidth;
}
}
public void BatDummyMove() {
if (ballx(batpos2))
batpos -= 3;
else if (ballx(batposbatW - 3))
batpos= 3;
}
public void CheckBat() {
batpos= batpos;
if (batposbWidth)
batpos = bWidth;
else if (batpos(d.width - bWidth - batW))
batpos = (d.width - bWidth - batW);
if (bally = (d.height - scoreH - 2 * bWidth - ballsize)
bally(d.height - scoreH - 2 * bWidth)
(ballxballsize) = batposballx = (batposbatW)) {
bally = d.height - scoreH - ballsize - bWidth * 2;
balldy = -dxval;
balldx = CheckBatBounce(balldx, ballx - batpos);
}
}
public int CheckBatBounce(int dy, int delta) {
int sign;
int stepsize, i = -ballsize, j = 0;
stepsize = (ballsizebatW) / 8;
if (dy0)
sign = 1;
else
sign = -1;
while (ibatWdeltai) {
i= stepsize;
j;
}
switch (j) {
case 0:
case 1:
return -4;
case 2:
return -3;
case 7:
return 3;
case 3:
case 6:
return sign * 2;
case 4:
case 5:
return sign * 1;
default:
return 4;
}
}
public void CheckBricks() {
int i, j, x, y;
int xspeed = balldx;
if (xspeed0)
xspeed = -xspeed;
int ydir = balldy;
if (bally(startline - ballsize)
|| bally(startlineline * (spacebrickH)))
return;
for (j = 0; jline; j) {
for (i = 0; ibrickNumOfLine; i) {
if (showbrick[j * brickNumOfLinei]) {
y = startlinej * (spacebrickH);
x = bWidthi * (spacebrickW);
if (bally = (y - ballsize)bally(ybrickH)
ballx = (x - ballsize)ballx(xbrickW)) {
showbrick[j * brickNumOfLinei] = false;
if (ingame)
iScroe= (line - j);
if (ballx = (x - ballsize)
ballx = (x - ballsize3)) {
balldx = -xspeed;
} else if (ballx = (xbrickW - 1)
ballx = (xbrickW - 4)) {
balldx = xspeed;
}
balldy = -ydir;
}
}
}
}
}
public void run() {
long starttime;
Graphics g = getGraphics();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
while (true) {
starttime = System.currentTimeMillis();
try {
paint(g);
starttime= 20;
Thread.sleep(Math
.max(0, starttime - System.currentTimeMillis()));
} catch (InterruptedException e) {
break;
}
}
}
public void start() {
if (mThread == null) {
mThread = new Thread(this);
mThread.start();
}
}
public void stop() {
if (mThread != null) {
mThread.stop();
mThread = null;
}
}
public static void main(String[] args) {
Frame frame = new Frame("弹球游戏");
ballApplet app = new ballApplet();
frame.add("Center", app);
frame.setSize(270, 350);
frame.validate();
frame.setVisible(true);
frame.addWindowListener(new WindowControl(app));
app.init();
app.start();
}
class BallGameKeyListener implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
// TODO Auto-generated method stub
if (ingame) {
switch(key){
case KeyEvent.VK_ESCAPE:
ingame = false;
break;
case KeyEvent.VK_RIGHT:
batdpos = 4;
break;
case KeyEvent.VK_LEFT:
batdpos = -4;
break;
}
} else {
if (key == KeyEvent.VK_S) {
ingame = true;
GameInit();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT)
batdpos = 0;
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
}
class WindowControl extends WindowAdapter {
Applet app;
public WindowControl(Applet app) {
this.app = app;
}
public void WindowClosing(WindowEvent e) {
app.stop();
app.destroy();
System.exit(0);
}
}
给你 了个keyListener 然后按你的理念给你加入了按键判定 。按S按键可以开始游戏但方向键不能移动应该是你的方法错误了你自己修改去把懒得看了 。。太长了 一般画可以移动的物体是直接按坐标来画然后移动的时候 = 好哦这-= 控制坐标我发现你的错误是你用batdpos这个变量去控制移动 。。但你根本没用这个变量去画图 。。所以不能移动 。。你自己去改把 。。
【弹球游戏java代码 java弹球游戏代码流程图】弹球游戏java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于java弹球游戏代码流程图、弹球游戏java代码的信息别忘了在本站进行查找喔 。

    推荐阅读