「java蜘蛛纸牌代码」电脑运行蜘蛛纸牌代码
今天给各位分享java蜘蛛纸牌代码的知识,其中也会对电脑运行蜘蛛纸牌代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
高分求java纸牌游戏源代码
Araneid 蜘蛛纸牌游戏
详细参考工程主页:
源代码下载(或附件):
二进制下载:
运行时请先解压缩,然后执行 classes 目录下的 start.bat
Java蜘蛛纸牌中用线程来实现什么
你可以这样想,Java程序是顺序执行的。
如果只有一个主线程的话,那么要是计算出牌、发牌这些后台操作前台显示就什么也做不了,那样你做的游戏,在程序后台做这些时候就处于假死状态。
为了让这些处理之间互相不干扰,就需要线程,让他们分别执行在不同的线程里,只在必要时进行数据共享和交换,这叫线程通信。
当然,这里只是举了一个简单例子,是显示和计算之间用不同线程,实际上你想做的任何事都可以用线程来做,这样可以充分利用CPU,毕竟现在都是双核四核了,说的广一点,这属于”并行程序设计“了。
JAVA蜘蛛纸牌
我们做作业,做了个扑克牌的游戏,不过不是蜘蛛牌,没有添加出牌规则算法的。发给你,你自己看看。。纸牌要自己下,把名字改成0--51的数字,背面名字改成rear,横向背面改成rearheng.jpg
package com;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class puke implements MouseListener,ActionListener{
public JFrame frame=new JFrame("扑克牌发牌");
public JButton button=new JButton("Start");
public JPanel pbutton=new JPanel();
public JPanel north=new JPanel();
public JPanel west=new JPanel();
public JPanel south=new JPanel();
public JPanel east=new JPanel();
public JPanel middle=new JPanel();
public Container container=frame.getContentPane();
public static JLabel[] l=new JLabel[52];
public static JLabel[] ll =new JLabel[52];
@SuppressWarnings("unchecked")
public static Set set=new HashSet();
public static JLabel[] l1=new JLabel[13];
public static JLabel[] l2 =new JLabel[13];
public static JLabel[] l3 =new JLabel[13];
public static JLabel[] l4 =new JLabel[13];
public static JLabel[] ll1 =new JLabel[13];
public static JLabel[] ll2 =new JLabel[13];
public static JLabel[] ll3 =new JLabel[13];
public static JLabel[] ll4=new JLabel[13];
boolean gstar=false;
int nn=ll.length;
int n=l.length;
int s=0;
int x=0,y=0;
@SuppressWarnings("unchecked")
public puke(){
frame.setBounds(0, 0, 1000, 750);
container.setLayout(null);
north.setLayout(null);
west.setLayout(null);
south.setLayout(null);
east.setLayout(null);
north.setBounds(200, 0, 600, 200);
north.setBackground(Color.GREEN);
west.setBounds(0,200,200,350);
west.setBackground(Color.RED);
south.setBounds(200,550,600,200);
south.setBackground(Color.PINK);
east.setBounds(800, 200, 200, 350);
east.setBackground(Color.BLUE);
pbutton.setBounds(420, 420,100, 40);
middle.setBounds(200, 200, 600, 400);
middle.setBackground(Color.YELLOW);
for(int i=0;il.length;i++){
l[i]=new JLabel(new ImageIcon("src/com/rear.jpg"));
l[i].setBounds(350+x,270,71,96);
container.add(l[i]);
x+=5;
}
for(int i=0;ill.length;i++){
ll[i]=new JLabel(new ImageIcon("src/com/"+i+".jpg"));
set.add(ll[i]);
}
button.addActionListener(this);
pbutton.add(button);
container.add(north);
container.add(west);
container.add(south);
container.add(east);
container.add(pbutton);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
compare();
}
@SuppressWarnings({ "unchecked", "unchecked" })
public void play()//分发正面52张牌
{
IteratorJLabel it = set.iterator();
while(it.hasNext()){
for(int i=0;i13;i++){
if(nn%4==0){
ll1[i]=(JLabel) it.next();
nn--;}
if(nn%4==3){
ll2[i]=(JLabel) it.next();
nn--;}
if(nn%4==2){
ll3[i]=(JLabel) it.next();
nn--;}
if(nn%4==1){
ll4[i]=(JLabel) it.next();
nn--;}
}
}
}
public void move()//完成发牌,每位玩家得到13张牌
{
play();
int i;
while(n!=0){
for(i=0;i13;i++){
if(s52){
if(n%4==0){
l1[i]=l[s];
l1[i].setBounds(-120+x, 70, 71, 96);
north.add(l1[i]);
try {
Thread.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
x+=10;
s++;
n--;
continue;
}
if(n%4==3){
l[s].setIcon(new ImageIcon("src/com/rearheng.jpg"));
l2[i]=l[s];
l2[i].setBounds(70, 20+y,96,71);
west.add(l2[i]);
try {
Thread.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
y+=10;
s++;
n--;
continue;
}
if(n%4==2){
l[s].setIcon(null);
l3[i]=ll3[i];
l3[i].setBounds(640-x, 30, 71, 96);
l3[i].addMouseListener(this);
south.add(l3[i]);
try {
Thread.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
x+=10;
s++;
n--;
continue;
}
if(n%4==1){
l[s].setIcon(new ImageIcon("src/com/rearheng.jpg"));
l4[i]=l[s];
l4[i].setBounds(30,260-y,96,71);
east.add(l4[i]);
try {
Thread.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
y+=10;
s++;
n--;
continue;
}
}
}
}
button.setVisible(false);
pbutton.remove(button);
container.remove(pbutton);
container.add(middle);
frame.repaint();
}
public void mouseClicked(MouseEvent e)//点击鼠标出牌
{
for(int i=0;i13;i++){
if(gstar){
middle.remove(l3[i]);
middle.remove(ll1[i]);
middle.remove(ll2[i]);
middle.remove(ll4[i]);
if(e.getSource()==l3[i]){
north.remove(l1[i]);
west.remove(l2[i]);
east.remove(l4[i]);
south.remove(l3[i]);
middle.add(l3[i]);
middle.add(ll1[i]);
middle.add(ll2[i]);
middle.add(ll4[i]);
l3[i].setBounds(l3[i].getBounds().x, 200, 71, 100);
ll1[i].setBounds(250, 50, 71, 96);
ll2[i].setBounds(150, 150, 71, 96);
ll3[i].setBounds(250, 200, 71, 96);
ll4[i].setBounds(350, 150, 71, 96);
frame.repaint();
}
}
}
}
public void mouseEntered(MouseEvent e) //选择要出的牌
{
for(int i=0;i13;i++){
if(gstar){
if(e.getSource()==l3[i]){
l3[i].setBounds(l3[i].getBounds().x, l3[i].getBounds().y-20, 71, 96);
}
}
}
}
public void mouseExited(MouseEvent e) {
for(int i=0;i13;i++){
if(gstar){
if(e.getSource()==l3[i]){
l3[i].setBounds(l3[i].getBounds().x,l3[i].getBounds().y+20, 71, 96);
}}}
}
public puke getthis(){
return this;
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public static void main(String[]args){
new puke();
}
public class TestThread extends Thread{
private puke g=null;
public TestThread(puke g){
this.g=g;
}
public void run() {
while(true){
g.move();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void actionPerformed(ActionEvent e) //点击按钮事件
{
if(e.getSource()==button){
gstar=true;
TestThread t= new TestThread(this);
t.start();
}
}
@SuppressWarnings("unchecked")
public int compare(){
JLabel[] lab=new JLabel[ll.length];
String[] str=new String[ll.length];
set.toArray(lab);
for(int l=0;lll.length;l++){
str[l]=lab[l].getIcon().toString();
System.out.println(str[l]);
}
return n;
}
}
java蜘蛛纸牌代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于电脑运行蜘蛛纸牌代码、java蜘蛛纸牌代码的信息别忘了在本站进行查找喔。
发布于:2022-11-22,除非注明,否则均为
原创文章,转载请注明出处。