小狗踢足球

来源:互联网 发布:python list转字典 编辑:程序博客网 时间:2024/04/27 15:24

马里奥是左哥教我们做的线程游戏,让我们认识线程游戏的做法,处理游戏中的元素,和一系列的原理,让我们从程序的角度去理解游戏。

然后让我们自己去做一个线程游戏出来,我自己做的游戏是小狗踢足球。

马里奥的代码我就不发了,发我自己做的小狗踢足球。

package 足球小狗;import java.awt.Graphics;import java.awt.Image;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.image.BufferedImage;import java.util.ArrayList;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JLabel;import javax.swing.JPanel;public class GamePanel extends JPanel{public ImageIcon image = new ImageIcon("image/Dog.png");public FootBall footBall;public Ball ball;public BlackDog blackDog;public WhiteDog whiteDog;public static ArrayList<BlackDog> list = new ArrayList<BlackDog>();public boolean flagReady = true;public ImageIcon ready = new ImageIcon("runimages/READY.png");public boolean flag = true;public ImageIcon icon = new ImageIcon("image/开始界面.png");public GamePanel(FootBall footBall){ball = new Ball();whiteDog = new WhiteDog(this,ball);blackDog = new BlackDog(this,ball,whiteDog);blackDog.start();whiteDog.start();this.footBall = footBall;initPanel();}public void initPanel(){KeyAdapter blackka = new KeyAdapter(){public void keyPressed(KeyEvent e) {int code = e.getKeyCode();if(code == 68){blackDog.right = true ;}else if(code == 65){blackDog.left = true;}else if(code == 87){blackDog.up = true;}else if(code == 83){blackDog.down = true;}if(code == 39){whiteDog.right = true ;}else if(code == 37){whiteDog.left = true;}else if(code == 38){whiteDog.up = true;}else if(code == 40){whiteDog.down = true;}} public void keyReleased(KeyEvent e) {int code = e.getKeyCode();if(code == 68){blackDog.right = false ;}else if(code == 65){blackDog.left = false;}else if(code == 87){blackDog.up = false;}else if(code == 83){blackDog.down = false;}if(code == 39){whiteDog.right = false ;}else if(code == 37){whiteDog.left = false;}else if(code == 38){whiteDog.up = false;}else if(code == 40){whiteDog.down = false;}}};footBall.addKeyListener(blackka);}public void paint(Graphics g) {BufferedImage bi = (BufferedImage)createImage(this.getWidth(),this.getHeight());Graphics graphics = bi.getGraphics();graphics.drawImage(image.getImage(), 0, 0, null);graphics.drawImage(ball.getImage().getImage(), ball.getX(), ball.getY(), null);graphics.drawImage(blackDog.getBlackDog(), blackDog.getX(), blackDog.getY(), null);graphics.drawImage(whiteDog.getWhiteDog(), whiteDog.getX(), whiteDog.getY(), null);g.drawImage(bi, 0, 0, null);}}
这是游戏面板类

下面是黑狗类,白狗类和黑狗类差不多,就不发了。

