打砖块JAVA游戏代码
- 格式:doc
- 大小:35.00 KB
- 文档页数:17
用Lua编写一个简易的打砖块游戏Lua是一种轻量级的脚本语言,被广泛应用于游戏开发领域。
本文将介绍如何使用Lua编写一个简易的打砖块游戏。
首先,我们需要了解游戏的基本规则和元素。
然后,我们将逐步实现游戏的各个功能,包括游戏场景的绘制、球和挡板的移动、碰撞检测以及游戏结束的判断。
最后,我们将通过一个完整的示例代码来演示游戏的实现过程。
1. 游戏规则和元素打砖块游戏是一款经典的街机游戏,玩家需要控制挡板反弹球,并击碎屏幕上的砖块。
游戏的基本元素包括挡板、球和砖块。
2. 游戏场景的绘制在Lua中,我们可以使用一些图形库或游戏引擎来绘制游戏场景。
这里我们以Love2D游戏引擎为例,它提供了简单且强大的绘图功能。
首先,我们需要创建一个窗口,并设置窗口的宽度和高度。
然后,我们可以使用Love2D提供的函数来绘制各个游戏元素,如挡板、球和砖块。
3. 球和挡板的移动玩家可以使用键盘或触摸屏来控制挡板的移动。
在Lua中,我们可以监听键盘事件或触摸事件,并根据事件的类型和参数来更新挡板的位置。
球的移动可以通过更新球的坐标实现,同时需要考虑球的速度和方向。
4. 碰撞检测球和挡板之间的碰撞检测可以通过判断球的位置和挡板的位置来实现。
如果球和挡板的位置有重叠,则表示碰撞发生,需要改变球的方向。
砖块和球之间的碰撞检测也类似,需要根据砖块的位置和球的位置来判断是否发生碰撞,并更新砖块的状态。
5. 游戏结束的判断游戏的结束可以通过判断砖块是否全部被打碎或球是否跌落出界来实现。
如果砖块全部被打碎,则游戏胜利;如果球跌落出界,则游戏失败。
下面是使用Lua和Love2D实现的一个简易的打砖块游戏的示例代码:```lua-- 导入Love2D模块love = require("love")-- 游戏初始化function love.load()-- 窗口设置love.window.setMode(800, 600)-- 初始化挡板位置paddle = {x = 350, y = 500, width = 100, height = 20}-- 初始化球的位置和速度ball = {x = 400, y = 300, dx = 5, dy = -5, radius = 10}-- 初始化砖块bricks = {}for i = 1, 8 dofor j = 1, 5 dotable.insert(bricks, {x = 70 * i, y = 40 * j + 50, width = 60, height = 30})endendend-- 触摸事件处理函数function love.touchpressed(id, x, y, dx, dy, pressure)paddle.x = x - paddle.width / 2end-- 键盘事件处理函数function love.keypressed(key)if key == "left" thenpaddle.x = paddle.x - 10 elseif key == "right" then paddle.x = paddle.y + 10 endend-- 碰撞检测函数function checkCollision(a, b) if a.x < b.x + b.width and a.x + a.width > b.x and a.y < b.y + b.height and a.y + a.height > b.y then return trueendreturn falseend-- 更新函数function love.update(dt)-- 更新球的位置ball.x = ball.x + ball.dxball.y = ball.y + ball.dy-- 检测球和挡板之间的碰撞if checkCollision(paddle, ball) thenball.dy = -ball.dyend-- 检测球和砖块之间的碰撞for _, brick in ipairs(bricks) doif checkCollision(brick, ball) thentable.remove(bricks, _)ball.dy = -ball.dyendend-- 检测球是否跌落出界if ball.y > love.window.getHeight() then love.load()endend-- 绘制函数function love.draw()-- 绘制挡板love.graphics.rectangle("fill", paddle.x, paddle.y, paddle.width, paddle.height)-- 绘制球love.graphics.circle("fill", ball.x, ball.y, ball.radius)-- 绘制砖块for _, brick in ipairs(bricks) dolove.graphics.rectangle("fill", brick.x, brick.y, brick.width, brick.height)endend```通过以上代码,我们可以在Love2D游戏引擎中运行一个简易的打砖块游戏。
打砖块游戏编程实现打砖块游戏是一款经典的街机游戏,由于其简单而富有挑战性的玩法,一直备受玩家的喜爱。
在这篇文章中,将介绍打砖块游戏的编程实现,通过使用合适的格式来展示代码,以便清晰明了地呈现整个游戏开发的过程。
首先,我们需要创建一个游戏窗口,用于显示游戏界面。
借助常见的编程语言和相关引擎,如Python与Pygame,可以轻松地实现这一步骤。
以下是一个示例代码片段,展示了如何创建游戏窗口:```import pygame# 初始化游戏pygame.init()# 设置窗口大小window_width = 800window_height = 600window = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("打砖块游戏")```接下来,我们需要在游戏窗口中绘制游戏元素,包括小球、挡板和砖块。
下面是一个示例代码片段,展示了如何在游戏窗口中绘制挡板:```# 绘制挡板paddle_width = 100paddle_height = 20paddle_x = window_width // 2 - paddle_width // 2paddle_y = window_height - paddle_height - 10paddle_color = (255, 255, 255)def draw_paddle():pygame.draw.rect(window, paddle_color, (paddle_x, paddle_y,paddle_width, paddle_height))```在游戏中,小球会从游戏窗口的顶部开始移动,并弹跳到窗口的边界和游戏元素之间。
我们需要编写代码来实现这种移动和弹跳的效果。
以下是一个示例代码片段,展示了如何移动和弹跳小球:```# 设置小球起始位置和速度ball_radius = 10ball_x = window_width // 2ball_y = window_height // 2ball_speed_x = 3ball_speed_y = 3ball_color = (255, 255, 255)def move_ball():global ball_x, ball_y, ball_speed_x, ball_speed_y# 在x轴上移动小球ball_x += ball_speed_x# 在y轴上移动小球ball_y += ball_speed_y# 检测小球与窗口边界碰撞if ball_x <= 0 or ball_x >= window_width - ball_radius:ball_speed_x *= -1if ball_y <= 0 or ball_y >= window_height - ball_radius:ball_speed_y *= -1# 检测小球与挡板碰撞if ball_x >= paddle_x and ball_x <= paddle_x + paddle_width and ball_y >= paddle_y - ball_radius:ball_speed_y *= -1# 检测小球与砖块碰撞# TODO: 编写检测小球与砖块碰撞的代码```最后,我们需要在游戏中加入交互性,让玩家能够通过操作挡板来控制小球的运动,并通过击碎砖块获得分数。
Java程序课程设计任务书一、主要内容打砖块游戏是一种动作电子游戏的名称。
玩家操作一根萤幕上水平的“棒子”,让一颗不断弹来弹去的“球”在撞击作为过关目标消去的“砖块”的途中不会落到萤幕底下。
球碰到砖块、棒子与底下以外的三边会反弹,落到底下会失去一颗球,把砖块全部消去就可以破关。
二、具体要求通过图形用户界面(Graphics User Interface,GUI)和线程,使用户和程序之间可以方便地进行交互。
运用Swing组件,编写小应用程序游戏,加深对Java 语言的理解,深入地了解图形用户界面的设计,更加熟练地运用编程软件。
功能要求:(1)游戏运行需键盘的操作;(2)游戏可统计分数;(3)球落到底下会失去一颗,按ENTER可重新开始。
开发工具:JCreater软件;开发技术: J2ME。
三、进度安排12月28日:课程设计选题,查找参考资料12月29日:完成打砖块游戏程序设计分析12月30日 ~ 1月3日:完成程序代码的编写1月4日 ~ 1月5日:游戏测试与完善1月6日 ~ 1月7日:完成课程设计报告,准备答辩四、主要参考文献[1] (美)埃克尔著陈昊鹏,饶若楠等译. Java编程思想[J]. 机械工业出版社,2005[2](美)Gary 著张珑刘雅文译. Java编程原理[J]. 清华大学出版社,2004[3](美)Michael Morrison著徐刚,于健,薛雷译. 游戏编程入门[J]. 人民邮电出版社,[4](美)Wendy Stahler著冯宝坤,曹英译. 游戏编程中的数理应用[J]. 红旗出版社,2005[5](美)克罗夫特(David Wallace Croft)著彭晖译. Java游戏高级编程[J].清华大学出版社,2005[6](美)David Brackeen著邱仲潘译. Java游戏编程[J]. 科学出版社,2004[7] 聂庆亮编著. Java应用开发指南[J]. 清华大学出版社,2010[8] 耿祥义,张跃平编著. Java面向对象程序设计[J]. 清华大学出版社,2010[9] 杨绍方编著. Java编程实用技术与案例[J]. 清华大学出版社,[10] 明日科技编著. Java编程全能词典[J]. 电子工业出版社,2010摘要随着Java语言的不断发展和壮大,现在的Java已经广泛的应用于各个领域,包括医药,汽车工业,手机行业,游戏,等等地方。
Java编程经典⼩游戏设计-打砖块⼩游戏源码[程序中使⽤的数据结构和符号说明]HitBrick类GreenBallThread控制⼩球路线xUp,yUp,bouncing定义变量存储16位数值形式x,y⼩球坐标xDx,yDy坐标增量MAX_X,MAX_Y坐标最⼤值renew初始化label标签Rx,Ry横板坐标Brick[]砖块ball⼩球HitBrick()定义⼩球横板及砖块位置坐标keyPressd(keyEent)定义⼩球启动键(按空格键启动)keyReleased(keyEvent)接收键盘事件侦听器接⼝)keyTyped(keyEvent)键⼊空格键时调⽤keyEventpaint(Graphics)对砖块填充颜⾊move定义⼩球的运动轨迹和砖块的消失main主函数BallThread类通过继承Thread类使Ball类线程化,并把⼩球的弹跳动作放进Run()中执⾏Brick类定义砖块坐标位置和砖块按钮Ball类定义⼩球坐标位置[程序设计流程]程序中使⽤的部分⽅法解释开始命令:空格键privateJLabellabel;定义⼀个标签,label=newJLabel("按空格键开始");内容为空格键开始,addKeyListener(this);定义⼀个键盘监听器,if (e.getKeyCode() ==e.VK_SPACE) {if(renew){greenBallThread=new BallThread(this);bouncing = true;greenBallThread.start();label.setVisible(false);}renew=false;重置并开始游戏移动命令:⽅向键左键和右键if(e.getKeyCode()==e.VK_LEFT){Rx=Rx-20;if(bouncing){if(Rx<0){Rx=0;}}else{if(Rx<0){Rx=0;}else{x=x-20;ball.ball_x=x;}}repaint();}同开始命令原理,如果键⼊左键,横版向左移动20个单位(Rx为横板坐标),如果⼩球还在运动,当横板移到最左侧(Rx=0),不能再向左移动,则横板靠在最左侧(Rx=0),if(e.getKeyCode()==e.VK_RIGHT){Rx=Rx+20;if(bouncing){if(Rx+80>300){Rx=220;}}else{if(Rx+80>300){Rx=220;}else{x=x+20;ball.ball_x=x;}}repaint();}}向右移动同向左移动原理,因为定义界⾯横坐标最⼤值为300,横板长度80,故Rx=220时碰最右侧砖块设定:brick[0]=new Brick(0,60,50,20);brick[1]=new Brick(50,60,50,20);brick[2]=new Brick(100,60,50,20);……brick[16]=new Brick(200,160,50,20);brick[17]=new Brick(250,160,50,20);ball=new Ball(150,450,10,10);public void paint(Graphics g){super.paint(g);ball.rect.setLocation(x,y);if(bouncing){for(int i=0;i<=17;i++){if(brick[i].visible==true){switch(i){case 0 :g.setColor(Color.blue);break;case 1 :g.setColor(Color.cyan);break;case 2 :g.setColor(Color.gray);break;……case 17 :g.setColor(Color.yellow);break;g.fill3DRect(brick[i].brick_x,brick[i].brick_y,brick[i].brick_width,brick[i].brick_height,true);}}g.setColor(Color.red);g.fillOval(x, y, 10, 10);g.setColor(Color.blue);g.fillRect(Rx,Ry,80,20);brick[0]=newBrick(0,60,50,20);设置砖块坐标,ball=newBall(150,450,10,10);和⼩球的坐标if(brick[i].visible==true)判断砖块存在,⽤switch语句,逐个对砖块填充颜⾊,最后四⾏代码是分别对⼩球和横板颜⾊坐标的定义⼩球的移动:try{Thread.currentThread().sleep(25);}catch(InterruptedException exception){System.err.println(exception.toString());}定义⼩球的速度,若发⽣错误,则执⾏catch语句,打印错误for(int i=0;i<=17;i++){if(ball.rect.intersects(brick[i].rect)&&brick[i].visible){brick[i].visible=false;yUp=!yUp;/}}当⼩球接触到砖块时,砖块不可见(消失)if(x+5>Rx&&x+5<Rx+80&&y+10>=Ry){yUp=false;xDx=(int)(Math.random()*5+2);yDy=(int)(Math.random()*5+2);}判断⼩球坐标和横板坐标,当⼩球落在横板坐标之内,⼩球反弹,⼩球横坐标和纵坐标都以⼀个随机值改变后运动if(xUp==true){x+=xDx;}else{x-=xDx;}if(yUp==true){y+=yDy;}else{y-=yDy;}判断⼩球横坐标如果在增加,⼩球横坐标=⼩球原横坐标+⼩球横坐标增量,否则⼩球横坐标=⼩球原横坐标-⼩球横坐标增量;纵坐标同理if(y<=0){y=0;ball.ball_y=y;yUp=true;xDx=(int)(Math.random()*5+2);yDy=(int)(Math.random()*5+2);}else if(y>=MAX_Y-15){yDy=(int)(Math.random()*5+2);//yUp=false;break;}判断⼩球到画⾯顶部(定义顶部的纵坐标为0),⼩球向下反弹,原理同⼩球和横板接触的反弹规则,否则,判断⼩球纵坐标是否⼤于MAX_Y-15(纵坐标最⼤值-15),反弹规则改变为横坐标不变,纵坐标随机改变if(x<=0){ball.ball_x=x;xUp=true;xDx=(int)(Math.random()*5+2);yDy=(int)(Math.random()*5+2);}else if(x>=MAX_X-10){x=MAX_X-10;ball.ball_x=x;xDx=(int)(Math.random()*5+2);yDy=(int)(Math.random()*5+2);xUp=false;}判断⼩球到画⾯最左侧(定义最左侧横坐标为0),向右侧反弹,反弹规则同⼩球和横板接触的反弹规则,或者⼩球到画⾯最右侧,向左反弹,反弹规则同上,(if(x>=MAX_X-10)判断⼩球是否到右边侧,⼩球的直径为10)int i;for(i=0;i<=17&&brick[i].visible==false;i++){}if(i==18){break;}如果所有砖块都不可见,则重新玩renew=true; //初始化bouncing=false;for(int i=0;i<=17;i++){brick[i].visible=true;}xUp=true;yUp=false;xDx=1;yDy=1;x=150;y=450;Rx=120;Ry=460; //重新开始,初始化,⼩球静⽌,所有砖块可见,⼩球在横坐标⽅向,可随横板移动⽽移动,纵坐标在未开时游戏前不能改变,定义⼩球横坐标和纵坐标增量都为1,⼩球初始位置坐标(150,450)横板初始位置坐标(120,460)[源程序]import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.event.*;public class HitBrick extends JFrame implements KeyListener{private BallThread greenBallThread;//控制⼩球的线程private Boolean xUp,yUp,bouncing;private int x,y,xDx,yDy;//⼩球坐标,增量private final int MAX_X=300,MAX_Y=500;private Boolean renew;private JLabel label;private int Rx,Ry;//横板坐标private Brick brick[]=new Brick[18];//砖块private Ball ball;//⼩球public HitBrick(){super("打砖块");Container pane=getContentPane();//设置空⽩⾯板容器label=new JLabel("按空格键开始");//标签label.setHorizontalAlignment(JLabel.CENTER);//⽔平label.setVerticalAlignment(JLabel.BOTTOM);//垂直pane.add(label);//向⾯板⾥添加标签xUp=true;//横坐标可以移动yUp=false;//纵坐标不可以移动xDx=1;yDy=1;x=150;//⼩球坐标y=450;Rx=120;//横板坐标Ry=460;renew=true;bouncing=false;addKeyListener(this);//键盘监听器brick[0]=new Brick(0,60,50,20);//砖块坐标brick[1]=new Brick(50,60,50,20);brick[2]=new Brick(100,60,50,20);brick[3]=new Brick(150,60,50,20);brick[4]=new Brick(200,60,50,20);brick[5]=new Brick(250,60,50,20);brick[6]=new Brick(0,90,50,20);brick[7]=new Brick(50,110,50,20);brick[8]=new Brick(100,130,50,20);brick[9]=new Brick(150,130,50,20);brick[10]=new Brick(200,110,50,20);brick[11]=new Brick(250,90,50,20);brick[12]=new Brick(0,160,50,20);brick[13]=new Brick(50,160,50,20);brick[14]=new Brick(100,160,50,20);brick[15]=new Brick(150,160,50,20);brick[16]=new Brick(200,160,50,20);brick[17]=new Brick(250,160,50,20);ball=new Ball(150,450,10,10);//球的坐标setSize(MAX_X,MAX_Y);//窗⼝⼤⼩setResizable(false);setVisible( true );//可视化}public void keyPressed(KeyEvent e) { if (e.getKeyCode() ==e.VK_SPACE) { if(renew){greenBallThread=new BallThread(this); bouncing = true;greenBallThread.start();label.setVisible(false);}renew=false;}if(e.getKeyCode()==e.VK_LEFT){Rx=Rx-20;if(bouncing){if(Rx<0){Rx=0;}} else{if(Rx<0){Rx=0;} else{x=x-20;ball.ball_x=x;}}repaint();}if(e.getKeyCode()==e.VK_RIGHT){Rx=Rx+20;if(bouncing){if(Rx+80>300){Rx=220;}if(Rx+80>300){Rx=220;} else{x=x+20;ball.ball_x=x;}}repaint();}}public void keyReleased (KeyEvent e) {}public void keyTyped (KeyEvent e){}public void paint(Graphics g){super.paint(g);ball.rect.setLocation(x,y);if(bouncing){for (int i=0;i<=17;i++){if(brick[i].visible==true){switch(i){case 0 :g.setColor(Color.blue);break;case 1 :g.setColor(Color.cyan);break;case 2 :g.setColor(Color.gray);break;case 3 :g.setColor(Color.green);break;case 4 :g.setColor(Color.magenta);break;case 5 :g.setColor(Color.yellow);break;case 6 :g.setColor(Color.white);break;case 7 :g.setColor(Color.black);break;case 8 :g.setColor(Color.orange);break;case 9 :g.setColor(Color.pink);break;case 10 :g.setColor(Color.darkGray);break;case 11 :g.setColor(Color.red);break;case 12 :g.setColor(Color.blue);break;case 13 :g.setColor(Color.cyan);break;case 14 :g.setColor(Color.gray);break;case 15 :g.setColor(Color.green);break;case 16 :g.setColor(Color.magenta);break;case 17 :g.setColor(Color.yellow);break;}g.fill3DRect(brick[i].brick_x,brick[i].brick_y,brick[i].brick_width,brick[i].brick_height,true); }}g.setColor(Color.red);g.fillOval(x, y, 10, 10);g.setColor(Color.blue);g.fillRect(Rx,Ry,80,20);} else{for (int i=0;i<=17;i++){switch(i){case 0 :g.setColor(Color.blue);break;case 1 :g.setColor(Color.cyan);break;case 2 :g.setColor(Color.gray);break;case 3 :g.setColor(Color.green);case 4 :g.setColor(Color.magenta);break;case 5 :g.setColor(Color.yellow);break;case 6 :g.setColor(Color.white);break;case 7 :g.setColor(Color.black);break;case 8 :g.setColor(Color.orange);break;case 9 :g.setColor(Color.pink);break;case 10 :g.setColor(Color.darkGray);break;case 11 :g.setColor(Color.red);break;case 12 :g.setColor(Color.blue);break;case 13 :g.setColor(Color.cyan);break;case 14 :g.setColor(Color.gray);break;case 15 :g.setColor(Color.green);break;case 16 :g.setColor(Color.magenta);break;case 17 :g.setColor(Color.yellow);break;}g.fill3DRect(brick[i].brick_x,brick[i].brick_y,brick[i].brick_width,brick[i].brick_height,true); }g.setColor(Color.red);g.fillOval(x, y, 10, 10);g.setColor(Color.blue);g.fillRect(Rx, Ry, 80, 20);}}public void move(){while(true){try{Thread.currentThread().sleep(25);}catch(InterruptedException exception){System.err.println(exception.toString());}for (int i=0;i<=17;i++){if(ball.rect.intersects(brick[i].rect)&&brick[i].visible){brick[i].visible=false;yUp=!yUp;//打到球不可见}}if(x+5>Rx&&x+5<Rx+80&&y+10>=Ry){yUp=false;xDx=(int)(Math.random()*5+2);//⼩球坐标增量yDy=(int)(Math.random()*5+2);}if(xUp==true){x+=xDx;//⼩球左右移动坐标改变} else{x-=xDx;}if(yUp==true){y+=yDy;} else{y-=yDy;}if(y<=0){y=0;ball.ball_y=y;yUp=true;xDx=(int)(Math.random()*5+2);yDy=(int)(Math.random()*5+2);} else if(y>=MAX_Y-15){yDy=(int)(Math.random()*5+2);//yUp=false;break;}if(x<=0){x=0;ball.ball_x=x;xUp=true;xDx=(int)(Math.random()*5+2);yDy=(int)(Math.random()*5+2);} else if(x>=MAX_X-10){x=MAX_X-10;ball.ball_x=x;xDx=(int)(Math.random()*5+2);yDy=(int)(Math.random()*5+2);xUp=false;}ball.rect.setLocation(ball.ball_x,ball.ball_y); repaint();int i;//如果所有砖块都不可见for (i=0;i<=17&&brick[i].visible==false;i++){ //则重新玩}if(i==18){break;}//}renew=true;//初始化bouncing=false;for (int i=0;i<=17;i++){brick[i].visible=true;}xUp=true;yUp=false;xDx=1;yDy=1;x=150;y=450;Rx=120;Ry=460;//repaint();repaint();label.setVisible(true);}public static void main(String[] args) {HitBrick mar=new HitBrick();}}class BallThread extends Thread{private HitBrick m;BallThread(HitBrick a){//super();m=a;}public void run(){m.move();m.repaint();}}class Brick{Rectangle rect=null;//长⽅形对象,砖块按钮的位置和宽⾼int brick_x,brick_y;//按扭的左上⾓坐标int brick_width,brick_height;//按扭的宽和⾼Boolean visible;public Brick(int x,int y,int w,int h){brick_x=x;brick_y=y;brick_width=w;brick_height=h;visible=true;rect=new Rectangle(x,y,w,h);//创建长⽅形对象---砖块按钮的位置和宽⾼。
打砖块游戏源代码说明本游戏的设计步骤:一、布局界面(把图片资源拉进Main。
storyboard)二、让小球动起来(给他一个初速度)三、碰撞检测1、屏幕碰撞2、砖块碰撞3、挡板碰撞四、挡板移动//ViewController。
h#import<UIKit/UIKit。
h>@interfaceViewController:UIViewController@property(strong,nonatomic)IBOutletCollection(UIImageView)NSArray*blockImageArray;@property(weak,nonatomic)IBOutletUIImageView*paddleImage;@property(weak,nonatomic)IBOutletUIImageView*ballImage;@end//ViewController。
m#import"ViewController。
h"@interfaceViewController(){//用这个布尔值标示游戏是否正在进行中BOOLisPlaying;//加一个游戏时钟CADisplayLink*_gameTimer;//小球移动的速度CGPoint_speed;//手指移动位置的差值CGFloatchaX;}//1.检测屏幕碰撞-(BOOL)checkWithScreen;//2.砖块碰撞检测-(BOOL)checkWithBlock;//3.挡板碰撞-(void)checkWithPandle;@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];}#pragmamark1之前进行了页面的布局//开始游戏-(void)touchesEnded:(NSSet<UITouch*>*)toucheswithEvent:(UIEvent*)event{//游戏是否在进行中,如果游戏还没有开始就让游戏开始if(!isPlaying){isPlaying=YES;//初始化一个游戏时钟,让step方法1/60秒执行一次_gameTimer=[CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(step)];//将游戏放在runloop中/***runLoop做两件事情1.监听输入源,一般自动放到runLoop中2.监听定时器,一般需手动放到runLoop中*///[_gameTimeraddToRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSDefaultRunLoopMode];[_gameTimeraddToRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSDefaultRunLoopMode];//给小球一个初始的速度_speed=CGPointMake(0,-5);}else{chaX=0;}}#pragmamark2让小球动起来//让小球按照1/60的频率动起来-(void)step{//如果返回值是yes就代表游戏失败if([selfcheckWithScreen]){[selfgameOver:@"再来一次"];}if([selfcheckWithBlock]){[selfgameOver:@"你真棒"];}[selfcheckWithPandle];//小球每次的移动都是以上一次的位置作为参考_ballImage。
#include <graphics.h>#include<stdio.h>#include<windows.h>#include<mmsystem.h>#include <stdlib.h>#include <conio.h>#include<math.h>#include<time.h>#pragma warning(disable:4996)#pragma comment(lib,"Winmm.lib")#define PI 3.14159//圆周率int sum = 0;int chengji;int count = 0;void zhujiemian() {initgraph(840, 480);IMAGE img;loadimage(&img, _T("D:\\SuperBall\\youxi.jpg"));putimage(0, 0, &img);}char tmp[100];int c;void defenjiemian() {initgraph(840, 480);PlaySound(("D:\\SuperBall\\timeover.wav"), NULL, SND_FILENAME | SND_ASYNC);IMAGE img;loadimage(&img, _T("D:\\SuperBall\\youxi11.jpg"));putimage(0, 0, &img);sprintf(tmp, " ");sprintf(tmp, "%d分", chengji);outtextxy(500, 140, tmp);MOUSEMSG m;while (true) {m = GetMouseMsg();if (m.uMsg == WM_LBUTTONDOWN && m.x > 630 && m.y > 365 && m.x < 795 && m.y < 405) {zhujiemian();return;}}}void zuigaofen() {initgraph(640, 480);int zuigao = 0;if (zuigao < sum)zuigao = sum;outtextxy(270, 240, "你的最高分是: ");sprintf(tmp, "你的最高分是");sprintf(tmp, "%d分", zuigao);outtextxy(370, 240, tmp);outtextxy(270, 260, "你闯过的关数是: ");sprintf(tmp, "你最高闯过");sprintf(tmp, " %d关", count);outtextxy(370, 260, tmp);outtextxy(560, 460, "返回主菜单");MOUSEMSG m;while (true) {m = GetMouseMsg();if (m.uMsg == WM_LBUTTONDOWN && m.x > 560 && m.y > 460 && m.x < 640 && m.y < 480) {zhujiemian();return;}}}int score(int score) {char strsco[10];sum += score;itoa(sum, strsco, 10);outtextxy(750, 90, " ");outtextxy(750, 90, strcat(strsco, ""));return sum;}int Time(int ti){char strsec[10];int sec = 18 - GetTickCount() / 1000 + ti / 1000;if (sec < 0) sec = 0;itoa(sec, strsec, 10);outtextxy(750, 60, " ");outtextxy(750, 60, strcat(strsec, ""));return sec;}void zhuyao(){//画背景initgraph(840, 480);int speed = 1;//速度int ball = 1;//小球数量int ti = GetTickCount();count = 0;sum = 0;bool flag = true;score(0);char runTime[] = "游戏倒计时: ";outtextxy(620, 60, runTime);char scores[] = "得分:";outtextxy(620, 90, scores);char checkpiont[] = "第关";checkpiont[11] = count + 1 + '0';outtextxy(660, 30, checkpiont);mciSendString("open bgm.mp3 alias background", NULL, 0, NULL); mciSendString("play background", NULL, 0, NULL);int i = 0, j = -1;for (int q = 480;q >= 10;q--) {setlinecolor(RGB(211, 175, 148));line(100, q, 540, q);}setlinecolor(BROWN);for (int q = 0;q <= 5;q++) {rectangle(99 - q, 9 - q, 541 + q, 481 + q);}setfillcolor(YELLOW);fillrectangle(590, 240, 645, 260);outtextxy(650, 240, "分数+1");setfillcolor(RGB(202, 105, 36));fillrectangle(590, 260, 645, 280);outtextxy(650, 260, "分数+1;需碰撞两次");setfillcolor(LIGHTBLUE);fillrectangle(590, 280, 645, 300);outtextxy(650, 280, "分数+3;消去一行方块");setfillcolor(RED);fillrectangle(590, 300, 645, 320);outtextxy(650, 300, "分数+3;炸掉周围方块");setfillcolor(RGB(242, 236, 222));fillrectangle(590, 320, 645, 340);outtextxy(650, 320, "分数+1;小球加速");setfillcolor(RGB(127, 127, 127));fillrectangle(590, 340, 645, 360);outtextxy(650, 340, "分数+1;时间+5s");setfillcolor(LIGHTRED);fillrectangle(590, 360, 645, 380);outtextxy(650, 360, "分数+1;获得另一个小球");//画砖setlinecolor(RGB(211, 175, 148));setfillcolor(YELLOW);int zhuan[8][6];for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++)zhuan[i][j] = 1;for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++)fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1)); srand(time(NULL));int t;//生成需要打两次的深色砖for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++) {t = rand() % 2;if (t == 1) {t = rand() % 2;}if (t == 1) {zhuan[i][j] = 2;setfillcolor(RGB(202, 105, 36));fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));}}//生成消去一行的蓝色砖for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1)t = rand() % 2;}}if (t == 1) {zhuan[i][j] = 4;setfillcolor(LIGHTBLUE);fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));}}//生¦成能爆炸的红色砖for (int i = 1;i < 7;i++)for (int j = 1;j < 5;j++) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1)t = rand() % 2;}}if (t == 1) {zhuan[i][j] = 3;setfillcolor(RED);fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));}}//生成能增加小球速度的白色砖for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1)t = rand() % 2;}}}if (t == 1) {zhuan[i][j] = 5;setfillcolor(RGB(242, 236, 222));fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));}}//生成能增加时间的灰色砖for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1)t = rand() % 2;}}}if (t == 1) {zhuan[i][j] = 7;setfillcolor(RGB(127, 127, 127));fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));}}//生成能增加小球数量的粉色砖for (int i = 0;i < 8;i++) {if (ball == 2)break;for (int j = 0;j < 6;j++) {if (ball == 2)break;t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1)t = rand() % 2;}if (t == 1) {zhuan[i][j] = 6;setfillcolor(LIGHTRED);fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));ball = 2;}}}}ball = 1;//画瞄准线int x = 320, y = 390;char c = 'l';setlinecolor(BLACK);line(320, 390, 320, 479);i = 0;//操作过程while (flag) {int zi, zj;zi = i;zj = j;if (!Time(ti)) flag = false;if (kbhit()) {c = getch();//发射小球if (c == ' ') {setlinecolor(BLACK);setfillcolor(BLACK);fillcircle(x + i * 64, y, 5);int p, q;p = x + i * 64;q = y;int yp = p, yq = q;int v = yp, u = yq;int k = i, l = j;while (q <= 490) {setfillcolor(RGB(211, 175, 148));setlinecolor(RGB(211, 175, 148));fillcircle(p, q, 5);p += (speed + ball - 1)*i;q += (speed + ball - 1)*j;if (p >= 534 || p <= 106) {i = -i;PlaySound(("D:\\SuperBall\\fantan.wav"), NULL, SND_FILENAME | SND_ASYNC);}if (q <= 20) {j = -j;PlaySound(("D:\\SuperBall\\fantan.wav"), NULL, SND_FILENAME | SND_ASYNC);}for (int m = 0;m < 8;m++)for (int n = 0;n < 6;n++) {switch (zhuan[m][n]) {case 1:if ((p >= 95 + m * 55) && (p <= 105 + (m + 1) * 55) && (q >= 5 + n * 20) && (q <= 15 + (n + 1) * 20)) {setlinecolor(RGB(211, 175, 148));setfillcolor(RGB(211, 175, 148));fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);PlaySound(("D:\\SuperBall\\fantan.wav"), NULL, SND_FILENAME | SND_ASYNC);zhuan[m][n] = 0;score(1);if (q >= 10 + n * 20 && q <= 10 + (n + 1) * 20)i = -i;j = -j;if (q >= 10 + (n + 1) * 20)j = 1;}break;case 2:if ((p >= 95 + m * 55) && (p <= 105 + (m + 1) * 55) && (q >= 5 + n * 20) && (q <= 15 + (n + 1) * 20)) {setfillcolor(YELLOW);fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);PlaySound(("D:\\SuperBall\\fantan.wav"), NULL, SND_FILENAME | SND_ASYNC);zhuan[m][n] = 1;score(1);if (q >= 10 + n * 20 && q <= 10 + (n + 1) * 20)i = -i;j = -j;if (q >= 10 + (n + 1) * 20)j = 1;}break;case 3:if ((p >= 95 + m * 55) && (p <= 105 + (m + 1) * 55) && (q >= 5 + n * 20) && (q <= 15 + (n + 1) * 20)) {setfillcolor(RGB(211, 175, 148));fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);fillrectangle(100 + (m + 1) * 55, 10 + n * 20, 100 + (m + 2) * 55, 10 + (n + 1) * 20);fillrectangle(100 + (m + 1) * 55, 10 + (n + 1) * 20, 100 + (m + 2) * 55, 10 + (n + 2) * 20);fillrectangle(100 + (m + 1) * 55, 10 + (n - 1) * 20, 100 + (m + 2) * 55, 10 + n * 20);fillrectangle(100 + (m - 1) * 55, 10 + n * 20, 100 + m * 55, 10 + (n + 1) * 20);fillrectangle(100 + (m - 1) * 55, 10 + (n - 1) * 20, 100 + m * 55, 10 + n * 20);fillrectangle(100 + m * 55, 10 + (n - 1) * 20, 100 + (m + 1) * 55, 10 + n * 20);fillrectangle(100 + (m - 1) * 55, 10 + (n + 1) * 20, 100 + m * 55, 10 + (n + 2) * 20);zhuan[m][n] = 0; zhuan[m + 1][n] = 0; zhuan[m + 1][n + 1] = 0; zhuan[m + 1][n - 1] = 0; zhuan[m - 1][n] = 0; zhuan[m - 1][n + 1] = 0; zhuan[m - 1][n - 1] = 0; zhuan[m][n + 1] = 0; zhuan[m][n - 1] = 0;PlaySound(("D:\\SuperBall\\boom.wav"), NULL, SND_FILENAME | SND_ASYNC);score(3);if (q >= 10 + n * 20 && q <= 10 + (n + 1) * 20)i = -i;j = -j;if (q >= 10 + (n + 1) * 20)j = 1;}break;case 4:if ((p >= 95 + m * 55) && (p <= 105 + (m + 1) * 55) && (q >= 5 + n * 20) && (q <= 15 + (n + 1) * 20)) {setfillcolor(RGB(211, 175, 148));for (int z = 0;z < 8;z++) {fillrectangle(100 + z * 55, 10 + n * 20, 100 + (z + 1) * 55, 10 + (n + 1) * 20);zhuan[z][n] = 0;}score(3);PlaySound(("D:\\SuperBall\\hengsao.wav"), NULL, SND_FILENAME | SND_ASYNC);if (q >= 10 + n * 20 && q <= 10 + (n + 1) * 20)i = -i;j = -j;if (q >= 10 + (n + 1) * 20)j = 1;}break;case 5:if ((p >= 95 + m * 55) && (p <= 105 + (m + 1) * 55) && (q >= 5 + n * 20) && (q <= 15 + (n + 1) * 20)) {setfillcolor(RGB(211, 175, 148));fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);PlaySound(("D:\\SuperBall\\fantan.wav"), NULL, SND_FILENAME | SND_ASYNC);score(1);zhuan[m][n] = 0;speed += 1;if (q >= 10 + n * 20 && q <= 10 + (n + 1) * 20)i = -i;j = -j;if (q >= 10 + (n + 1) * 20)j = 1;}break;case 6:if ((p >= 95 + m * 55) && (p <= 105 + (m + 1) * 55) && (q >= 5 + n * 20) && (q <= 15 + (n + 1) * 20)) {setfillcolor(RGB(211, 175, 148));fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);PlaySound(("D:\\SuperBall\\fantan.wav"), NULL, SND_FILENAME | SND_ASYNC);zhuan[m][n] = 0;ball = 2;score(1);if (q >= 10 + n * 20 && q <= 10 + (n + 1) * 20)i = -i;j = -j;if (q >= 10 + (n + 1) * 20)j = 1;}break;case 7:if ((p >= 95 + m * 55) && (p <= 105 + (m + 1) * 55) && (q >= 5 + n * 20) && (q <= 15 + (n + 1) * 20)) {setfillcolor(RGB(211, 175, 148));fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);PlaySound(("D:\\SuperBall\\shijian.wav"), NULL, SND_FILENAME | SND_ASYNC);zhuan[m][n] = 0;ti += 5000;score(1);if (q >= 10 + n * 20 && q <= 10 + (n + 1) * 20)i = -i;j = -j;if (q >= 10 + (n + 1) * 20)j = 1;}break;}}setlinecolor(BLACK);setfillcolor(BLACK);fillcircle(p, q, 5);Sleep(10);if (!Time(ti)) flag = false;if (q <= 375 && ball == 2) {setfillcolor(RGB(211, 175, 148));setlinecolor(RGB(211, 175, 148));fillcircle(v, u, 5);v += (speed + ball - 1)*k;u += (speed + ball - 1)*l;if (v >= 534 || v <= 106)k = -k;if (u <= 20)l = -l;for (int m = 0;m < 8;m++)for (int n = 0;n < 6;n++) {switch (zhuan[m][n]) {case 1:if ((v >= 95 + m * 55) && (v <= 105 + (m + 1) * 55) && (u >= 5 + n * 20) && (u <= 15 + (n + 1) * 20)) {setlinecolor(RGB(211, 175, 148));setfillcolor(RGB(211, 175, 148));fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);PlaySound(("D:\\SuperBall\\fantan.wav"), NULL, SND_FILENAME | SND_ASYNC);zhuan[m][n] = 0;score(1);if (u >= 10 + n * 20 && u <= 10 + (n + 1) * 20)k = -k;l = -l;if (u >= 10 + (n + 1) * 20)l = 1;}break;case 2:if ((v >= 95 + m * 55) && (v <= 105 + (m + 1) * 55) && (u >= 5 + n * 20) && (u <= 15 + (n + 1) * 20)) {setfillcolor(YELLOW);fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);PlaySound(("D:\\SuperBall\\fantan.wav"), NULL, SND_FILENAME | SND_ASYNC);zhuan[m][n] = 1;score(1);if (u >= 10 + n * 20 && u <= 10 + (n + 1) * 20)k = -k;l = -l;if (u >= 10 + (n + 1) * 20)l = 1;}break;case 3:if ((v >= 95 + m * 55) && (v <= 105 + (m + 1) * 55) && (u >= 5 + n * 20) && (u <= 15 + (n + 1) * 20)) {setfillcolor(RGB(211, 175, 148));fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);fillrectangle(100 + (m + 1) * 55, 10 + n * 20, 100 + (m + 2) * 55, 10 + (n + 1) * 20);fillrectangle(100 + (m + 1) * 55, 10 + (n + 1) * 20,100 + (m + 2) * 55, 10 + (n + 2) * 20);fillrectangle(100 + (m + 1) * 55, 10 + (n - 1) * 20, 100 + (m + 2) * 55, 10 + n * 20);fillrectangle(100 + (m - 1) * 55, 10 + n * 20, 100 + m * 55, 10 + (n + 1) * 20);fillrectangle(100 + (m - 1) * 55, 10 + (n - 1) * 20, 100 + m * 55, 10 + n * 20);fillrectangle(100 + m * 55, 10 + (n - 1) * 20, 100 + (m + 1) * 55, 10 + n * 20);fillrectangle(100 + (m - 1) * 55, 10 + (n + 1) * 20, 100 + m * 55, 10 + (n + 2) * 20);zhuan[m][n] = 0; zhuan[m + 1][n] = 0; zhuan[m + 1][n + 1] = 0; zhuan[m + 1][n - 1] = 0; zhuan[m - 1][n] = 0; zhuan[m - 1][n + 1] = 0; zhuan[m - 1][n - 1] = 0; zhuan[m][n + 1] = 0; zhuan[m][n - 1] = 0;PlaySound(("D:\\SuperBall\\boom.wav"), NULL, SND_FILENAME | SND_ASYNC);score(3);if (u >= 10 + n * 20 && u <= 10 + (n + 1) * 20)k = -k;l = -l;if (u >= 10 + (n + 1) * 20)l = 1;}break;case 4:if ((v >= 95 + m * 55) && (v <= 105 + (m + 1) * 55) && (u >= 5 + n * 20) && (u <= 15 + (n + 1) * 20)) {setfillcolor(RGB(211, 175, 148));for (int z = 0;z < 8;z++) {fillrectangle(100 + z * 55, 10 + n * 20, 100 + (z + 1) * 55, 10 + (n + 1) * 20);zhuan[z][n] = 0;}PlaySound(("D:\\SuperBall\\hengsao.wav"), NULL, SND_FILENAME | SND_ASYNC);if (u >= 10 + n * 20 && u <= 10 + (n + 1) * 20)k = -k;l = -l;if (u >= 10 + (n + 1) * 20)l = 1;}break;case 5:if ((v >= 95 + m * 55) && (v <= 105 + (m + 1) * 55) && (u >= 5 + n * 20) && (u <= 15 + (n + 1) * 20)) {setfillcolor(RGB(211, 175, 148));fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);PlaySound(("D:\\SuperBall\\fantan.wav"), NULL, SND_FILENAME | SND_ASYNC);zhuan[m][n] = 0;score(1);speed += 1;if (u >= 10 + n * 20 && u <= 10 + (n + 1) * 20)k = -k;l = -l;if (u >= 10 + (n + 1) * 20)l = 1;}break;case 7:if ((v >= 95 + m * 55) && (v <= 105 + (m + 1) * 55) && (u >= 5 + n * 20) && (u <= 15 + (n + 1) * 20)) {setfillcolor(RGB(211, 175, 148));fillrectangle(100 + m * 55, 10 + n * 20, 100 + (m + 1) * 55, 10 + (n + 1) * 20);PlaySound(("D:\\SuperBall\\shijian.wav"), NULL, SND_FILENAME | SND_ASYNC);zhuan[m][n] = 0;if (u >= 10 + n * 20 && u <= 10 + (n + 1) * 20)k = -k;l = -l;if (u >= 10 + (n + 1) * 20)l = 1;}break;}}if (q <= 365) {setlinecolor(BLACK);setfillcolor(BLACK);fillcircle(v, u, 5);Sleep(10);}}}setfillcolor(RGB(211, 175, 148));setlinecolor(RGB(211, 175, 148));fillcircle(v, u, 5);j = -1;}}elsec = '.';i = zi;setlinecolor(RGB(211, 175, 148));line(x + i * 64, y, x, 479);i = zi;j = zj;int yx, yy, yi;yx = x;yi = i;yy = y;//瞄准线移动转向switch (c) {case'a':x -= 5;break;case'd':x += 5;break;case'q':i = -1;y = 410;break;case'e':i = 1;y = 410;break;case'w':i = 0;y = 390;break;}if (x + i * 64 <= 106 || x + i * 64 >= 540 || x <= 100 || x >= 534) { x = yx;i = yi;y = yy;}setlinecolor(BLACK);line(x + i * 64, y, x, 479);Sleep(10);//进入下一关int h, g, f = 0;for (h = 0;h < 8;h++)for (g = 0;g < 6;g++){if (zhuan[h][g] != 0)f = 1;}if (f == 0) {ti += 60000 - count * 10000;score(count * 10 + 20);count++;checkpiont[11] = count + 1 + '0';outtextxy(660, 30, checkpiont);setlinecolor(RGB(211, 175, 148));setfillcolor(YELLOW);for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++)zhuan[i][j] = 1;for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++)fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));srand(time(NULL));for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++) {t = rand() % 2;if (t == 1) {t = rand() % 2;}if (t == 1) {zhuan[i][j] = 2;setfillcolor(RGB(202, 105, 36));fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));}}for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1)t = rand() % 2;}}if (t == 1) {zhuan[i][j] = 4;setfillcolor(LIGHTBLUE);fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));}}for (int i = 1;i < 7;i++)for (int j = 1;j < 5;j++) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1)t = rand() % 2;}}if (t == 1) {zhuan[i][j] = 3;setfillcolor(RED);fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));}}for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1)t = rand() % 2;}}}if (t == 1) {zhuan[i][j] = 5;setfillcolor(RGB(242, 236, 222));fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));}}for (int i = 0;i < 8;i++)for (int j = 0;j < 6;j++) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1) {t = rand() % 2;if (t == 1)t = rand() % 2;}}}if (t == 1) {zhuan[i][j] = 7;setfillcolor(RGB(127, 127, 127));fillrectangle(100 + 55 * i, 10 + 20 * j, 100 + 55 * (i + 1), 10 + 20 * (j + 1));}}}}chengji = sum;defenjiemian();}void main(){zhujiemian();MOUSEMSG mmp;while (true) {mmp = GetMouseMsg();if (mmp.uMsg == WM_LBUTTONDOWN && mmp.x > 465 && mmp.y > 80 && mmp.x < 730 && mmp.y < 165) {zhuyao();}else if (mmp.uMsg == WM_LBUTTONDOWN && mmp.x > 465 && mmp.y > 265 && mmp.x < 730 && mmp.y < 340) {zuigaofen();}}}。
#include<graphics.h>#include<conio.h>#include<windows.h>#include<stdlib.h>#include<stdio.h>void draw(int x1){int m=0,n=0,x=40,y=20;int t=0,i,j,k=1;int a=0,b=127,c=88;for(i=0;i<8;i++){n=i*20;y=n+20;for(j=0;j<16-t;j++){m=(j+i)*40;x=m+40;POINT points[]={{m,n}, {x,n}, {x,y}, {m,y}}; setfillcolor(RGB(a,b,c));fillpolygon(points, 4);a=8*k+8;b=j*3+k+200;c=20*k;k=k+8;}t=t+2;k=1;}m=300;n=160;x=340;y=180;POINT points[]={{m,n}, {x,n}, {x,y}, {m,y}}; setfillcolor(RGB(a,b,c));fillpolygon(points, 4);}void end(){settextstyle(50, 0, "宋体", 0, 0, FW_BOLD, false, false, false);settextcolor(RED);outtextxy(200,200,"游戏结束!");getch();closegraph();}void start(){int n=0;loadimage(NULL,"IMSGE","background"); while(1){setbkmode(TRANSPARENT);settextstyle(50, 0, "楷体", 0, 0, FW_BOLD, false, false, false);if(n%2==0)settextcolor(GREEN);elsesettextcolor(RED);outtextxy(80,100,"请按空格键开始游戏"); Sleep(250);if(_kbhit())break;n++;}}void main(){int x=320,y=430,m=270,n=370; int a,b,c,e=0,f=0,g=0,k[3];int i,j;char d;initgraph(640,480);start();setbkcolor(WHITE); cleardevice();setlinecolor(WHITE);draw(1);while(1){setlinecolor(LIGHTBLUE); setlinestyle(PS_SOLID, 10); line(m,445,n,445); setlinestyle(PS_SOLID, 0); setlinecolor(WHITE); setfillcolor(BROWN); fillcircle(x,y,10);Sleep(60);setfillcolor(WHITE); fillcircle(x,y,10);if(_kbhit()){d=getch();setlinecolor(WHITE); setlinestyle(PS_SOLID, 10); line(m,445,n,445);if(n<640&&d=='d'){m=m+27;n=n+27;}else if(m>0&&d=='a'){m=m-27;n=n-27;}}//确定x,y的值// if(y>=435)end();if(y==430&&x>=m-5&&x<=n+5) {if(x>m&&x<=m+25){a=0;b=2;c=2;if(10/(x-m)==0)k[0]=2;else if(10/(x-m)>3)k[0]=3;elsek[0]=10/(x-m);}else if(x>m+25&&x<=m+55) {a=2;b=0;c=2;if(15/(x-m-25)==0)k[1]=2;else if(15/(x-m-25)>3)k[1]=3;elsek[1]=10/(x-m-25);}else if(x>m+55&&x<=n) {a=2;b=2;c=0;if(10/(x-m-55)==0)k[2]=2;else if(10/(x-m-55)>3)k[2]=3;elsek[2]=10/(x-m-55);}}if(1){if(e==0){y=y-10;if(y<=10){g=1-g;e=1;}if(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){e=1;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}else if(e==1){y=y+10;if(getpixel(x-1,y-14)!=WHITE||getpixel(x-1,y+14)! =WHITE||getpixel(x+1,y-14)!=WHITE||getpixel(x +1,y+14)!=WHITE){e=0;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}else if(y==430&&x>=m-5&&x<=n+5)e=0;}if(g==0&&a==0){x=x-k[0]*5;if(y<=10)g=1-g;else if(x<=10||x>=630)g=1;if(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=1;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}else if(g==1&&a==0){x=x+k[0]*5;if(y<=10)g=1-g;else if(x<=10||x>=630)g=0;elseif(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=0;setfillcolor(WHITE);for(i=10;i<=18;i++){for(j=10;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}if(g==0&&b==0){x=x-k[1]*2;if(y<=10)g=1-g;else if(x<=10||x>=630)g=1;elseif(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=1;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}else if(g==1&&b==0){x=x+k[1]*2;if(y<=10)g=1-g;else if(x<=10||x>=630)g=0;elseif(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)!=WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=0;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}if(g==0&&c==0){x=x-k[2]*5;if(y<=10)g=1-g;else if(x<=10||x>=630)g=1;elseif(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=1;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}else if(g==1&&c==0)x=x+k[2]*5;if(x<=10||x>=630)g=0;else if(y<=10)g=1-g;elseif(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=0;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}} end(); }。
Java程序课程设计任务书一、主要内容打砖块游戏是一种动作电子游戏的名称。
玩家操作一根萤幕上水平的“棒子”,让一颗不断弹来弹去的“球”在撞击作为过关目标消去的“砖块”的途中不会落到萤幕底下。
球碰到砖块、棒子与底下以外的三边会反弹,落到底下会失去一颗球,把砖块全部消去就可以破关。
二、具体要求通过图形用户界面(Graphics User Interface,GUI)和线程,使用户和程序之间可以方便地进行交互。
运用Swing组件,编写小应用程序游戏,加深对Java语言的理解,深入地了解图形用户界面的设计,更加熟练地运用编程软件。
功能要求:(1)游戏运行需键盘的操作;(2)游戏可统计分数;(3)球落到底下会失去一颗,按ENTER可重新开始。
开发工具:JCreater软件;开发技术:J2ME。
三、进度安排12月28日:课程设计选题,查找参考资料12月29日:完成打砖块游戏程序设计分析12月30日~ 1月3日:完成程序代码的编写1月4日~ 1月5日:游戏测试与完善1月6日~ 1月7日:完成课程设计报告,准备答辩四、主要参考文献[1] (美)埃克尔著陈昊鹏,饶若楠等译. Java编程思想[J]. 机械工业出版社,2005[2](美)Gary J.Bronson著张珑刘雅文译. Java编程原理[J]. 清华大学出版社,2004[3](美)Michael Morrison著徐刚,于健,薛雷译. 游戏编程入门[J]. 人民邮电出版社,2005.9[4](美)Wendy Stahler著冯宝坤,曹英译. 游戏编程中的数理应用[J]. 红旗出版社,2005[5](美)克罗夫特(David Wallace Croft)著彭晖译. Java游戏高级编程[J]. 清华大学出版社,2005[6](美)David Brackeen著邱仲潘译. Java游戏编程[J]. 科学出版社,2004[7] 聂庆亮编著. Java应用开发指南[J]. 清华大学出版社,2010[8] 耿祥义,张跃平编著. Java面向对象程序设计[J]. 清华大学出版社,2010[9] 杨绍方编著. Java编程实用技术与案例[J]. 清华大学出版社,2000.11[10] 明日科技编著. Java编程全能词典[J]. 电子工业出版社,2010摘要随着Java语言的不断发展和壮大,现在的Java已经广泛的应用于各个领域,包括医药,汽车工业,手机行业,游戏,等等地方。
——《打砖块》学院:班级:学号:姓名:一.游戏设计规则游戏开始时,小球会停在挡板正中间,且此时挡板可左右自由移动,当按下空格键后小球会弹出,并在程序设定的区域内不停的碰撞反弹。
当小球碰到墙壁、挡板和砖块时,均会以相反的速度反弹,并且砖块被碰撞后会自动消失。
挡板可左右移动以接住小球,若小球没有被接住,超出指定区域下边缘,则程序会自动提示“游戏结束”,点击“确定”后退出程序。
当所有的砖块均消失时,程序会弹出提示“恭喜你,你赢了”。
二.游戏设计思路整个游戏可以分为三个类:小球类、挡板类和砖块类。
其中最简单的当然是挡板了,在回调函数中增加“WM_KEYDOWN”的按键消息,在里面编写控制挡板移动的代码,挡板的移动是通过“上、下、左、右”四个键来实现的。
砖块在整个程序中负责两件事情,其一是程序初始化时随着小球和挡板出现在游戏界面中,再就是被小球碰到后消失。
控制砖块出现与否是通过一个bool变量“blockExist”来实现的,当该变量为“true”时,就将砖块贴到目标DC上,否则就不贴。
由于砖块数量很多,故可以用一个for循环来实现,所有的砖块用的都是一张图片,而变化的只是砖块的坐标。
本游戏中最复杂的要数小球了,因为小球要涉及到很多碰撞检测和反弹,而且这其中还和砖块、挡板有联系。
小球涉及到的碰撞有:小球与墙壁碰撞、小球与挡板碰撞、小球与砖块碰撞。
碰撞检测实际上是检测小球的坐标是否进入了某个特定的区域,若进入了,则小球要反弹;否则,按原来的路线继续前进。
小球能不断的反弹,事实上靠的是小球速度方向的改变,对于小球而言,它能出现在某个点上,是它所在坐标系中的坐标导致的。
若设小球的坐标为(x_ball,y_ball),其速度为水平:vx,竖直:vy,将其坐标与速度之间建立赋值关系,即:x_ball+=vx,y_ball-=vy。
只要对其坐标进行相对应的计算,就能控制小球的反弹了。
三.游戏主要代码int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){// TODO: Place code here.MSG msg;HACCEL hAccelTable;// Initialize global stringsLoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);LoadString(hInstance, IDC_APIDEMOGAME, szWindowClass, MAX_LOADSTRING);MyRegisterClass(hInstance);// Perform application initialization:if (!InitInstance (hInstance, nCmdShow)){return FALSE;}hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_APIDEMOGAME);// Main message loop:while (GetMessage(&msg, NULL, 0, 0)){if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)){TranslateMessage(&msg);DispatchMessage(&msg);}}return msg.wParam;}A TOM MyRegisterClass(HINSTANCE hInstance){WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = (WNDPROC)WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_APIDEMOGAME);wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = (LPCSTR)IDC_APIDEMOGAME;wcex.lpszClassName = szWindowClass;wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);return RegisterClassEx(&wcex);}BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){hInst = hInstance; // Store instance handle in our global variablehWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW ,400, 100, 400, 500, NULL, NULL, hInstance, NULL);if (!hWnd){return FALSE;}ShowWindow(hWnd, nCmdShow);UpdateWindow(hWnd);GetClientRect(hWnd,&rect); //取得内部窗口区域大小HBITMAP bmp;m_bg=(HBITMAP)LoadImage(NULL,"matlab.bmp",IMAGE_BITMAP,0,0, LR_LOADFROMFILE);m_board=(HBITMAP)LoadImage(NULL,"Board.bmp",IMAGE_BITMAP,0, 0,LR_LOADFROMFILE);m_ball=(HBITMAP)LoadImage(NULL,"ball_0.bmp",IMAGE_BITMAP,0,0, LR_LOADFROMFILE);bgDC=GetDC(hWnd);mdc=CreateCompatibleDC(bgDC);scDC=CreateCompatibleDC(bgDC);boardDC=CreateCompatibleDC(bgDC);ballDC=CreateCompatibleDC(bgDC);bmp=CreateCompatibleBitmap(bgDC,1024,745);SelectObject(mdc,bmp);SelectObject(scDC,m_bg);SelectObject(ballDC,m_ball);SelectObject(boardDC,m_board);int i;int bx=40,by=30; //控制砖块坐标//初始化砖块for(i=0;i<BN;i++){if(block[i].blockExist)block[i].bKilled=0;block[i].blockDC=CreateCompatibleDC(bgDC);m_block[i]=(HBITMAP)LoadImage(NULL,"block.bmp",IMAGE_BITMAP, 32,21,LR_LOADFROMFILE);SelectObject(block[i].blockDC,m_block[i]);block[i].rc.left=bx;block[i].rc.top=by;block[i].rc.right=block[i].rc.left+64;block[i].rc.bottom=block[i].rc.top+32;bx+=60;}return TRUE;}/********控制小球碰壁后反弹*************/ void DrawBall(){//计算X轴方向贴图坐标与速度x_ball+=vx;if(x_ball<=0){x_ball=0;vx=-vx;bHeng=false;}else if(x_ball>=rect.right-32){x_ball=rect.right-32;vx=-vx;bHeng=true;}//计算Y轴方向贴图坐标与速度y_ball-=vy;if(y_ball<=0){y_ball=0;vy=-vy;bZong=false;}/* if(y_ball>=rect.bottom-32){y_ball= rect.bottom-32;vy=-vy;}*/}/*****判断小球是否与砖块相碰*******/void CheckCrashBlock(){int i;float WIDTH = 96;float HEIGHT = 64;float tan=HEIGHT / WIDTH;//tanθ的值for(i=0;i<BN;i++){float x_ =(float)(x_ball - block[i].rc.left);float y_ =(float)(y_ball - block[i].rc.top);if((x_ball>=block[i].rc.left-32&&x_ball<=block[i].rc.right)&&(y_ball>=bloc k[i].rc.top-32&&y_ball<=block[i].rc.bottom)){block[i].bKilled=1;block[i].blockExist=false;num--;}else if(tan > abs(y_ / x_)) //在1、3区域{vx=-vx;x_ball=vx;}else //在2、4区域{vy=-vy;y_ball-=vy;}*/}}}/*****判断小球是否与挡板相碰******/void CheckCrashBoard(){if((y_ball+32)>y){if(((x_ball+64)>=x)&&(x_ball<=(x+128))){vy=-vy;y_ball-=vy;// vx=10;}else{MessageBox(hWnd,"游戏结束","提示",MB_OK|MB_ICONSTOP);exit(0);}}}void DrawMap(){GetClientRect(hWnd,&rect); //取得内部窗口区域大小int i;if(!win){BitBlt(mdc,0,0,1024,745,scDC,0,0,SRCCOPY);for(i=0;i<BN;i++){CheckCrashBlock();if(block[i].bKilled) //||block[i].blockDC==0continue;else{BitBlt(mdc,block[i].rc.left,block[i].rc.top,block[i].rc.right-block[i].rc.left,blo ck[i].rc.bottom-block[i].rc.top,block[i].blockDC,0,0,SRCCOPY);}}//挡板贴图BitBlt(mdc,x,y,128,32,boardDC,0,0,SRCCOPY);//小球贴图if(isKeyDown){DrawBall();CheckCrashBoard();}BitBlt(mdc,x_ball,y_ball,32,32,ballDC,32,0,SRCAND);BitBlt(mdc,x_ball,y_ball,32,32,ballDC,0,0,SRCPAINT);}//将整个图贴到目的DC上BitBlt(bgDC,0,0,1024,745,mdc,0,0,SRCCOPY);}DWORD WINAPI ThreadProc(LPVOID lpParameter ){void *ct = (void *)lpParameter;while(1){DrawMap();Sleep(80);}delete ct;return 0;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){int wmId, wmEvent;PAINTSTRUCT ps;HDC hdc;void *ct;TCHAR szHello[MAX_LOADSTRING];LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);GetClientRect(hWnd,&rect);switch (message){case WM_COMMAND:wmId = LOWORD(wParam);wmEvent = HIWORD(wParam);// Parse the menu selections:switch (wmId){case IDM_START:{CreateThread(NULL,1024,ThreadProc,ct,0,NULL);}break;case IDM_STARTAGAIN:{x=300;y=300;x_ball=319;y_ball=270;DrawMap();}break;case IDM_ABOUT:DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);break;case IDM_EXIT:DestroyWindow(hWnd);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}break;case WM_PAINT:{hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code here...HBITMAPm_bottom=(HBITMAP)LoadImage(NULL,"matlab.bmp",IMAGE_BITMAP,102 4,745,LR_LOADFROMFILE);HDC hDC=CreateCompatibleDC(hdc);SelectObject(hDC,m_bottom);BitBlt(hdc,0,0,1024,745,hDC,0,0,SRCCOPY);DeleteObject(m_bottom);DeleteDC(hDC);ReleaseDC(hWnd,hdc);EndPaint(hWnd, &ps);}break;case WM_KEYDOWN:{switch(wParam){case VK_LEFT:x-=30;if(x<=0)x=0;if(!isKeyDown)x_ball-=30;break;case VK_RIGHT:x+=30;if(x>=rect.right-128)x=rect.right-128;if(!isKeyDown)x_ball+=30;break;case VK_SPACE:isKeyDown=1;break;}}break;case WM_DESTROY:PostQuitMessage(0);KillTimer(hWnd,1);DeleteObject(m_ball);DeleteDC(ballDC);DeleteObject(m_bg);DeleteObject(m_board);DeleteDC(scDC);DeleteDC(boardDC);ReleaseDC(hWnd,bgDC);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0;}// Mesage handler for about box.LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){switch (message){case WM_INITDIALOG:return TRUE;case WM_COMMAND:if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL){EndDialog(hDlg, LOWORD(wParam));return TRUE;}break;}return FALSE;}四.游戏截图游戏截图(一)游戏截图(二)。
J2ME手机游戏设计之“打砖块”一、游戏分析:随着现代社会发展的日新月异,人们平常工作学习的竞争也在加大,生活的压力也跟着加重。
所以利用闲暇之余,大家都需要休闲娱乐工具,来放松自己,缓解压力,消除紧张的疲劳状态。
在这样的背景下,打砖块游戏作为一向休闲手机游戏应运而生,它不仅能舒缓我们的身心,而且能够开发我们的动手能力,提高自己的反应敏捷程度,充实自己。
二、游戏规则设计:BeatBrick:打砖块游戏的游戏规则为,每局有三个球,也就是有三条生命。
首先发球,球在击打砖块后弹回。
此时,须用球拍接住弹回的球并令其继续击打砖块。
否则,球会掉出屏幕。
直至球全部使用完或通全关则游戏结束。
游戏开始时,小球会停在挡板正中间,且此时挡板可左右自由移动,当按下空格键后小球会弹出,并在程序设定的区域内不停的碰撞反弹。
当小球碰到墙壁、挡板和砖块时,均会以相反的速度反弹,并且砖块被碰撞后会自动消失。
挡板可左右移动以接住小球,若小球没有被接住,超出指定区域下边缘,则程序会提示游戏结束,点击“确定”后退出程序。
当所有的砖块均消失时,程序会提示你赢了。
三、游戏实现:1、J2ME,即Java 2 Micro Edition,是SUN公司推出的在移动设备上运行的微型版Java平台,常见的移动设备有手机,PDA,电子词典,以及各式各样的信息终端如机顶盒等等。
最重要的移动终端当然是手机了,因此,我们主要讨论手机相关的J2ME规范。
J2ME是Sun公司为嵌入式开发所定义的一个框架,一系列标准的规范所组成。
所以J2ME是一个协议簇,而不是单一的规范。
2、游戏由6个类构成:GameScreen:GameScreen类表示游戏的核心,它包括对象的移动、图像的绘制以读取和响应用户的输人。
本游戏从MIDP LCDUI的低级Canvas类派生一个类来代表游戏的屏幕,后面将对这个类进行详细的说明。
BeatBrick:由MIDIet继承而来的应用程序类来代表这个应用程序。
突然发现,之前还有个JavaFX游戏开发第一课。
不过,这两个教程并不冲突。
目前这个系列是做一个完整的打砖块游戏。
第一课主要用到的知识有,JavaFX的动态绑定,Rectangle的使用,简单的MouseMove事件,BoxBlur特效。
那么,我们首先创建一个名叫BrickBlock的JavaFX Project。
本人是使用的e(fx)clipse 进行开发的。
e(fx)clipse的官方网站为:/,下载整合插件的eclipse即可。
首先创建一个游戏对象的基类BaseObject,继承于Parent。
import javafx.beans.property.DoubleProperty;import javafx.beans.property.SimpleDoubleProperty;import javafx.scene.Parent;/*** @author wing* @date 2012/7/26*/public abstract class BaseObject extends Parent{protected DoubleProperty widthProperty = new SimpleDoubleProperty(0);protected DoubleProperty heightProperty = new SimpleDoubleProperty(0);protected DoubleProperty xProperty = new SimpleDoubleProperty(0);protected DoubleProperty yProperty = new SimpleDoubleProperty(0);public DoubleProperty widthProperty() {return widthProperty;}public double getWidth(){return widthProperty.get();}public void setWidth(double width){this.widthProperty.set(width);}public DoubleProperty heightProperty() {return heightProperty;}public double getHeight(){return heightProperty.get();}public void setHeight(double height){this.heightProperty.set(height);}public DoubleProperty xProperty() {return xProperty;}public double getX(){return xProperty.get();}public void setX(double x){this.xProperty.set(x);}public DoubleProperty yProperty() {return yProperty;}public double getY(){return yProperty.get();}public void setY(double y){this.yProperty.set(y);}public void moveX(double x){this.xProperty.set(getX() + x);}public void moveY(double y){this.yProperty.set(getY() + y);}public boolean isCollisionWith(BaseObject baseObject){if(getX() + getWidth() > baseObject.getX() && getX() < baseObject.getX() + baseObject.getWidth() && getY() + getHeight() > baseObject.getY() && getY() < baseObject.getY() + baseObject.getHeight()){return true;}return false;}}可以看到,基类BaseObject中,包含有坐标和宽高的属性,并且还有一个检测碰撞的isCollisionWith方法。
编程实现的有趣打砖块游戏指南打砖块游戏(Brick breaker game)是一款经典的街机游戏,已经在电脑和手机上流行了很多年。
如果你对编程有一定了解,并想挑战自己创建一个有趣的游戏,那么本文将为你提供一个简单的指南,帮助你使用编程语言来实现一个打砖块游戏。
1. 游戏的基本原理在开始编写游戏之前,我们首先需要了解游戏的基本原理。
打砖块游戏的目标是使用一个移动的板挡住从顶部下落的小球,同时击碎顶部的砖块。
如果球碰到板或砖块,它会反弹。
当所有砖块都被击碎后,游戏胜利。
如果小球触底,游戏失败。
2. 游戏的基本元素打砖块游戏通常由以下几个基本元素组成:- 小球:代表游戏中的球体,会从顶部开始下落。
- 板:用于挡住小球,并反弹它。
- 砖块:顶部出现的多个砖块,玩家需要击碎它们。
- 壁:游戏区域的四周,小球碰到壁会反弹。
3. 编程语言的选择你可以根据自己的编程经验选择适合你的编程语言。
常用的语言如Python、JavaScript、C++等都可以完成该项目。
这里我们以Python为例进行讲解。
4. 游戏的实现在代码编写过程中,首先我们需要创建一个窗口来显示游戏,然后绘制出小球、板、砖块和壁等元素。
我们可以利用Python的图形库,如Pygame或Turtle来实现窗口和绘图的功能。
下面是一个简单的Python代码示例:```python# 导入所需的库import pygame# 初始化游戏pygame.init()# 创建游戏窗口screen = pygame.display.set_mode((800, 600))pygame.display.set_caption("打砖块游戏")# 游戏主循环running = Truewhile running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falsescreen.fill((0, 0, 0)) # 设置背景颜色为黑色pygame.display.flip()# 退出游戏pygame.quit()```在以上代码中,我们首先导入Pygame库,然后初始化游戏,并创建一个800x600像素的窗口。
Java实现经典游戏打砖块游戏的⽰例代码⽬录前⾔主要设计功能截图代码实现游戏核⼼类⼩球类砖块类总结前⾔《JAVA打砖块》游戏是⾃制的游戏。
玩家操作⼀根萤幕上⽔平的“棒⼦”,让⼀颗不断弹来弹去的“球”在撞击作为过关⽬标消去的“砖块”的途中不会落到萤幕底下。
主要设计设计游戏界⾯,⽤swing实现设计砖块,砖块类,设计⼩球,满屏乱跑的⼩球类,负责打碎砖块设计棒⼦,左右移动的⽊头板类球碰到砖块、棒⼦与底下以外的三边会反弹,落到底下会失去⼀颗球,把砖块全部消去就可以破关。
⼩球碰到砖块的回调算法设计⼩球碰到棒⼦的回调算法设计设计碰撞特效,⼀个负责显⽰爆炸效果的类设计⾳效类,碰撞时发出⾳效。
功能截图游戏开始:碰撞效果:代码实现游戏核⼼类public class BricksGame extends BaseGame {private ArrayList<GameObject> list = new ArrayList<>();private RenderTask render;private Random random = new Random();private boolean over;private Image memImage;private Graphics memG;// 双缓冲的画布public BricksGame(int width, int height, String title) throws HeadlessException {super(width, height, title);super.setBgColor(new Color(0x23,0x30,0x41));initGame(width, height);}private void initGame(int width, int height) {setFps(60);over=false;// 获取窗⼝的内外⼤⼩Container contentPane = this.getContentPane();Dimension size = contentPane.getSize();int contentWidth = size.width;int contentHeight = size.height;this.setContentWidth(contentWidth);this.setContentHeight(contentHeight);//System.out.println(contentPane instanceof JPanel);// true//System.out.println(size.width);//582//System.out.println(size.height);//403// 窗⼝四个⽅向的边界值Insets insets = getInsets();//System.out.println(insets.top);//System.out.println(insets.bottom);//System.out.println(insets.left);//System.out.println(insets.right);Scene env = new Scene(width, height, new Margin(insets.left, insets.right, insets.top, insets.bottom)); ImageIcon woodIcon = new ImageIcon("image/wood.png");int w = woodIcon.getIconWidth();int h = woodIcon.getIconHeight();Wood wood = new Wood(w, h, new Color(0x97, 0x5B, 0x12));wood.setScene(env);wood.setX(getWidth()/2 - w/2);wood.setY(getHeight()-50);wood.setImage(woodIcon.getImage());list.add(wood);Ball ball = new Ball(10, Color.WHITE);ImageIcon iconBall = new ImageIcon("image/ball2.png");ball.setImage(iconBall.getImage());ball.setScene(env);ball.setCoordinator(width / 2 - ball.getRadius(), wood.getY()-ball.getRadius()*2);ball.setWoodBar(wood);list.add(ball);Color[] colors = new Color[]{new Color(0xAA, 0xCF, 0x51),new Color(0xFC, 0xA9, 0x4B),new Color(0x73, 0xC7, 0xFF),Color.PINK,Color.GRAY};//ImageIcon brickIcon = new ImageIcon("image/brick_1.png");int brW = 60;int brH = 30;int start = 25;int brickX = start;int brickY = 100;int brickAreaWidth = getWidth() - start *2;int count = brickAreaWidth / brW - 1;int remainWidth = brickAreaWidth - count * brW;int intervalHort = brW + 3;start = brickX = (getWidth() - intervalHort * (count)) / 2;int intervalVert = brH + 3;HitBrick hitBrick = new HitBrick();for (int j = 0; j < 3; j++) {// brick linefor(int i = 0; i< count; i++){// brick columnsBrick brick = new Brick();brick.setColor(colors[i % colors.length]);//brick.setImage(brickIcon.getImage());brick.setWidth(brW);brick.setHeight(brH);brick.setX(brickX);brick.setY(brickY);brick.setBall(ball);brick.setHitListener(hitBrick);brickX += intervalHort;list.add(brick);}brickX = start;brickY += intervalVert;}// 双缓冲,在内存⾥⾯创建⼀个和窗⼝JFrame⼀样⼤⼩的ImagememImage = createImage(getWidth(), getHeight());memG = memImage.getGraphics();GameOver gameOver = new GameOver(memG);ball.setGameOverListener(gameOver);// 键盘事件的监听Input input = new Input();input.init();addKeyListener(input);// 重新渲染画⾯任务render = new RenderTask(this);render.start();addMouseListener(new MouseAdapter() {@Overridepublic void mousePressed(MouseEvent e) {//super.mousePressed(e);System.out.println(String.format("x:%d, y:%d", e.getX(), e.getY()));//x:218,y:305,w:164,h:45if ((e.getX() >= 218 && e.getX() <=(218+164))&& (e.getY() >= 305 && e.getY() <= (305+45))){render.setExitd(true);render = null;memImage = null;memG = null;removeKeyListener(input);removeMouseListener(this);list.clear();initGame(width, height);}}});}@Overridepublic void paint(Graphics g) {clear(memG);// 将画布清空为背景⾊for (int i = 0; i < list.size(); i++) {list.get(i).onTick();list.get(i).draw(memG);}if (list.size() == 2){// 只剩下⼩球和挡板,则通关成功!Wood wood = (Wood) list.get(0);wood.setX(getWidth()/2 - wood.getWidth()/2);wood.setY(getHeight()-50);Ball ball = (Ball) list.get(1);ball.setCoordinator(getWidth() / 2 - ball.getRadius(), /*bottom*/wood.getY()-ball.getRadius()*2); ball.setMoving(false);String gameOver = "恭喜通关!";memG.setFont(new Font("Serif", Font.BOLD, 35));int stringWidth = memG.getFontMetrics().stringWidth(gameOver);memG.setColor(Color.RED);memG.drawString(gameOver, getWidth()/2 - stringWidth/2, getHeight()/2);stopRenderTask();}if (over) {String gameOver = "Game Over";memG.setFont(new Font("Serif", Font.BOLD, 35));int stringWidth = memG.getFontMetrics().stringWidth(gameOver);memG.setColor(Color.WHITE);memG.drawString(gameOver, getWidth()/2 - stringWidth/2, getHeight()/2);String playAgain = "重新开始";stringWidth = memG.getFontMetrics().stringWidth(playAgain);int increase = 16;int fontSize = memG.getFont().getSize();int rectH = (int) (fontSize * 1.3);int rx=getWidth()/2 - stringWidth/2 - increase /2;int ry=getHeight() - fontSize * 4-(rectH-fontSize)/2;int rw = stringWidth + increase;int rh = rectH;//System.out.println(String.format("x:%d,y:%d,w:%d,h:%d", rx, ry, rw, rh));memG.drawRect(rx, ry, rw, rh);memG.setColor(new Color(33, 165, 230));memG.drawString(playAgain, getWidth()/2 - stringWidth/2, getHeight() - fontSize * 3 - 5); }// 将内存Image的内容复制到窗⼝上g.drawImage(memImage, 0, 0, null);// 耗性能的轮询判断,⼀个对象是否要消失for (int i = 2; i < list.size(); i++) {// 0,1位置是挡板和⼩球,不能消失GameObject gameObject = list.get(i);if (gameObject.isGone()) {list.remove(i);--i;}}}private void stopRenderTask() {new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}render.setExitd(true);}}).start();}public void exit(){//System.exit(1);}public void clear(Graphics g){if (g!=null) {g.setColor(getBgColor());g.fillRect(0, 0, getWidth(), getHeight());}}// ⼩球碰到砖块的回调class HitBrick implements Brick.HitListener{private ExecutorService executorService = Executors.newFixedThreadPool(3);private File file = new File("audio/brick2.wav");private AudioClip audio;public HitBrick() {try {audio = Applet.newAudioClip(file.toURI().toURL());} catch (MalformedURLException e) {e.printStackTrace();}}@Overridepublic void hit(Brick brick) {executorService.execute(new PlayHitAudioTask(audio));ExplodeObject eo = new ExplodeObject();eo.x = brick.x;eo.y = brick.y;eo.width = brick.width;eo.height = brick.height;eo.color = brick.color;list.add(eo);}}// 游戏结束内容的绘制class GameOver implements Ball.GameOverListener{private Graphics memG;public GameOver(Graphics g) {this.memG = g;}@Overridepublic void over() {over = true;}}}⼩球类public class Ball extends RectGameObject{private int radius;private int speed = 4;// ⼩球移动速度private boolean moving;// 是否在移动private boolean gameOver;// 是否overprivate boolean postv;// 初始⽔平⽅向的移动左右⽅向private int horiMove;// ⽔平移动距离(正负号代表移动⽅向)private int vertMove;// 垂直移动距离(正负号代表移动⽅向)private Wood woodBar;//⽊头板private Image image;private Point center = new Point();private GameOverListener l;public Ball(int radius, Color color){this.radius = radius;this.color = color;}@Overridepublic void draw(Graphics g) {g.setColor(color);//g.drawImage(image, x, y, null);g.fillOval(x, y, radius * 2, radius * 2);}@Overridepublic void onTick() {if (!moving){if(Input.getKeyDown(KeyEvent.VK_UP)){postv = (Math.random() * 10) <= 4 ? true : false;moving = true;gameOver = false;horiMove = postv ? speed : -speed;vertMove = -speed;} /*else if(Input.getKeyDown(KeyEvent.VK_LEFT)){Wood wb = woodBar;if (!wb.isReachEdge()) {transferBy(-wb.getSpeed(), 0);}} else if(Input.getKeyDown(KeyEvent.VK_RIGHT)){Wood wb = woodBar;if (!wb.isReachEdge()) {transferBy(wb.getSpeed(), 0);}}*/}if (moving){// arrive at left and right edgeScene scene = getScene();Margin margin = scene.in;if (x <= margin.left || x >= scene.width - margin.right - radius * 2){ horiMove = -horiMove;}// arrive at top edgeif (y <= margin.top && vertMove < 0){vertMove = -vertMove;}// ⼩球落在了挡板上if(getCenter().x >= woodBar.getX()&& getCenter().x <= woodBar.getX() + woodBar.getWidth() && Math.abs(getCenter().y - woodBar.y) <= radius&& vertMove > 0){vertMove = -vertMove;}// arrive at bottom edge// ⼩球落在了窗⼝的底部,停住⼩球 GAME OVERif (y >= scene.height - margin.bottom - radius * 2){moving = false;gameOver = true;if (l != null)l.over();return;}this.transferBy(horiMove, vertMove);}}public int getRadius() {return radius;}public void setRadius(int radius) {this.radius = radius;}public Wood getWoodBar() {return woodBar;}public void setWoodBar(Wood woodBar) {this.woodBar = woodBar;}public Image getImage() {return image;}public void setImage(Image image) {this.image = image;}public boolean isMoving() {return moving;}public void setMoving(boolean moving) {this.moving = moving;}public int getHoriMove() {return horiMove;}public void setHoriMove(int horiMove) {this.horiMove = horiMove;}public int getVertMove() {return vertMove;}public void setVertMove(int vertMove) {this.vertMove = vertMove;}@Overridepublic int getWidth() {return 2 * radius;}@Overridepublic int getHeight() {return getWidth();}public Point getCenter(){center.x = x + radius;center.y = y + radius;return center;}public boolean isGameOver() {return gameOver;}public void setGameOver(boolean gameOver) {this.gameOver = gameOver;}public GameOverListener getGameOverListener() {return l;}public void setGameOverListener(GameOverListener l) {this.l = l;}public interface GameOverListener{void over();}}砖块类package game;import java.awt.*;public class Brick extends RectGameObject {private Ball ball;private Point leftTop = new Point();private Point leftBottom = new Point();private Point rightTop = new Point();private Point rightBottom = new Point();public Brick(){}@Overridepublic void draw(Graphics g) {g.setColor(getColor());g.fillRect(x, y, getWidth(), getHeight());}@Overridepublic void onTick() {if (ball.isMoving()) {//start 碰撞检测/////////////////////////////////////////////boolean is = isSameQuadrant(ball.getCenter(), getLeftTop(), getRightBottom()); if (is) {int r = ball.getRadius();Point lt = getLeftTop();Point lb = getLeftBottom();Point rt = getRightTop();Point rb = getRightBottom();Point c = ball.getCenter();int dx1 = Math.abs(c.x - lt.x), dy1 = Math.abs(c.y - lt.y);int dx2 = Math.abs(c.x - lb.x), dy2 = Math.abs(c.y - lb.y);int dx3 = Math.abs(c.x - rt.x), dy3 = Math.abs(c.y - rt.y);int dx4 = Math.abs(c.x - rb.x), dy4 = Math.abs(c.y - rb.y);if(((dx1*dx1) + (dy1*dy1) <= r*r)||((dx2*dx2) + (dy2*dy2) <= r*r)||((dx3*dx3) + (dy3*dy3) <= r*r)||((dx4*dx4) + (dy4*dy4) <= r*r)){System.out.println("发⽣了碰撞");if (hitListener != null) {hitListener.hit(this);}setGone(true);}} else {Point c = ball.getCenter();int squareW = ball.getRadius() * 2;int squareH = squareW;int brcx = x + getWidth() / 2;int brcy = y + getHeight() / 2;if((Math.abs(c.x - brcx) <= (squareW + getWidth())*0.5)&&(Math.abs(c.y - brcy) <= (squareH + getHeight())*0.5)){System.out.println("......发⽣碰撞");if (hitListener != null) {hitListener.hit(this);}setGone(true);/* 击中砖块,改变⼩球的⽅向 */// 判断⼩球⾸先撞击的是砖块的左右还是上下侧,⾮常重要,否则出现不合理的移动⽅向。
打砖块1 窗体package com.game;import java.awt.*;import java.awt.event.*; import javax.swing.*;/*** 打砖块游戏* @author 孙沛林**/public class MyWindow {public static void main(String[] args) {JFrame f = new JFrame("打砖块");f.setSize(500,500);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);MyPanel mp = new MyPanel();f.add(mp);// 键盘监听mp.addKeyListener(mp);f.addKeyListener(mp);f.setVisible(true);}}2 面板package com.game;import java.awt.*;import java.awt.event.*;import javax.swing.*;/*** 游戏界面* @author 孙沛林**/public class MyPanel extends JPanel implements Runnable, KeyListener{Thread t = null; // 线程int speed = 8; // 暂停的毫秒数Ball ball; // 小球Pane pane; // 挡板int m = 8; // 横向砖块int n = 5; // 纵向砖块Block[][] blocks; // 砖块数组/*** 构造器*/public MyPanel(){ball = new Ball();// 生成小球pane = new Pane(); // 生成挡板blocks = new Block[m][n];// 生成砖块数组for(int i = 0; i<m; i++ ){for (int j = 0; j < n; j++) {blocks[i][j] = new Block(i,j);}}t = new Thread(this); // 产生线程t.start(); // 启动线程}/*** 绘图*/public void paint(Graphics g){g.clearRect(0, 0, 500, 500);// 边界g.setColor(Color.blue);g.drawRect(40, 40, 400, 400);// 挡板g.setColor(Color.gray);for(int i = 0; i<m; i++ ){for (int j = 0; j < n; j++) {if(blocks[i][j].visible){g.fillRoundRect(blocks[i][j].x, blocks[i][j].y, blocks[i][j].w, blocks[i][j].h, blocks[i][j].r, blocks[i][j].r);}}}// 小球g.setColor(Color.black);g.fillOval(ball.x, ball.y, ball.d, ball.d);// 挡板g.setColor(Color.orange);g.fillRoundRect(pane.x, pane.y, pane.w, pane.h, pane.r, pane.r);}/*** 运行*/public void run() {while(true){ball.move();// 小球移动一步for(int i = 0; i<m; i++ ){for (int j = 0; j < n; j++) {if(blocks[i][j].visible){blocks[i][j].crash(ball);// 碰撞判断+处理}}}repaint();try {t.sleep(speed);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/*** 按下键盘*/public void keyPressed(KeyEvent e) {if(e.getKeyCode() == KeyEvent.VK_LEFT){pane.left();}if(e.getKeyCode() == KeyEvent.VK_RIGHT){pane.right();}}public void keyReleased(KeyEvent arg0) {// TODO Auto-generated method stub}public void keyTyped(KeyEvent arg0) {// TODO Auto-generated method stub}}3 小球package com.game;/***小球类*@author孙沛林**/public class Ball {/***属性*/int x =100;int y =200;int dx = 1;int dy = 1;int d = 10; // 直径/* 容器的参数 */int xContainer = 40;int yContainer = 40;int width = 400;int height = 400;/***构造器*/public Ball(){}/***移动一步*/public void move(){x += dx;y += dy;this.wall();}/***碰四周边界反弹*/public void wall(){if(x < this.xContainer || x > this.xContainer+this.width - d ){dx *= -1;}if(y < this.yContainer || y > this.yContainer+this.height - d ){ dy *= -1;}}}4 挡板package com.game;/***挡板类-圆角矩形*@author孙沛林**/public class Pane {int x = 200;int y = 440;int dx = 4;int w = 80; // 自宽int h = 10; // 自高int r = 5; // 圆角弧度/* 容器的参数 */int xContainer = 40; // 容器起点横坐标int width = 400; // 容器宽度/***构造器*/public Pane(){}/***←移动*/public void left(){if(x>this.xContainer){x -= dx;}}/***→移动*/public void right(){if(x<this.xContainer + this.width - this.w){ x += dx;}}}5 砖块package com.game;/***砖块类-圆角矩形*@author孙沛林**/public class Block {int x;int y;int w = 49;int h = 20;int r = 5;int xContainer = 40; // 容器起点横坐标boolean visible = true;/***构造器*/public Block(){}/***构造器*@param x*@param y*/public Block(int x, int y){this.x = x*(w+1) + xContainer + 1;this.y = y*(h+1) + xContainer + 1;}/***计算小球是否接触的砖块*如果接触了左右两边,那么改变小球的dx方向*如果接触了上下两边,那么改变小球的dy方向*同时,本砖块消失*@param ball小球*/public void crash(Ball ball){if(ball.y + ball.d > this.y && ball.y < this.y + this.h// 纵向区间&& (ball.x + ball.d == this.x || ball.x == this.x + this.w ) // 横向区间){ball.dx *= -1;this.visible = false;}if(ball.x + ball.d > this.x && ball.x < this.x + this.w// 纵向区间&& (ball.y+ ball.d== this.y|| ball.y== this.y+ this.h) // 横向区间){ball.dy *= -1;this.visible = false;}}}。
Imports System.Drawing.Drawing2DPublic Class Form1Public shuiping As Integer = 1Public chuizhi As Integer = 1Public WithEvents board As New ButtonPublic buttonList1 As New ArrayList()Public ball As New LabelPublic num As IntegerPublic canDrag As Boolean = FalseDim x As IntegerDim i As Integer = 0'界面布置Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.Text = "张少霞制作2010242499"'一些控制按钮Button1.Left = 650Button1.Top = 590Button1.Text = "开始游戏"Button2.Left = 750Button2.Top = 590Button2.Text = "暂停游戏"Button3.Left = 650Button3.Top = 625Button3.Text = "分数"Button4.Left = 750Button4.Top = 625Button4.Text = ""GroupBox1.Left = 330GroupBox1.Top = 590'添加小球ball.BackColor = System.Drawing.Color.Transparentball.ForeColor = System.Drawing.Color.Redball.Text = "●"ball.TextAlign = System.Drawing.ContentAlignment.MiddleCenterball.Width = 14ball.Height = 14ball.Left = 645ball.Top = 539Me.Controls.Add(ball)'添加挡板board.Width = 100board.Height = 7board.Left = 600 '330board.Top = 553board.BackColor = Color.GrayMe.Controls.Add(board)Button4.Text = 0End SubPrivate Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick '小球的初始状态ball.Left = ball.Left + shuiping * 7ball.Top = ball.Top + chuizhi * 7Dim a As Integera = ball.Left + 7Dim b As Integerb = ball.Top + 7Button4.Text = 66 - num'判断小球是否和砖块碰撞For Each ctl In buttonList1If ((a - 7 >= ctl.Left And a - 7 <= ctl.Right) Or (a + 7 >= ctl.Left And a + 7 <= ctl.Right)) And (b >= ctl.Top And b <= ctl.Bottom) Thenshuiping = -shuipingMe.Controls.Remove(ctl)num = num - 1Exit ForElseIf ((b - 7 >= ctl.Top And b - 7 <= ctl.Bottom) Or (b + 7 >= ctl.Top And b + 7 <= ctl.Bottom)) And (a >= ctl.Left And a <= ctl.Right) Thenchuizhi = -chuizhiMe.Controls.Remove(ctl)num = num - 1Exit ForEnd IfNext'判断小球是否和边界碰撞If (a - 7 <= Me.Left And a + 7 >= Me.Left) Or (a + 7 >= Me.ClientSize.Width And a - 7 <= Me.ClientSize.Width) Thenshuiping = -shuipingEnd If'判断小球是否和挡板碰撞If ((b + 7 >= board.Top And b + 7 <= board.Bottom) And (a >= board.Left And a <= board.Right)) Or (b - 7 <= 0 And b + 7 >= 0) Thenchuizhi = -chuizhiEnd If'当挡板没有借住小球时予以提示(三次机会)If ((board.Top <= b + 7 And b + 7 <= board.Top + board.Height) And (a + 7 <= board.Left Or a - 7 >= board.Right)) ThenIf i < 3 Thenball.Left = 645ball.Top = 538board.Left = 600board.Top = 553Timer1.Enabled = FalseMessageBox.Show("亲,还有" & (3 - i) & "次机会!")Button1.Enabled = TrueEnd IfIf i = 3 ThenMessageBox.Show("Game over!")Button1.Enabled = FalseEnd IfEnd If'砖块全部打完时予以提示If (num = 0) ThenMessageBox.Show("胜利!")Button1.Enabled = FalseEnd IfEnd Sub'控制开始和播放以音乐按钮Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Timer1.Enabled = TrueIf Timer1.Enabled = True Thenputer.Audio.Play("Dreamtale-The Dawn - 铃声.wav", AudioPlayMode.Background)End IfEnd Sub'暂停游戏和音乐按钮Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Timer1.Enabled = Not Timer1.EnabledIf (Timer1.Enabled = False) ThenButton2.Text = "继续游戏"puter.Audio.Stop()ElseButton2.Text = "暂停游戏"puter.Audio.Play("Dreamtale-The Dawn - 铃声.wav", AudioPlayMode.Background)End IfEnd Sub'游戏者选择砖块的排列模式Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChangedFor Each ctl In buttonList1Me.Controls.Remove(ctl)num = 0NextIf Timer1.Enabled = True Thenputer.Audio.Play("Dreamtale-The Dawn - 铃声.wav", AudioPlayMode.Background)End IfButton4.Text = 0ball.Left = 645ball.Top = 538board.Left = 600board.Top = 553Button1.Enabled = TrueTimer1.Enabled = False'添加砖块Dim i As IntegerDim j As IntegerFor i = 0 To 5For j = 0 To 10Dim button As New Buttonbutton.Height = 25If j Mod 2 = 0 And i Mod 2 = 0 Thenbutton.Width = 100button.BackColor = Color.CadetBlueElseIf j Mod 2 = 0 And i Mod 2.0 = 1 Thenbutton.Width = 80button.BackColor = Color.CornsilkElseIf j Mod 2.0 = 1 And i Mod 2.0 = 1 Thenbutton.Width = 60button.BackColor = Color.PinkElsebutton.Width = 40button.BackColor = Color.CadetBlueEnd Ifbutton.Top = (i + 1) * 40button.Left = (j + 1) * 110Me.Controls.Add(button)buttonList1.Add(button)num = num + 1NextEnd Sub'模式二Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChangedFor Each ctl In buttonList1Me.Controls.Remove(ctl)num = 0NextIf Timer1.Enabled = True Thenputer.Audio.Play("Dreamtale-The Dawn - 铃声.wav", AudioPlayMode.Background)End IfButton4.Text = 0ball.Left = 645ball.Top = 538board.Left = 600board.Top = 553Button1.Enabled = TrueTimer1.Enabled = False'添加砖块Dim i As IntegerDim j As IntegerFor i = 0 To 10For j = 0 To iDim button As New Buttonnum = num + 1button.Width = 50button.Height = 30button.Left = 600 - i * 35 + j * 70button.Top = i * 30 + 20button.BackColor = Color.FromArgb(Color.Pink.ToArgb + j * 200) 'Button1.BackColor.ToArgb + i * 200 ' Color.CadetBlue + j * 100Me.Controls.Add(button)buttonList1.Add(button)NextNextEnd Sub'模式三Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChangedFor Each ctl In buttonList1Me.Controls.Remove(ctl)num = 0If Timer1.Enabled = True Thenputer.Audio.Play("Dreamtale-The Dawn - 铃声.wav", AudioPlayMode.Background)End IfButton4.Text = 0ball.Left = 645ball.Top = 538board.Left = 600board.Top = 553Button1.Enabled = TrueTimer1.Enabled = False'添加砖块Dim i As IntegerDim j As IntegerFor i = 0 To 5For j = 0 To 10Dim button As New Button()num = num + 1button.Width = 50button.Height = 30button.Left = 380 - i * 25 + j * 70button.Top = 80 + i * 30 + 20Dim k As Integerk = Int(21 * Rnd()) - 10button.BackColor = Color.FromArgb(Color.CadetBlue.ToArgb + k * 100) 'Button1.BackColor.ToArgb + i * 200 ' Color.CadetBlue + j * 100Me.Controls.Add(button)buttonList1.Add(button)NextNextEnd SubPrivate Sub board_MouseDown(sender As Object, e As MouseEventArgs) Handles board.MouseDowncanDrag = TrueDim p As New Point()p = Me.PointToClient(MousePosition)x = p.X - board.LeftEnd SubPrivate Sub board_MouseMove(sender As Object, e As MouseEventArgs) Handles board.MouseMoveIf canDrag = True And Timer1.Enabled = True ThenDim p As New Point()p = Me.PointToClient(MousePosition)board.Left = p.X - xEnd IfEnd SubPrivate Sub board_MouseUp(sender As Object, e As MouseEventArgs) canDrag = FalseEnd SubEnd Class。
javascript实现打砖块⼩游戏(附完整源码)⼩时候玩⼀天的打砖块⼩游戏,附完整源码在?给个赞?实现如图需求分析1、⼩球在触碰到⼤盒⼦上、左、右边框,以及滑块后沿另⼀⽅向反弹,在碰到底边框后游戏结束;2、⼩球在触碰到⽅块之后,⽅块消失;3、消除所有⽅块获得游戏胜利;4、可通过⿏标与键盘两种⽅式移动滑块;5、游戏难度可调整,实时显⽰得分。
代码分析1、html结构:左右两个提⽰框盒⼦分别⽤⼀个div,在其中添加需要的内容;中间主体部分⽤⼀个div,⾥⾯包含⼀个滑块(slider),⼀个⼩球(ball),以及⼀个装有所有⽅块的brick盒⼦,我们通过使⽤js在brick中动态添加⽅块,这样⼤⼤减少了div的数量。
简化了html结构。
2、css样式:通过使⽤position:relative/absolute/fixed,完成对整个页⾯的布局;3、js⾏为:⾸先对于⼩球的运动,我们通过使⽤setInterval定时器进⾏实现;⼩球与滑块以及⽅块之间的碰撞,我们通过碰撞检测函数进⾏实现;滑块的移动我们需要设置两种⽅法,通过⿏标实现可以使⽤拖拽;通过键盘实现使⽤键盘事件。
⼀些封装的函数动态创建⽅块函数分析1、⾸先,我们在id=“brick”的div盒⼦中动态插⼊n个⽅块,在css中预先为这些盒⼦设置了固定的宽⾼,并设置了左浮动布局。
这样,所有的⽅块就会按照⾃左到右⾃上⽽下排列在盒⼦中;但是通过浮动布局在中间某⼀个⽅块碰撞消失后,后⾯的⽅块会补充上来,这并不是我们想要的;2、为了防⽌后⾯的⽅块向前移动,显然我们不能使⽤浮动,在这⾥我们对每⼀个⽅块使⽤绝对定位;3、在给每⼀个⽅块进⾏绝对定位之前,我们先要获取它们当前所在位置的left与top值,并赋给它们,否则⽅块将全部挤在⼀个格⼦⾥;4、最后再给每个⽅块进⾏绝对定位。
function createBrick(n){var oBrick = document.getElementById("brick")//在⼤盒⼦brick中插⼊n个div⽅块,并给予随机颜⾊for(var i = 0; i<n; i++){var node = document.createElement("div");node.style.backgroundColor= color();oBrick.appendChild(node);}//获取所有的⽅块var brickArr = obrick.getElementsByTagName("div")//根据每个⽅块当前所在位置,将left与top值赋给⽅块for(var i=0;i<brickArr.length;i++){brickArr[i].style.left = brickArr[i].offsetLeft+"px";brickArr[i].style.top = brickArr[i].offsetTop+"px";}//将所有⽅块设置成绝对定位,注意这⼀步与上⼀步顺序不能调换for(var i =0;i<brickArr.length;i++){brickArr[i].style.position="absolute";}}碰撞检测函数代码分析见上⼀篇function knock(node1,node2){var l1 = node1.offsetLeft;var r1 = node1.offsetLeft + node1.offsetWidth;var t1 = node1.offsetTop;var b1 = node1.offsetTop+node1.offsetHeight;var l2 = node2.offsetLeft;var r2 = node2.offsetLeft + node2.offsetWidth;var t2 = node2.offsetTop;var b2 = node2.offsetTop+node2.offsetHeight;if(l2>r1||r2<l1||t2>b1||b2<t1){return false;}else{return true;}}完整代码(复制可⽤)<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>打砖块⼩游戏</title><style>#box{width: 600px;height: 650px;border: 5px solid rgb(168, 139, 8);position: relative;left: 500px;background:linear-gradient(rgb(19, 192, 120),rgb(47, 32, 114));}#ball{width: 20px;height: 20px;border-radius: 10px;background-color: white;position: absolute;top: 560px;box-shadow: 0px 0px 3px 3px aqua;}#btn{width: 150px;height: 90px;position: fixed;left: 730px;top: 350px;border-radius: 10px;font-size: 24px;cursor: pointer;}#slider{width: 120px;height: 20px;background-color: rgb(168, 139, 8);position: absolute; top: 585px;border-radius: 10px;box-shadow: 0px 0px 2px 2px red;cursor: pointer; }#brick div{width: 98px;height: 20px;float: left;border: 1px solid black;}#tip{width: 280px;position:fixed;top: 100px;left: 150px;border: 1px solid black;text-indent: 2em;padding: 10px;border-radius: 20px; }#grade{width: 180px;position:fixed;top: 100px;left: 1150px;border: 1px solid black;text-indent: 2em;padding: 10px;border-radius: 20px; }#grade h3{font-weight: 500;}</style></head><body><div id="box"><div id="ball"></div><div id="slider"></div><div id="brick"></div></div><div id="tip"><h1>提⽰</h1><p>点击按钮开始游戏</p><p>⽅法1:使⽤⿏标按住滑块,左右拖动,反弹⼩球</p><p>⽅法2:使⽤键盘“左”“右”⽅向键控制滑块移动,按住快速移动</p><p>⼩球触碰到底部为失败</p><p>清空所有⽅块游戏胜利</p></div><div id="grade"><h3>游戏强度:</h3><h2>XXX</h2><h3>得分:</h3><h1>00</h1></div><button id="btn">开始游戏</button><script>var box = document.getElementById("box");var ball = document.getElementById("ball");var btn = document.getElementById("btn");var slider = document.getElementById("slider")var obrick = document.getElementById("brick")var brickArr = obrick.getElementsByTagName("div")var grade = document.getElementById("grade")var rank = grade.children[1]var score = grade.children[3]var sco = 0;var timer;var isRunning = false;var speedX = rand(3,12);var speedY = -rand(3,12);var num =speedX-speedY;console.log(num)switch(num){case 6:case 7:case 8:rank.innerHTML="简单";break;case 9:case 10:case 11:rank.innerHTML="⼀般";break;case 12:case 13:case 14:rank.innerHTML="中等";break;case 15:case 16:case 17:rank.innerHTML="难"break;case 18:case 19:case 20:rank.innerHTML="很难"slider.style.width = 100+"px";break;case 21:case 22:rank.innerHTML="特别难"slider.style.width = 80+"px";break;case 23:case 24:rank.innerHTML="哭了"slider.style.width = 60+"px";break;}//随机⽣成⼩球与滑块位置var beginGo = rand(100,500)ball.style.left = beginGo +40 +"px"slider.style.left = beginGo +"px"//开始按钮点击事件btn.onclick = function(){btn.style.display="none";isRunning = true;clearInterval(timer);timer = setInterval(function(){//获取⼩球初始位置var ballLeft = ball.offsetLeft;var ballTop = ball.offsetTop;//获取⼩球运动之后位置var nextleft = ballLeft + speedX;var nexttop = ballTop + speedY;//⽔平边界判断,当⼩球的left值⼩于容器左边界或者⼤于容器右边界时,将⽔平⽅向速度取反 if(nextleft<=0||nextleft>=box.offsetWidth-ball.offsetWidth-10){speedX=-speedX;}//垂直边界判断,当⼩球的top值⼩于容器上边界时,将垂直⽅向速度取反if(nexttop<=0){speedY=-speedY;}//当⼩球触碰到下边界时,提⽰“游戏失败”,重新刷新页⾯if(nexttop>box.offsetHeight-ball.offsetHeight){location.reload();alert("You were dead!")}//将运动后的位置重新赋值给⼩球ball.style.left = nextleft+"px";ball.style.top= nexttop+"px";//⼩球与滑块的碰撞检测if(knock(ball,slider)){speedY=-speedY;}//⼩球与⽅块的碰撞检测for(var j=0; j<brickArr.length;j++){if(knock(brickArr[j],ball)){speedY=-speedYobrick.removeChild(brickArr[j]);sco++;score.innerHTML=sco;break;}}//当容器中⽅块数量为0时,宣布“游戏胜利”,刷新页⾯if(brickArr.length<=0){location.reload();alert("You win!")}},20)}//⿏标控制滑块slider.onmousedown = function(e){var e = e||window.event;//获取滑块初始位置var offsetX = e.clientX - slider.offsetLeft;if(isRunning){document.onmousemove = function(e){var e = e||window.event;var l =e.clientX-offsetX;if(l<=0){l=0;}if(l>=box.offsetWidth-slider.offsetWidth-10){l=box.offsetWidth-slider.offsetWidth-10 ;}slider.style.left = l + "px";}}}document.onmouseup = function(){document.onmousemove = null;}//按键控制滑块document.onkeydown = function(e){var e = e||window.event;var code = e.keyCode || e.which;var offsetX = slider.offsetLeft;if(isRunning){switch(code){case 37:if(offsetX<=0){slider.style.left=0break;}slider.style.left = offsetX*4/5 + "px";break;case 39:if(offsetX>=box.offsetWidth-slider.offsetWidth-10){slider.style.left=box.offsetWidth-slider.offsetWidth;break;}slider.style.left = (box.offsetWidth-slider.offsetWidth-offsetX)/5 +offsetX + "px";break;}}}createBrick(72)//容器内创建⽅块function createBrick(n){var oBrick = document.getElementById("brick")//在⼤盒⼦brick中插⼊n个div⽅块,并给予随机颜⾊for(var i = 0; i<n; i++){var node = document.createElement("div");node.style.backgroundColor= color();oBrick.appendChild(node);}//获取所有的⽅块var brickArr = obrick.getElementsByTagName("div")//根据每个⽅块当前所在位置,将left与top值赋给⽅块for(var i=0;i<brickArr.length;i++){brickArr[i].style.left = brickArr[i].offsetLeft+"px";brickArr[i].style.top = brickArr[i].offsetTop+"px";}//将所有⽅块设置成绝对定位,注意这⼀步与上⼀步顺序不能调换for(var i =0;i<brickArr.length;i++){brickArr[i].style.position="absolute";}}//随机⽣成颜⾊function color(){var result= "#";for(var i=0;i<6;i++){result += rand(0,15).toString(16)// 把⼗进制的数字变成⼗六进制的字符串:0 1 ...9 a b c d f}return result;}//随机数⽣成function rand(n,m){return n+parseInt(Math.random()*(m-n+1));}//碰撞检测函数function knock(node1,node2){var l1 = node1.offsetLeft;var r1 = node1.offsetLeft + node1.offsetWidth;var t1 = node1.offsetTop;var b1 = node1.offsetTop+node1.offsetHeight;var l2 = node2.offsetLeft;var r2 = node2.offsetLeft + node2.offsetWidth;var t2 = node2.offsetTop;var b2 = node2.offsetTop+node2.offsetHeight;if(l2>r1||r2<l1||t2>b1||b2<t1){return false;}else{return true;}}</script></body></html>以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
不多说,,直接可以拷贝下面的东西,然后记得把那个BLOCK的名字改成你自己的类名,这个很关键哦,不然是错的可别怪我,呵呵~~import java.awt.*。
import java.awt.event.*。
import javax.swing.*。
import java.applet.*。
import ng.String.*。
import ng.*。
import java.io.*。
publicclass Block extends JPanel implements ActionListener,KeyListener//应该是继承JPanel{static Button but[] = new Button[6]。
static Button noStop = new Button("取消暂停">。
static Label scoreLab = new Label("分数:">。
static Label infoLab = new Label("提示:">。
static Label speedLab = new Label("级数:">。
static Label scoreTex = new Label("0">。
static Label infoTex = new Label(" ">。
static Label speedTex = new Label("1">。
static JFrame jf = new JFrame(>。
static MyTimer timer。
static ImageIcon icon=new ImageIcon("resource/Block.jpg">。
#include<graphics.h>#include<conio.h>#include<windows.h>#include<stdlib.h>#include<stdio.h>void draw(int x1){int m=0,n=0,x=40,y=20;int t=0,i,j,k=1;int a=0,b=127,c=88;for(i=0;i<8;i++){n=i*20;y=n+20;for(j=0;j<16-t;j++){m=(j+i)*40;x=m+40;POINT points[]={{m,n}, {x,n}, {x,y}, {m,y}}; setfillcolor(RGB(a,b,c));fillpolygon(points, 4);a=8*k+8;b=j*3+k+200;c=20*k;k=k+8;}t=t+2;k=1;}m=300;n=160;x=340;y=180;POINT points[]={{m,n}, {x,n}, {x,y}, {m,y}}; setfillcolor(RGB(a,b,c));fillpolygon(points, 4);}void end(){settextstyle(50, 0, "宋体", 0, 0, FW_BOLD, false, false, false);settextcolor(RED);outtextxy(200,200,"游戏结束!");getch();closegraph();}void start(){int n=0;loadimage(NULL,"IMSGE","background"); while(1){setbkmode(TRANSPARENT);settextstyle(50, 0, "楷体", 0, 0, FW_BOLD, false, false, false);if(n%2==0)settextcolor(GREEN);elsesettextcolor(RED);outtextxy(80,100,"请按空格键开始游戏"); Sleep(250);if(_kbhit())break;n++;}}void main(){int x=320,y=430,m=270,n=370; int a,b,c,e=0,f=0,g=0,k[3];int i,j;char d;initgraph(640,480);start();setbkcolor(WHITE); cleardevice();setlinecolor(WHITE);draw(1);while(1){setlinecolor(LIGHTBLUE); setlinestyle(PS_SOLID, 10); line(m,445,n,445); setlinestyle(PS_SOLID, 0); setlinecolor(WHITE); setfillcolor(BROWN); fillcircle(x,y,10);Sleep(60);setfillcolor(WHITE); fillcircle(x,y,10);if(_kbhit()){d=getch();setlinecolor(WHITE); setlinestyle(PS_SOLID, 10); line(m,445,n,445);if(n<640&&d=='d'){m=m+27;n=n+27;}else if(m>0&&d=='a'){m=m-27;n=n-27;}}//确定x,y的值// if(y>=435)end();if(y==430&&x>=m-5&&x<=n+5) {if(x>m&&x<=m+25){a=0;b=2;c=2;if(10/(x-m)==0)k[0]=2;else if(10/(x-m)>3)k[0]=3;elsek[0]=10/(x-m);}else if(x>m+25&&x<=m+55) {a=2;b=0;c=2;if(15/(x-m-25)==0)k[1]=2;else if(15/(x-m-25)>3)k[1]=3;elsek[1]=10/(x-m-25);}else if(x>m+55&&x<=n) {a=2;b=2;c=0;if(10/(x-m-55)==0)k[2]=2;else if(10/(x-m-55)>3)k[2]=3;elsek[2]=10/(x-m-55);}}if(1){if(e==0){y=y-10;if(y<=10){g=1-g;e=1;}if(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){e=1;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}else if(e==1){y=y+10;if(getpixel(x-1,y-14)!=WHITE||getpixel(x-1,y+14)! =WHITE||getpixel(x+1,y-14)!=WHITE||getpixel(x +1,y+14)!=WHITE){e=0;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}else if(y==430&&x>=m-5&&x<=n+5)e=0;}if(g==0&&a==0){x=x-k[0]*5;if(y<=10)g=1-g;else if(x<=10||x>=630)g=1;if(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=1;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}else if(g==1&&a==0){x=x+k[0]*5;if(y<=10)g=1-g;else if(x<=10||x>=630)g=0;elseif(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=0;setfillcolor(WHITE);for(i=10;i<=18;i++){for(j=10;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}if(g==0&&b==0){x=x-k[1]*2;if(y<=10)g=1-g;else if(x<=10||x>=630)g=1;elseif(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=1;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}else if(g==1&&b==0){x=x+k[1]*2;if(y<=10)g=1-g;else if(x<=10||x>=630)g=0;elseif(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)!=WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=0;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}if(g==0&&c==0){x=x-k[2]*5;if(y<=10)g=1-g;else if(x<=10||x>=630)g=1;elseif(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=1;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}else if(g==1&&c==0)x=x+k[2]*5;if(x<=10||x>=630)g=0;else if(y<=10)g=1-g;elseif(getpixel(x-1,y-10)!=WHITE||getpixel(x-1,y+10)! =WHITE||getpixel(x+1,y-10)!=WHITE||getpixel(x +1,y+10)!=WHITE){g=0;setfillcolor(WHITE);for(i=8;i<=18;i++){for(j=8;j<=18;j++){floodfill(x-i,y-j,WHITE);floodfill(x+i,y-j,WHITE);floodfill(x-i,y+j,WHITE);floodfill(x+i,y+j,WHITE);}}}}} end(); }。