最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

骰子游戲《欲罷不能》JAVA實現(xiàn),2-4人游玩

2022-04-23 15:08 作者:yijq23  | 我要投稿


游戲界面

游戲看著挺有意思,邏輯不難,隨便實現(xiàn)了一下,不到300行搞定了,代碼如下,歡迎改進:



import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Point;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;


import javax.swing.JFrame;

import javax.swing.JPanel;


public class MainPanel extends JFrame{

private static final long serialVersionUID = 1L;

public static void main(String[] args) {

new MainPanel();

}

public MainPanel(){

MyPanel mp=new MyPanel();

add(mp);

addMouseListener(mp);

setTitle("欲罷不能");

setSize(800,850);

setLocationRelativeTo(null);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

class MyPanel extends JPanel implements MouseListener{

private static final long serialVersionUID = 1L;

Point[] token;

int flag=0;

int[][] rate;

boolean ifTou=false;

int sel=-1;

int[] hasTaken;

int[] tou;

int maxFlag;

final int[] max={3,5,7,9,11,13,11,9,7,5,3};

Color[] cs={Color.red,Color.blue,Color.green,Color.yellow};

String[] flagString={"紅色","藍色","綠色","黃色"};

public MyPanel(){

init(2);

}

void init(int mf){

flag=0;

sel=-1;

ifTou=false;

token=new Point[3];

rate=new int[4][11];

tou=new int[]{-1,-1,-1,-1};

hasTaken=new int[]{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};

maxFlag=mf;

}

void endTurn(){

for (int i = 0; i < token.length; i++) {

if(token[i]!=null){

rate[flag][token[i].x]=token[i].y;

if(max[token[i].x]==token[i].y){

hasTaken[token[i].x]=flag;

}

}

}

nextFlag();

token=new Point[3];

}

void failTurn(){

nextFlag();

token=new Point[3];

}

void nextFlag(){

flag++;

if(flag==maxFlag)

flag=0;

}

@Override

public void paint(Graphics g) {

super.paint(g);

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

int s=Math.abs(i-5)*60+20;

if(hasTaken[i]>-1)

g.setColor(cs[hasTaken[i]]);

else

g.setColor(Color.black);

for (int j = 0; j < max[i]; j++) {

g.draw3DRect(i*60+50, j*60+s, 50, 50, true);

g.drawString(""+(i+2), i*60+70, j*60+s+30);

}

}

for (int i = 0; i < rate.length; i++) {

g.setColor(cs[i]);

for (int j = 0; j < rate[i].length; j++) {

if(rate[i][j]>0&&hasTaken[j]==-1){

int s=Math.abs(j-5)*60+20;

g.fill3DRect(j*60+50, (max[j]-rate[i][j])*60+s+(12*i), 50, 12, true);

}

}

}

g.setColor(Color.black);

for (int i = 0; i < token.length; i++) {

if(token[i]!=null){

int s=Math.abs(token[i].x-5)*60+20;

g.fill3DRect(token[i].x*60+63, (max[token[i].x]-token[i].y)*60+s+13, 25, 25, true);

}

}

g.setFont(new Font("黑體", 1, 20));

if(ifTou){

g.draw3DRect(600, 650, 50, 50, true);

drawTou(g, 600, 650, tou[0]);

g.draw3DRect(600, 720, 50, 50, true);

drawTou(g, 600, 720, tou[1]);

g.draw3DRect(670, 650, 50, 50, true);

drawTou(g, 670, 650, tou[2]);

g.draw3DRect(670, 720, 50, 50, true);

drawTou(g, 670, 720, tou[3]);

g.setColor(cs[flag]);

switch (sel) {

case 0:

g.draw3DRect(599, 649, 52, 52, true);

break;

case 1:

g.draw3DRect(599, 719, 52, 52, true);

break;

case 2:

g.draw3DRect(669, 649, 52, 52, true);

break;

case 3:

g.draw3DRect(669, 719, 52, 52, true);

break;

}

}

else{

g.setColor(cs[flag]);

g.draw3DRect(600, 650, 100, 50, true);

g.drawString("骰子", 630, 685);

g.draw3DRect(600, 720, 100, 50, true);

g.drawString("結束", 630, 755);

}

g.drawString(flagString[flag]+"的回合", 20, 100);

g.setColor(Color.black);

g.drawString("參與人數(shù)為"+maxFlag, 20, 130);

g.drawString("選擇人數(shù)重新開始:", 520, 100);

for (int i = 1; i < 4; i++) {

g.draw3DRect(600+i*40, 105, 40, 40, true);

g.drawString((i+1)+"", 615+i*40, 135);

}

}

void drawTou(Graphics g,int x,int y,int t){

g.setColor(Color.black);

switch (t) {

case 1:

g.fillOval(x+23, y+23, 4, 4);

break;

case 2:

g.fillOval(x+18, y+23, 4, 4);

g.fillOval(x+29, y+23, 4, 4);

break;

case 3:

g.fillOval(x+23, y+18, 4, 4);

g.fillOval(x+18, y+29, 4, 4);

g.fillOval(x+29, y+29, 4, 4);

break;

case 4:

g.fillOval(x+18, y+18, 4, 4);

g.fillOval(x+29, y+18, 4, 4);

g.fillOval(x+18, y+29, 4, 4);

g.fillOval(x+29, y+29, 4, 4);

break;

case 5:

g.fillOval(x+18, y+18, 4, 4);

g.fillOval(x+29, y+18, 4, 4);

g.fillOval(x+23, y+23, 4, 4);

g.fillOval(x+18, y+29, 4, 4);

g.fillOval(x+29, y+29, 4, 4);

break;

case 6:

g.fillOval(x+18, y+18, 4, 4);

g.fillOval(x+29, y+18, 4, 4);

g.fillOval(x+18, y+23, 4, 4);

g.fillOval(x+29, y+23, 4, 4);

g.fillOval(x+18, y+29, 4, 4);

g.fillOval(x+29, y+29, 4, 4);

break;

}

}

boolean moveToken(int[] h){

boolean hasMove=false;

for (int j = 0; j < h.length; j++) {

if(hasTaken[h[j]]==-1){

for (int i = 0; i < token.length; i++) {

if(token[i]==null){

token[i]=new Point(h[j],rate[flag][h[j]]+1);

hasMove=true;

break;

}

else if(token[i].x==h[j]&&token[i].y<max[h[j]]){

token[i].y++;

hasMove=true;

break;

}

else if(token[i].y==max[h[j]]){

break;

}

}

}

}

return hasMove;

}

@Override

public void mouseClicked(MouseEvent e) {

int x=e.getX()-10;

int y=e.getY()-38;

if(x>640&&x<680&&y>105&&y<145)init(2);

else if(x>680&&x<720&&y>105&&y<145)init(3);

else if(x>720&&x<760&&y>105&&y<145)init(4);

else if(ifTou){

int s=-1;

if(x>600&&x<650&&y>650&&y<700)s=0;

else if(x>600&&x<670&&y>720&&y<770)s=1;

else if(x>670&&x<720&&y>650&&y<700)s=2;

else if(x>670&&x<720&&y>720&&y<770)s=3;

if(s==-1){}

else if(s==sel){

sel=-1;

}

else if(sel==-1){

sel=s;

}

else{

int[] h={0,0};

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

if(i==sel||i==s)h[0]+=tou[i];

else h[1]+=tou[i];

}

h[0]-=2;

h[1]-=2;

if(!moveToken(h)){

failTurn();

}

ifTou=false;

sel=-1;

}

}

else{

if(x>600&&x<700&y>650&&y<700){

for (int i = 0; i < tou.length; i++) {

tou[i]=(int)(Math.random()*6+1);

}

ifTou=true;

}

else if(x>600&&x<700&y>720&&y<770){

endTurn();

}

}

repaint();

}

@Override

public void mousePressed(MouseEvent e) {

}

@Override

public void mouseReleased(MouseEvent e) {

}

@Override

public void mouseEntered(MouseEvent e) {

}

@Override

public void mouseExited(MouseEvent e) {

}

}


骰子游戲《欲罷不能》JAVA實現(xiàn),2-4人游玩的評論 (共 條)

分享到微博請遵守國家法律
康乐县| 五大连池市| 东乡族自治县| 唐山市| 佛教| 正安县| 晋宁县| 抚顺县| 合水县| 茂名市| 临颍县| 海晏县| 开原市| 武强县| 兴业县| 陇西县| 宁强县| 会理县| 元谋县| 宁德市| 西乌珠穆沁旗| 朔州市| 贵南县| 德惠市| 遂宁市| 蓬莱市| 宁城县| 赤峰市| 平武县| 定南县| 江油市| 二连浩特市| 轮台县| 济南市| 双流县| 张家港市| 陆丰市| 阳春市| 商河县| 枞阳县| 乌兰察布市|