打砖块JAVA游戏代码
用Lua编写一个简易的打砖块游戏

用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课程设计打砖块

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编程经典小游戏设计-打砖块小游戏源码

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();}}}。
打砖块JAVA游戏代码

#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课程设计打砖块(含代码)

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已经广泛的应用于各个领域,包括医药,汽车工业,手机行业,游戏,等等地方。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#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(); }。