package 足球小狗;import java.awt.Graphics;import java.awt.Image;import java.awt.Rectangle;import javax.swing.ImageIcon;public class BlackDog extends Thread{public Image image1 = new ImageIcon("runimages\\小黑狗奔跑22.png").getImage();// 黑狗图public int x = 250,y = 230;public int width = 50,height = 50;public int speed = 2;public boolean up = false,down = false,left = false,right = false;public GamePanel gp;public Ball ball;public int imageFlag = 0;public WhiteDog whiteDog;public boolean flagpeng = true;public Graphics g;public Image image2 = new ImageIcon("runimages\\GOAL.png").getImage();public BlackDog(int x,int y,GamePanel gp,Ball ball){this.x = x;this.y = y;this.gp = gp;this.ball = ball;}public BlackDog(GamePanel gp,Ball ball,WhiteDog whiteDog){this.gp = gp;this.ball = ball;this.whiteDog = whiteDog;}public void run() {while(true){new Thread(){public void run() {boolean pointflag = ball.ballIn();if(pointflag){//黑狗或者白狗得分复位ball.x = 350;ball.y = 248;x = 250;y = 230;whiteDog.x = 422;whiteDog.y = 230;Image image1 = new ImageIcon("image/小白狗向左.png").getImage();whiteDog.setWhiteDogImage(image1);Image image2 = new ImageIcon("runimages\\小黑狗奔跑22.png").getImage();setWhiteDogImage(image2);}};}.start();new Thread(){//判断小球和狗碰撞(左右碰撞)public void run() {Rectangle dogRect = new Rectangle(x,y,width,height);Rectangle ballRect = new Rectangle(ball.x,ball.y,ball.width,ball.height);//小狗在球左方if(dogRect.intersects(ballRect)&&dogRect.x<ballRect.x){for(int i=0;i<50;i++){if(ball.x<644&&ball.x>30){ball.x += speed;}try {sleep(i);gp.repaint();} catch (InterruptedException e) {e.printStackTrace();}}//小狗在球右方}else if(dogRect.intersects(ballRect)&&dogRect.x>ballRect.x){for(int i=0;i<50;i++){if(ball.x<644&&ball.x>30){ball.x -= speed;}try {sleep(i);gp.repaint();} catch (InterruptedException e) {e.printStackTrace();}}}};}.start();//判断小狗足球左下右上斜碰撞new Thread(){public void run() {Rectangle dogRect = new Rectangle(x,y,width,height);Rectangle ballRect = new Rectangle(ball.x,ball.y,ball.width,ball.height);//小狗在球左边实现右上if(dogRect.intersects(ballRect)&&dogRect.x<ballRect.x&&dogRect.y>ballRect.y){for(int i=0;i<40;i++){if(ball.x<644&&ball.x>30){ball.x += speed;}if(ball.y>75&&ball.y<435){ball.y -= speed;}try {sleep(i);gp.repaint();} catch (InterruptedException e) {e.printStackTrace();}}//小狗在球右边实现左下}else if(dogRect.intersects(ballRect)&&dogRect.x>ballRect.x&&dogRect.y<ballRect.y){for(int i=0;i<40;i++){if(ball.x<644&&ball.x>30){ball.x -= speed;}if(ball.y>75&&ball.y<435){ball.y += speed;}try {sleep(i);gp.repaint();} catch (InterruptedException e) {e.printStackTrace();}}}};}.start();//判断小狗足球左上右下斜碰撞new Thread(){public void run() {Rectangle dogRect = new Rectangle(x,y,width,height);Rectangle ballRect = new Rectangle(ball.x,ball.y,ball.width,ball.height);//小狗在球左边实现小球右下if(dogRect.intersects(ballRect)&&dogRect.x<ballRect.x&&dogRect.y<ballRect.y){for(int i=0;i<40;i++){if(ball.x<644&&ball.x>30){ball.x += speed;}if(ball.y>75&&ball.y<435){ball.y += speed;}try {sleep(i);gp.repaint();} catch (InterruptedException e) {e.printStackTrace();}}//小狗在球右边实现小球左上}else if(dogRect.intersects(ballRect)&&dogRect.x>ballRect.x&&dogRect.y>ballRect.y){for(int i=0;i<40;i++){if(ball.x<644&&ball.x>52){ball.x -= speed;}if(ball.y>75&&ball.y<435){ball.y -= speed;}try {sleep(i);gp.repaint();} catch (InterruptedException e) {e.printStackTrace();}}}};}.start();//判断小球到了角落,再踢出来new Thread(){public void run() {Rectangle dogRect = new Rectangle(x,y,width,height);Rectangle ballRect = new Rectangle(ball.x,ball.y,ball.width,ball.height);if(ball.x<=52){if(dogRect.intersects(ballRect)){for(int i=0;i<40;i++){ball.x += speed*2;try {sleep(i);} catch (InterruptedException e) {e.printStackTrace();}}}}else if(ball.y>=435){if(dogRect.intersects(ballRect)){for(int i=0;i<40;i++){ball.y -= speed*2;try {sleep(i);} catch (InterruptedException e) {e.printStackTrace();}}}}else if(ball.x>=642){if(dogRect.intersects(ballRect)){for(int i=0;i<40;i++){ball.x -= speed*2;try {sleep(i);} catch (InterruptedException e) {e.printStackTrace();}}}}else if(ball.y<=75){if(dogRect.intersects(ballRect)){for(int i=0;i<40;i++){ball.y += speed*2;try {sleep(i);} catch (InterruptedException e) {e.printStackTrace();}}}}};}.start();//小狗跑if(up){if(y>=50){y -= speed;if(!left&&!right){image1 = new ImageIcon("runimages/黑狗尾.png").getImage();}}}if(down){if(y<=415){y += speed;if(!left&&!right){image1 = new ImageIcon("runimages/黑狗头.png").getImage();}}}if(left){if(x>=32){x -= speed;}if (!up && !down) {if (imageFlag < 8) {image1 = new ImageIcon("runimages/小黑狗奔跑11.png").getImage();imageFlag++;} else if (imageFlag == 8) {image1 = new ImageIcon("runimages/小黑狗奔跑1.png").getImage();imageFlag = 0;}}}if(right){if(x<=642){x += speed;}if (!up && !down) {if (imageFlag < 8) {image1 = new ImageIcon("runimages/小黑狗奔跑22.png").getImage();imageFlag++;} else if (imageFlag == 8) {image1 = new ImageIcon("runimages/小狗奔跑2.png").getImage();imageFlag = 0;}}}gp.repaint();try {this.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}}public int getX(){return x;}public int getY(){return y;}public void setX(int x){this.x = x;}public void setY(int y){this.y = y;}public Image getBlackDog(){return image1;}public void setWhiteDogImage(Image image){image1 = image;}}

然后在窗口的地方用卡片布局来切换面板,让游戏更多乐趣

package 足球小狗;import java.awt.CardLayout;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JFrame;public class FootBall extends JFrame{public CardLayout cl;public static void main(String[] args) {FootBall fb = new FootBall();fb.initFrame();}public void initFrame(){setTitle("C罗Dog Vs 贝克Dog");setSize(735,520);cl = new CardLayout();this.setLayout(cl);setLocationRelativeTo(null);setDefaultCloseOperation(3);//添加开始面板StartFrame sf = new StartFrame();this.getContentPane().add(sf);//添加游戏面板GamePanel gp = new GamePanel(this);this.getContentPane().add(gp);setVisible(true);MouseAdapter ma = new MouseAdapter() {public void mouseClicked(MouseEvent e) {cl.last(FootBall.this.getContentPane());gp.setFocusable(false);sf.setFocusable(false);FootBall.this.setFocusable(true);}};this.addMouseListener(ma);}}

这就是我这几天做的小狗踢足球。

2 0
原创粉丝点击