java代码编写完整版水果忍者,java游戲制作之水果忍者

packageFruitninja;importjava.awt.Dimension;importjava.awt.Graphics;importjava.awt.Image;importjava.util.Random;importjavax.swing.ImageIcon;importjavax.swing.JFrame;importjavax.swing.JPanel;public class Backpicture extendsJPanel{private int x=1,y=1;private int line = 100;private int[] ti = new int[3];private int[] pi = new int[6];private int i=0;

Random ran= newRandom();private intvalue;

ThreadPicture tp= new ThreadPicture(800);

AddListener al= new AddListener(this);

ImageIcon background= new ImageIcon(this.getClass().getResource("水果忍者.jpg"));

ImageIcon apple= new ImageIcon(this.getClass().getResource("apple.png"));

ImageIcon lemon= new ImageIcon(this.getClass().getResource("lemon.png"));

ImageIcon orange= new ImageIcon(this.getClass().getResource("orange.png"));

ImageIcon purple= new ImageIcon(this.getClass().getResource("purple.png"));

ImageIcon strawberry= new ImageIcon(this.getClass().getResource("strawberry.png"));

ImageIcon watermelon= new ImageIcon(this.getClass().getResource("watermelon.png"));

ImageIcon apple1= new ImageIcon(this.getClass().getResource("apple-1.png"));

ImageIcon lemon1= new ImageIcon(this.getClass().getResource("lemon-1.png"));

ImageIcon orange1= new ImageIcon(this.getClass().getResource("orange-1.png"));

ImageIcon purple1= new ImageIcon(this.getClass().getResource("purple-1.png"));

ImageIcon strawberry1= new ImageIcon(this.getClass().getResource("strawberry-1.png"));

ImageIcon watermelon1= new ImageIcon(this.getClass().getResource("watermelon-1.png"));public static voidmain(String[] args) {

Backpicture bp= newBackpicture();

bp.initUI();

}publicBackpicture(){

ti=tp.setTi();

}public voidinitUI(){

JFrame jf= newJFrame();

jf.setDefaultCloseOperation(3);

jf.setSize(new Dimension(1500,1000));

jf.setLocationRelativeTo(null);

jf.setTitle("Fruit ninja");

jf.add(this);this.setSize(new Dimension(500,600));

jf.addMouseListener(al);

al.setThreadPicture(tp);

ranValue();

tp.getAl(al);tp.getTi(ti);

jf.setVisible(true);

Thread t= newThread(al);

t.start();

}public voidpaint(Graphics g){super.paint(g);

g.drawImage(background.getImage(),0, 0, 1500, 1000, null);for(int i=0;i<3;i++){

al.Cut();if(ti[i]==1){

g.drawImage(apple.getImage(), ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);

}if(ti[i]==2){

g.drawImage(lemon.getImage(),ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);

}if(ti[i]==3){

g.drawImage(orange.getImage(), ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);

}if(ti[i]==4){

g.drawImage(purple.getImage(), ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);

}if(ti[i]==5){

g.drawImage(strawberry.getImage(),ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);

}if(ti[i]==6){

g.drawImage(watermelon.getImage(),ti[i]*line+ti[i]*100, tp.getY(), 150, 150, null);

}if(ti[i]==7){

System.out.println("ti[i]"+ti[i]);

g.drawImage(apple1.getImage(), (ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);

}if(ti[i]==8){

System.out.println("ti[i]"+ti[i]);

g.drawImage(lemon1.getImage(), (ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);

}if(ti[i]==9){

System.out.println("ti[i]"+ti[i]);

g.drawImage(orange1.getImage(), (ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);

}if(ti[i]==10){

System.out.println("ti[i]"+ti[i]);

g.drawImage(purple1.getImage(), (ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);

}if(ti[i]==11){

System.out.println("ti[i]"+ti[i]);

g.drawImage(strawberry1.getImage(),(ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);

}if(ti[i]==12){

System.out.println("ti[i]"+ti[i]);

g.drawImage(watermelon1.getImage(), (ti[i]-6)*line+(ti[i]-6)*100, tp.getY(), 150, 150, null);

}

}//System.out.println("al.Cut()="+al.Cut());//if(al.Cut()!=0){//

//if(al.Cut()==1){//g.drawImage(apple1.getImage(), al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);//

//}//if(al.Cut()==2){//g.drawImage(lemon1.getImage(),al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);//}//if(al.Cut()==3){//g.drawImage(orange1.getImage(), al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);//}//if(al.Cut()==4){//g.drawImage(purple1.getImage(), al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);//}//if(al.Cut()==5){//g.drawImage(strawberry1.getImage(),al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);//}//if(al.Cut()==6){//g.drawImage(watermelon1.getImage(),al.Cut()*line+al.Cut()*100, tp.getY(), 150, 150, null);//}//}

}public voidranValue(){int pi[] = {1,2,3,4,5,6};//for(int t=3;t > 0;t--)//{//value = ran.nextInt(6)+1;//ti[t-1]=value;//}//al.getValue(ti);// //al.Cut();

while(i<3){

value= ran.nextInt(6);ti[i]=0;if(pi[value]!=0){

ti[i]=pi[value];pi[value]=0;

i++;

}

al.getValue(ti);

}

}

}

水果忍者(Flappy Bird)是一款非常著名的手机游戏,而将其转换成Python项目通常涉及到游戏开发的基础知识,如用户输入处理、图形界面以及简单的动画。由于这是一个相对复杂的任务,我将提供一个简化的版本示例,它并不包含完整的图形用户界面,而是使用文本表示。 ```python import random # 定义游戏角色和障碍物 class FlappyBirdCharacter: def __init__(self): self.y = 0 self.speed = 5 class Obstacle: def __init__(self, x): self.x = x self.speed = -4 def move_character(character, obstacles): # 用户模拟按键控制角色上下移动 if 'up' in keys and character.y > 0: character.y -= character.speed elif 'down' in keys: character.y += character.speed # 防止角色超出屏幕范围 character.y = max(0, min(character.y, HEIGHT)) def game_loop(): screen.fill(BLACK) character.draw(screen) for obstacle in obstacles: obstacle.draw(screen) # 移动并检查碰撞 move_character(character, obstacles) for obstacle in obstacles: if character.y + character.height >= obstacle.y: print("Game Over!") return # 更新屏幕 pygame.display.flip() if __name__ == "__main__": # 初始化Pygame pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() character = FlappyBirdCharacter() obstacles = [Obstacle(random.randint(WIDTH, WIDTH * 2))] while True: events = pygame.event.get() keys = pygame.key.get_pressed() game_loop() clock.tick(FPS) ``` 这个代码只是一个基础框架,实际的游戏会更复杂,包括图形绘制、碰撞检测、得分系统等。要运行这样的游戏,你需要安装pygame库,并根据需求添加更多细节和功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值