
正文
java跑酷游戏代码 java跑步代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
免费可以玩大游戏的代码
本文实例为大家分享了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;
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关问答
Q1: 地铁跑酷不用实名认证的游戏代码
现在地铁酷跑不用实名认证的游戏代码是2436一般都是游戏代码的编号开头都是2436,所以现在天天酷跑基本上都不用实名认证的游戏,都是使用代码的
Q2: 什么代码能玩的地铁跑酷空间站
要玩地铁跑酷空间站,你需要安装手机上的对应的游戏应用。这个游戏的代码不是公开的,是游戏开发商的商业秘密,所以无法通过自己写代码来实现游戏。地铁跑酷空间站是一款比较流行的跑酷游戏,你需要在地铁车站、铁路轨道和地球之外的太空站上完成各种挑战。你需要跑、跳、闪避和滑行,轻松操作就能感受到紧张刺激的游戏体验。总而言之,如果你想享受地铁跑酷空间站带来的乐趣,就需要安装相应的游戏应用并开始进入游戏世界尽情畅玩!
Q3: 急求JAVA源代码,小游戏或者别的
//这是个聊天程序java跑酷游戏代码, 在ECLIPSE 运行 Client.java 就可以java跑酷游戏代码了。 连接是java跑酷游戏代码:localhost
//Server 代码java跑酷游戏代码,
package message;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
public static void main(String[] args) throws Exception{
System.out.print("Server");
ServerSocket socket=new ServerSocket(8888);
Vector v=new Vector();
while(true){
Socket sk=socket.accept();
DataInputStream in=new DataInputStream(sk.getInputStream());
DataOutputStream out=new DataOutputStream(sk.getOutputStream());
v.add(sk);
new ServerThread(in,v).start();
}
}
}
//ServerThread.java 代码
package message;
import java.net.*;
import java.io.*;
import java.util.*;
public class ServerThread extends Thread{
DataInputStream in;
Vector all;
public ServerThread(DataInputStream in,Vector v){
this.in=in;
this.all=v;
}
public void run()
{
while(true)
{
try{
String s1=in.readUTF();
for(int i=0;iall.size();i++)
{
Object obj=all.get(i);
Socket socket=(Socket)obj;
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
out.writeUTF(s1);
System.out.print(i);
out.flush();
}
System.out.print("Message send over!");
}catch(Exception e){e.printStackTrace();};
}
}
}
//ClientFrame.java 代码
package message;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ClientFrame extends JFrame implements ActionListener{
JButton b1=new JButton ("SendMessage");
JButton b2=new JButton("Link Server");
JTextField t1=new JTextField(20);
JTextField t2=new JTextField(20);
JLabel l=new JLabel("输入服务器名字:");
JTextArea area=new JTextArea(10,20);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
Socket socket;
public ClientFrame()
{
this.getContentPane().add(p1);
p2.add(new JScrollPane(area));
p3.add(t1);
p3.add(b1);
p4.add(l);
p4.add(t2);
p4.add(b2);
p2.setLayout(new FlowLayout());
p3.setLayout(new FlowLayout());
p4.setLayout(new FlowLayout());
p1.setLayout(new BorderLayout());
p1.add("North",p2);
p1.add("Center",p3);
p1.add("South",p4);
b1.addActionListener(this);
b2.addActionListener(this);
this.pack();
show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Link Server"))
{
try{
socket=new Socket(t2.getText(),8888);
b2.setEnabled(false);
JOptionPane.showMessageDialog(this, "Connection Success");
DataInputStream in=new DataInputStream(socket.getInputStream());
new ClientThread(in,area).start();
}
catch(Exception e1){
JOptionPane.showMessageDialog(this, "Connection Error");
e1.printStackTrace();};
}
else if(e.getActionCommand().equals("SendMessage"))
{
try{
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
out.writeUTF(t1.getText());
t1.setText("");
}catch(Exception e1){e1.printStackTrace();};
}
}
}
//ClientThread.java 代码
package message;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class ClientThread extends Thread {
DataInputStream in;
JTextArea area;
public ClientThread(DataInputStream in,JTextArea area){
this.in=in;
this.area=area;
}
public void run()
{
while(true){
try{
String s=in.readUTF();
area.append(s);
}
catch(Exception e){e.printStackTrace();};
}
}
}
//Client.java代码
package message;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
new ClientFrame();
}
}
// 每段代码都是个类java跑酷游戏代码,不要弄在一个文件里。 运行 Client.java
good luck to you!
关于java跑酷游戏代码和java跑步代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







