本文实例为大家分享了Java实现方块赛跑小游戏的具体代码,供大家参考,具体内容如下
在一个图形界面上构造两个位于同一起跑线方块,起跑线位于界面靠左位置, A 方块先开始运动,向右移动 50 像素后停止,B 方块开始运动,向右移动 100 像素后停 止,A 方块继续向右运动 100 像素后停止,B 方块开始运动,如此循环接替执行,直至 某一个方块到达终点,界面显示该方块胜利信息。
1) 自定义一个threadA,ThreadB, ThreadFrame类(均继承自Thread)。
2) 定义全局变量,方块的位置,总长度,冠军,睡眠时间等,布尔值方块等待变量、游戏继续变量、绘图变量
3) ThreadA(ThreadB):等待waitA(waitB)变量释放,即:等待另一个方块更新完位置;然后随机产生要移动的长度,检查运动后位置与总长度的关系,以此判断游戏是否结束。更新位置信息,更改绘图变量,通知绘图线程重绘。自锁本身,释放另一个方块线程。
4) ThreadFrame:创建类对象,重写run函数,等待绘图变量的命令。接到绘图命令,重绘,判断游戏释放结束,重置绘图命令。
5) 构造函数,paint函数绘制,并进行游戏是否结束的判断,若结束,则打印冠军是谁,退出
6) 主函数,定义threadA,ThreadB, ThreadFrame类的对象,运行。用jion函数等待子线程的结束。
import java.awt.*;import javax.swing.*;public class four3 extends JFrame { // 全局变量 static int positionA = 50, positionB = 50, distanceAll = 1600; static int RecWidth = 50, RecHeight = 50; static char winner; static long sleeptime = 300; static boolean waitA = true, waitB = true, gaming = true, unrepaint = true; //构造函数 public four3() { setTitle("多线程:方块赛跑"); setBackground(Color.WHITE); setSize(1600, 500); setLocation(0, 200); setResizable(false); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } //绘图函数 public void paint(Graphics g) { // TODO Auto-generated method stub g.clearRect(0, 0, 1600, 900); g.setColor(Color.RED); g.fillRect(positionA - 50, 100, RecWidth, RecHeight); g.setColor(Color.BLUE); g.fillRect(positionB - 50, 300, RecWidth, RecHeight); if (!gaming) { g.setFont(new Font("宋体", ALLBITS, 50)); if (winner == 'A') { g.setColor(Color.RED); g.drawString(new String("Winner Is The Red One!"), 550, 250); } else if (winner == 'B') { g.setColor(Color.BLUE); g.drawString(new String("Winner Is The Blue One!"), 550, 250); } } } public static void main(String[] args) { waitA = false; waitB = true; unrepaint = false; threadframe tf = new threadframe(); threadA tA = new threadA(); threadB tB = new threadB(); tf.start(); tA.start(); tB.start(); try { tf.join(); tA.join(); tB.join(); } catch (Exception e) { // TODO: handle exception } return; } //红色方块线程 public static class threadA extends Thread { public void run() { while (gaming) { while (waitA) { if (!gaming)return; System.out.print(""); } try { sleep(sleeptime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } int distance = (int) (Math.random() * 100000) % 100; positionA += distance; if (positionA >= distanceAll) { positionA = distanceAll; unrepaint = false; gaming = false; winner = 'A'; } unrepaint = false; waitA = true; waitB = false; } } } //蓝色方块线程 public static class threadB extends Thread { public void run() { while (gaming) { while (waitB) { if (!gaming)return; System.out.print(""); } try { sleep(sleeptime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } int distance = (int) (Math.random() * 100000) % 100; positionB += distance; if (positionB >= distanceAll) { positionB = distanceAll; unrepaint = false; gaming = false; winner = 'B'; } unrepaint = false; waitB = true; waitA = false; } } } //框架刷新线程 public static class threadframe extends Thread { four3 jiemian = new four3(); public void run() { while (gaming) { while (unrepaint) { if (!gaming)return; System.out.print(""); } jiemian.repaint(); unrepaint = true; } } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
TAG:java游戏代码