扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
;
成都创新互联是一家专业提供浦东企业网站建设,专注与做网站、成都网站设计、H5开发、小程序制作等业务。10年已为浦东众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
要制造那种效果只需要大约 30 行 Java 代码:
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class RollingBall extends JPanel {
Ellipse2D.Float ball = new Ellipse2D.Float( -100, 100, 50, 50 );
public void paintComponent( Graphics g ) {
super.paintComponent( g );
Graphics2D g2 = ( Graphics2D ) g;
// Draw the ball
g2.fill( ball );
// Draw the rotating ellipse by skewing the Device Space
double angdeg = // One rotation per ball's travelling over its perimeter
ball.x++ % ( Math.PI * ball.width ) / ( Math.PI * ball.width ) * 360;
g2.rotate( Math.toRadians( angdeg ), ball.getCenterX( ), ball.getCenterY( ) );
g2.scale( .5, 1 );
g2.translate( ball.getCenterX( ), 0 );
g2.setColor( Color.gray );
g2.fill( ball );
}
public void roll( ) throws Exception {
while( true ) {
repaint( );
Thread.sleep( 8 );
}
}
public static void main( String[ ] args ) throws Exception {
JFrame f = new JFrame( );
RollingBall rb = new RollingBall( );
f.setSize( 999, 185 );
f.getContentPane( ).add( rb );
f.setVisible( true );
rb.roll( );
}
}
#includegraphics.h
#includeconio.h
#define HIGH 480
#define WIDTH 640
#define N 14//砖块的数目
int ball_x,ball_y;
int ball_vx,ball_vy;
int radius;
int bar_x,bar_y;
int bar_high,bar_width;
int bar_left,bar_right,bar_top,bar_bottom;
int isbrick[N];
int brick_high,brick_width;
void startup()//数据初始化
{
initgraph(640,480);
ball_x=WIDTH/2;
ball_y=HIGH/2;
ball_vx=4;
ball_vy=4;
radius=20;
bar_high=HIGH/14;
bar_width=WIDTH/4;
bar_x=WIDTH/2;
bar_y=HIGH-bar_high/2;
bar_left=bar_x-bar_width/2;
bar_right=bar_x+bar_width/2;
bar_top=bar_y-bar_high/2;
bar_bottom=bar_y+bar_high/2;
brick_width=WIDTH/N;
brick_high=HIGH/N;
int i;
for(i=0;iN;i++)
isbrick[i]=1;
BeginBatchDraw();
}
void clear()//清除画面
{
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x,ball_y,radius);
bar(bar_left,bar_top,bar_right,bar_bottom);
int i,brick_left,brick_right,brick_top,brick_bottom;
for(i=0;iN;i++)
{
brick_left=i*brick_width;
brick_right=brick_left+brick_width;
brick_top=0;
brick_bottom=brick_high;
if(!isbrick[i])
fillrectangle(brick_left,brick_top,brick_right,brick_bottom);
}
}
void show()//显示画面
{
setcolor(RED);
setfillcolor(WHITE);
fillcircle(ball_x,ball_y,radius);
bar(bar_left,bar_top,bar_right,bar_bottom);
int i,brick_left,brick_right,brick_top,brick_bottom;
for(i=0;iN;i++)
{
brick_left=i*brick_width;
brick_right=brick_left+brick_width;
brick_top=0;
brick_bottom=brick_high;
if(isbrick[i])
{setcolor(WHITE);
setfillcolor(BROWN);
fillrectangle(brick_left,brick_top,brick_right,brick_bottom);
}
}
FlushBatchDraw();
Sleep(10);
}
void output()//与用户输入无关的更新
{
if(((ball_y+radius=bar_top)(ball_y+radiusbar_bottom-bar_high/3))
||((ball_y-radius=bar_bottom)(ball_y-radiusbar_top-bar_high/3)))
if((ball_x=bar_left)(ball_x=bar_right))//挡板与小球碰撞
ball_vy=-ball_vy;
ball_x=ball_x+ball_vx;//小球更新坐标
ball_y=ball_y+ball_vy;
if((ball_x=radius)||(ball_x=WIDTH-radius))//小球碰到边框返回
ball_vx=-ball_vx;
if((ball_y=radius)||(ball_y=HIGH-radius))
ball_vy=-ball_vy;
int i,brick_left,brick_right,brick_top,brick_bottom;//判断小球是否和砖块碰撞
for(i=0;iN;i++)
{
if(isbrick[i])//砖块存在
{
brick_left=i*brick_width;
brick_right=brick_left+brick_width;
brick_bottom=brick_high;
if((ball_y==brick_bottom+radius)(ball_x=brick_left)(ball_x=brick_right))
{
isbrick[i]=0;
ball_vy=-ball_vy;
}
}
}
}
void Input()//与用户输入有关的更新
{
{
MOUSEMSG m;//定义鼠标信息
if(MouseHit())//检测当前是否有鼠标信息
{
m=GetMouseMsg();//获取一条鼠标信息
if(m.uMsg==WM_MOUSEMOVE)
{
//挡板的值得鼠标的位置
bar_x=m.x;
bar_y=m.y;
bar_left=bar_x-bar_width/2;
bar_right=bar_x+bar_width/2;
bar_top=bar_y-bar_high/2;
bar_bottom=bar_y+bar_high/2;
}
else if(m.uMsg==WM_LBUTTONDOWN)//按下鼠标左键,初始化小球的位置为挡板上面中心
{
ball_x=bar_x;
ball_y=bar_top-radius-3;
}
}
}
}
void gameover()//游戏结束
{
EndBatchDraw();
closegraph();
}
main()
{
startup();
while(1)
{
clear();
output();
Input();
show();
}
gameover();
return 0;
}
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import javax.swing.JFrame;
public class dazhuankuai implements KeyListener
{
private JFrame jframe;
Canvas canvas;
board b;
int x;
int scoret;
int diffscore;
private int canvaswidth = 400;//canvas 的属性
private int canvasheight =600;
dazhuankuai(){
jframe = new JFrame("打砖块");
canvas = new Canvas();
canvas.setSize(canvaswidth, canvasheight);
canvas.addKeyListener(this);
jframe.add(canvas);
jframe.setBounds(320, 100,410, 500);
jframe.addKeyListener(this);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
begin();
}
public void begin()
{ b=new board(this);
b.run();
b.ball.vx=5;
b.ball.vy=10;
}
void paint()
{Graphics g = canvas.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 400, 350);
for(int i=0;i20;i++)
for(int j=0;j20;j++)
if(b.matrix[i][j]){
g.setColor(b.brickcolor[i][j]);
g.fillRect(i*20, j*10, 20, 10);
g.setColor(Color.white);
g.drawRect(i*20, j*10, 20, 10);}
g.setColor(Color.RED);
if(b.ball.y=350)
g.fillOval(b.ball.x, b.ball.y, 10, 10);
}
void paintrect()
{Graphics g = canvas.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 350, 400, 10);
g.setColor(Color.black);
g.fillRect(b.leftpoint, 350, 50, 10);
}
void paintscore()
给一个建议参考,如果你觉得有用可以试试哈~
用一个结构体定义小球,包含的其中一个变量:short direction;用来指示运动方向,再包含一个函数指针,指向一个函数如:short funL();若小球四周均没有障碍,返回0,优先判断上下,再判断左右,或反过来,再根据哪个方向有障碍返回1~4,1~4分别代表什么方向自己定义,可以用宏,若函数返回1~4,根据相应的情况改变变量direction的值就可以了
c语言游戏中实现动画靠的是1秒钟多于25次的刷新。大一用easyx做过类似的,现在有点忘了,核心思想大概是
while(判断游戏未结束)//时间没停,未触发游戏终止标志
{
for(int i=0;iobjectNum;i++)
{
//根据时间更新每个砖块的状态
//如果某砖块的flag设为被打到,清除该物品,如果是链表删节点
//未被打到,砖块.y更新
}
//画背景图
for(int i=0;iobjectNum;i++)
{
//画每个砖块
}
//获取用户命令
//一旦有命令,DispatchCommand()
//调用那个函数,检测鼠标位置停留的时候是不是按键了,检测有没有操作砖块
//sleep(100ms),延时造成视觉停留
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流