《游戏学习》java代码实现《愤怒的小鸟》实战源码

this.getCharacterfixdef().filter.groupIndex=-1;

this.charactershape=new CircleShape();

Body bird=mom.createBody(characterdef);

switch(birdtype) {

case 1:

this.charactershape.m_radius=0.5f;

this.getCharacterfixdef().shape= this.charactershape;

this.getCharacterinfo().setName(“Lil Bird”);

this.getCharacterinfo().setHafheight(0.5f);

this.getCharacterinfo().setHafwidth(0.5f);

this.getCharacterinfo().setAppearance(birds1.getImage());

bird.m_userData=this.getCharacterinfo();

bird.createFixture(characterfixdef);

break;

case 2:

this.charactershape.m_radius=0.7f;

this.getCharacterfixdef().shape= this.charactershape;

this.getCharacterinfo().setName(“Strong Bird”);

this.getCharacterinfo().setHafheight(0.7f);

this.getCharacterinfo().setHafwidth(0.7f);

this.getCharacterinfo().setAppearance(birds2.getImage());

bird.m_userData=this.getCharacterinfo();

bird.createFixture(characterfixdef);

break;

case 3:

this.charactershape.m_radius=1f;

this.getCharacterfixdef().shape= this.charactershape;

this.getCharacterinfo().setName(“Angry Bird”);

this.getCharacterinfo().setHafheight(1f);

this.getCharacterinfo().setHafwidth(1f);

this.getCharacterinfo().setAppearance(birds3.getImage());

bird.m_userData=this.getCharacterinfo();

bird.createFixture(characterfixdef);

break;

}

return bird;

}

}

启动类  AngryBirdsMain

import AngryBirdsLevel.BirdsLevel;

/**

  • @author tarzan

*/

public class AngryBirdsMain {

/**

  • Entrance of This Game.

*/

public static void main(String[] args){

BirdsLevel ts=new BirdsLevel();

AngryBirdsMenu menu=new AngryBirdsMenu();

AngryBirdsPanel game=new AngryBirdsPanel();

AngryBirdsViewFrame mainframe=new AngryBirdsViewFrame(menu,game);

//MusicController music=new MusicController();

AngryBirdsController tc=new AngryBirdsController(ts,game);

AngryBirdsMenuController mc=new AngryBirdsMenuController(menu);

AngryBirdsPanelController pc=new AngryBirdsPanelController(mainframe.getLayoutpanel(),mc,tc);

ts.getWorld().setContactListener(tc);

//music.start();

Thread switcher=new Thread(pc);

switcher.start();

mainframe.setVisible(true);

}

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

面试前一定少不了刷题,为了方便大家复习,我分享一波个人整理的面试大全宝典

  • Java核心知识整理

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

Java核心知识

  • Spring全家桶(实战系列)

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

