java飞扬的小鸟代码_小代码   飞翔的小鸟

/**********************

【飞翔的小鸟】

已由DOS环境测试

命令 为    javac Main.java

java   Main

*************************/

import java.awt.*;

import java.util.*;

import java.awt.event.*;

import java.util.List;

import javax.swing.JPanel;

class wzpoint {

int x;

int y1;

int y2;

wzpoint(int xx, int y, int yy) {

x = xx;

y1 = y;

y2 = yy;

}

public void setX(int x) {

this.x = this.x-x;

}

}

public class Main extends JPanel {

/*全局變量的解釋    a 用於時間    p傳點   鏈表產生綫條    wzy 飛行高度frame面板*/

static int a = 1;

static int wzy = 100;

static Point p = null;

static List list = new ArrayList();

public static Frame frame = new Frame();

public static void main(String[] args) {

list.add(new wzpoint(480, 50, 220));

list.add(new wzpoint(240, 100, 220));

list.add(new wzpoint(100, 100, 500));

list.add(new wzpoint(360, 60, 360));

list.add(new wzpoint(280, 150, 300));

Main m = new Main();

m.setSize(new Dimension(800, 666));

m.setVisible(true);

frame.add(m);

frame.setSize(800, 666);

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

m.Action();

frame.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

int x = e.getX();

int y = e.getY();

wzy=y;

p = new Point(x, y);

frame.repaint();

}

});

frame.setVisible(true);

}

private Timer timer;

private void Action() {

timer = new Timer();

timer.schedule(new TimerTask() {

public void run() {

int n=5;//速度值

for(int i=0;i<5;i++)

{

list.get(i).setX(n*5);//改變 x值  使器移動

}

if(list.size()<6)

{

int x=(int) (Math.random()*100)+50;

int y=(int) (Math.random()*400)+50;

list.add(new wzpoint(x+300,x,y));

}

wzpoint wz=list.get(0);

if(wz.x<50)list.remove(0);

if((wzy>wz.y1&&wzywz.y2&&wzy

{

p =new Point(1000,1000);

frame.repaint();

}

else

{

frame.repaint();

}

}

}, new Date(), 1000);

}

@Override

public void paint(Graphics g) {

if (a == 1)

g.drawString("wz", 60, 200);

g.drawString("【欢迎使用飞行机】", 550, 100);

g.drawString("点击屏幕上下两状态改变飞机的位置", 520, 120);

g.drawString("不点击时屏幕不断刷新新的页面", 520, 140);

g.drawLine(50, 50, 500, 50);

g.drawLine(50, 50, 50, 500);

g.drawLine(500, 50, 500, 500);

g.drawLine(50, 500, 500, 500);

for (int i = 0; i <5; i++) {

wzpoint wz=list.get(i);

if (wz.x> 50) {

g.drawLine(wz.x, wz.y1, wz.x, wz.y2);

}

}

if (p != null) {

if (p.getX()==1000)g.drawString("you had fail once",300,300);

if (50 

g.drawString("wz", 60, (int) p.getY());

}

else {

g.drawString("【error】", 44, 510);

g.drawString("X = " + p.getX(), 44, 520);

g.drawString("Y = " + p.getY(), 44, 530);

}

}

g.drawString("我会一直打印时间 " + a + "s", 100, 40);

a++;

}

}

891412ee892aa15e2b2f48f1042c722a.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的飞扬小鸟游戏的代码,使用了pgzero库: ```python import pgzrun import random WIDTH = 400 HEIGHT = 708 bird = Actor('bird1', (75, 350)) bird.dead = False bird.score = 0 bird.vy = 0 bird.gravity = 0.25 bird.flap = -4.6 pipe_gap = 140 pipe_frequency = 1200 last_pipe = pipe_frequency pipes = [] def update_pipes(): global pipes, last_pipe for pipe in pipes: pipe.centerx -= 2 if pipe.right < 0: pipes.remove(pipe) if last_pipe > pipe_frequency: size = random.randint(50, 220) bottom_pipe = Actor('bottom', (WIDTH, size + pipe_gap + 80)) top_pipe = Actor('top', (WIDTH, size - 80)) pipes.append(bottom_pipe) pipes.append(top_pipe) last_pipe = 0 last_pipe += 1 def update_bird(): global bird bird.vy += bird.gravity bird.y += bird.vy if bird.y > HEIGHT: bird.dead = True bird.y = HEIGHT def update(): global bird if not bird.dead: update_pipes() update_bird() def on_key_down(): global bird if not bird.dead: bird.vy = bird.flap def draw(): screen.blit('background', (0,0)) for pipe in pipes: if pipe.image == 'bottom': pipe.draw() else: pipe.draw() bird.draw() screen.draw.text( str(bird.score), color='white', midtop=(WIDTH//2, 10), fontsize=70 ) if bird.dead: screen.draw.text( 'Game Over', color='white', center=(WIDTH//2, HEIGHT//2), fontsize=50 ) def check_collisions(): global bird if not bird.dead: for pipe in pipes: if pipe.colliderect(bird): bird.dead = True return if bird.y > HEIGHT: bird.dead = True return if bird.top < 0: bird.top = 0 elif bird.bottom > HEIGHT: bird.bottom = HEIGHT bird.score = len([p for p in pipes if p.right < bird.x]) def game_loop(): while True: check_collisions() draw() if keyboard.RETURN: reset() def reset(): global bird, pipes, last_pipe bird = Actor('bird1', (75, 350)) bird.dead = False bird.score = 0 bird.vy = 0 bird.gravity = 0.25 bird.flap = -4.6 pipes = [] last_pipe = pipe_frequency game_loop() ``` 运行以上代码,即可开始游戏。玩家需要控制小鸟跳过障碍物,并在屏幕上方积累分数。当小鸟碰到障碍物或触底时,游戏结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值