  • 其他电子书资料

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多

Step3:刷题

既然是要面试,那么就少不了刷题,实际上春节回家后,哪儿也去不了,我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。

以下是我私藏的面试题库:

2020年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。

以下是我私藏的面试题库:

[外链图片转存中…(img-VDkt9GtW-1713385639494)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 15
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是愤怒小鸟游戏Java示例代码: ```java import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; public class AngryBirds extends JPanel { private static final long serialVersionUID = 1L; private static final int WIDTH = 800; private static final int HEIGHT = 600; private BufferedImage birdImage; private BufferedImage pigImage; private BufferedImage background; private List<Bird> birds = new ArrayList<Bird>(); private List<Pig> pigs = new ArrayList<Pig>(); private boolean isDragging = false; private int mouseX; private int mouseY; private int releaseX; private int releaseY; public AngryBirds() { try { birdImage = ImageIO.read(getClass().getResourceAsStream("/bird.png")); pigImage = ImageIO.read(getClass().getResourceAsStream("/pig.png")); background = ImageIO.read(getClass().getResourceAsStream("/background.png")); } catch (IOException e) { e.printStackTrace(); } addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { for (Bird bird : birds) { if (bird.contains(e.getX(), e.getY())) { isDragging = true; mouseX = e.getX(); mouseY = e.getY(); bird.setSelected(true); break; } } } @Override public void mouseReleased(MouseEvent e) { if (isDragging) { isDragging = false; Bird bird = null; for (Bird b : birds) { if (b.isSelected()) { bird = b; break; } } if (bird != null) { releaseX = e.getX(); releaseY = e.getY(); bird.setVelocity((mouseX - releaseX) / 5, (mouseY - releaseY) / 5); bird.setSelected(false); } } } }); addMouseMotionListener(new MouseAdapter() { @Override public void mouseDragged(MouseEvent e) { if (isDragging) { for (Bird bird : birds) { if (bird.isSelected()) { bird.setX(e.getX()); bird.setY(e.getY()); break; } } } } }); addBird(WIDTH / 2, HEIGHT - 100); addBird(WIDTH / 2 + 50, HEIGHT - 100); addBird(WIDTH / 2 + 100, HEIGHT - 100); addPig(WIDTH / 2 + 200, HEIGHT - 100); addPig(WIDTH / 2 + 250, HEIGHT - 100); addPig(WIDTH / 2 + 300, HEIGHT - 100); } private void addBird(int x, int y) { birds.add(new Bird(x, y, birdImage)); } private void addPig(int x, int y) { pigs.add(new Pig(x, y, pigImage)); } public void update() { for (Bird bird : birds) { if (bird.isFlying()) { bird.update(); } } for (Pig pig : pigs) { if (pig.isAlive()) { pig.update(); for (Bird bird : birds) { if (bird.collidesWith(pig)) { bird.setFlying(false); pig.setAlive(false); break; } } } } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawImage(background, 0, 0, null); for (Bird bird : birds) { bird.draw(g2d); } for (Pig pig : pigs) { pig.draw(g2d); } } public static void main(String[] args) { JFrame frame = new JFrame("Angry Birds"); frame.setSize(WIDTH, HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); AngryBirds game = new AngryBirds(); frame.add(game); frame.setVisible(true); while (true) { game.update(); game.repaint(); try { Thread.sleep(1000 / 60); } catch (InterruptedException e) { e.printStackTrace(); } } } private static class Bird { private int x; private int y; private int width; private int height; private double vx; private double vy; private boolean flying; private boolean selected; private BufferedImage image; public Bird(int x, int y, BufferedImage image) { this.x = x; this.y = y; this.width = image.getWidth(); this.height = image.getHeight(); this.vx = 0; this.vy = 0; this.flying = true; this.selected = false; this.image = image; } public void update() { x += vx; y += vy; vy += 0.5; } public void draw(Graphics2D g2d) { AffineTransform transform = new AffineTransform(); transform.translate(x - width / 2, y - height / 2); if (selected) { transform.scale(1.5, 1.5); } g2d.drawImage(image, transform, null); } public boolean contains(int x, int y) { return x >= this.x - width / 2 && x <= this.x + width / 2 && y >= this.y - height / 2 && y <= this.y + height / 2; } public boolean collidesWith(Pig pig) { int px = pig.getX(); int py = pig.getY(); int pw = pig.getWidth(); int ph = pig.getHeight(); return x >= px - pw / 2 && x <= px + pw / 2 && y >= py - ph / 2 && y <= py + ph / 2; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public double getVelocityX() { return vx; } public double getVelocityY() { return vy; } public void setVelocity(double vx, double vy) { this.vx = vx; this.vy = vy; } public boolean isFlying() { return flying; } public void setFlying(boolean flying) { this.flying = flying; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } } private static class Pig { private int x; private int y; private int width; private int height; private boolean alive; private BufferedImage image; public Pig(int x, int y, BufferedImage image) { this.x = x; this.y = y; this.width = image.getWidth(); this.height = image.getHeight(); this.alive = true; this.image = image; } public void update() {} public void draw(Graphics2D g2d) { g2d.drawImage(image, x - width / 2, y - height / 2, null); } public int getX() { return x; } public int getY() { return y; } public int getWidth() { return width; } public int getHeight() { return height; } public boolean isAlive() { return alive; } public void setAlive(boolean alive) { this.alive = alive; } } } ``` 这个示例代码使用 Java Swing 实现了一个简单的“愤怒小鸟游戏。在游戏中,玩家可以拖动小鸟,调整其位置和发射角度,然后释放鼠标,使小鸟飞向目标猪。游戏中包括了小鸟和猪的图像、碰撞检测、物理运动等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值