本文目录一览:
- java代码猜拳游戏相关代码请教
- 运行在Eclipse环境下的java扫雷游戏的初级代码是什么?
- 基于Java语言的打地鼠的小游戏源代码是什么?
- 求一份抽奖游戏(Java写的代码)
- 求java小游戏源代码
java代码猜拳游戏相关代码请教
comp
是电脑产生的随机数字(电脑出的拳),people
是人出的拳。因为剪刀石头布只有 1 2 3。
如果电脑的数字比的你刚好大1,就是它比你的大。例如21,32对应就是(石头大于剪刀,布大于石头)。但也有可能是剪刀大于布。那么剪刀的位置是1,布的位置是3。所以当电脑数字减你的数字等于2时,就是(电脑出的布,你出的石头这样的情况了)。
所以是if((comp-people)==-1 || (comp-people)==2)
这两者结合就是你赢的时候。
运行在Eclipse环境下的java扫雷游戏的初级代码是什么?
import java.awt.Button;
import java.util.Set;
// 每一个小方块类
public class Diamond extends Button {
private Diamond[] diamonds;
// 该小方块周围的八个方向上的小方块
private Diamond east;
private Diamond north;
private Diamond northEast;
private Diamond northWest;
private Diamond south;
private Diamond southEast;
private Diamond southWest;
private Diamond west;
private boolean isBomb; // 是否是雷
private boolean isChange; // 又没有被翻过
private int no; // 产生的方块的编号
// 持有所有小方块的引用,方便进行操作
public Diamond(Diamond[] diamonds) {
this.diamonds = diamonds;
}
// 按键时方块发生改变
public boolean change() {
this.isChange = true; // 说明已经翻过了
if (isBomb) { // 触雷
// this.setBackground(Color.red);
return true;
} else { // 不是雷,就显示周围雷的数目
// this.setLabel(this.getNearBombNo() + "");
this.setLabel(this.getNearBombNo() + "");
// if(this.getNearBombNo() == 0) {
// this.moveon();
// }
return false;
}
}
// 获得该小方块周围雷的数量
public int getNearBombNo() {
int no = 0;
if (this.northWest != null && this.northWest.isBomb) no++;
if (this.north != null && this.north.isBomb) no++;
if (this.northEast != null && this.northEast.isBomb) no++;
if (this.east != null && this.east.isBomb) no++;
if (this.southEast != null && this.southEast.isBomb) no++;
if (this.south != null && this.south.isBomb) no++;
if (this.southWest != null && this.southWest.isBomb) no++;
if (this.west != null && this.west.isBomb) no++;
return no;
}
// 获得该小方块周围的小方块
public Diamond getNearDimaond(int i) {
int index = -1;
switch (i) {
case 1: // 1表示西北,2,表示北,以此类推
index = no - 10;
if (index < 0) {
return null;
} else {
return diamonds[index];
}
case 2:
index = no - 9;
if (index < 0) {
return null;
} else {
return diamonds[index];
}
case 3:
index = no - 8;
if (index < 0) {
return null;
} else {
return diamonds[index];
}
case 4:
index = no + 1;
if (no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {
return null;
} else {
return diamonds[index];
}
case 5:
index = no + 10;
if (index > 81 || no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {
return null;
} else {
return diamonds[index];
}
case 6:
index = no + 9;
if (index > 81) {
return null;
} else {
return diamonds[index];
}
case 7:
index = no + 8;
if (index > 81 || no == 1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
case 8:
index = no - 1;
if (no == 1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {
return null;
} else {
return diamonds[index];
}
}
return null;
}
// 递归,set是用来装已经翻过的小方块的,不然会死循环,为什么用set,因为set是不重复的
public void moveon(Set set) {
set.add(this); // 先把自己加上
if (this.getNorthWest() != null && this.getNorthWest().isBomb == false) {
this.getNorthWest().change();
if (this.getNorthWest().getNearBombNo() == 0) {
if (set.contains(this.getNorthWest()) == false)
this.getNorthWest().moveon(set);
}
set.add(this.getNorthWest());
}
if (this.getNorth() != null && this.getNorth().isBomb == false) {
this.getNorth().change();
if (this.getNorth().getNearBombNo() == 0) {
if (set.contains(this.getNorth()) == false)
this.getNorth().moveon(set);
}
set.add(this.getNorth());
}
if (this.getNorthEast() != null && this.getNorthEast().isBomb == false) {
this.getNorthEast().change();
if (this.getNorthEast().getNearBombNo() == 0) {
if (set.contains(this.getNorthEast()) == false)
this.getNorthEast().moveon(set);
}
set.add(this.getNorthEast());
}
if (this.getEast() != null && this.getEast().isBomb == false) {
this.getEast().change();
if (this.getEast().getNearBombNo() == 0) {
if (set.contains(this.getEast()) == false)
this.getEast().moveon(set);
}
set.add(this.getEast());
}
if (this.getSouthEast() != null && this.getSouthEast().isBomb == false) {
this.getSouthEast().change();
if (this.getSouthEast().getNearBombNo() == 0) {
if (set.contains(this.getSouthEast()) == false)
this.getSouthEast().moveon(set);
}
set.add(this.getSouthEast());
}
if (this.getSouth() != null && this.getSouth().isBomb == false) {
this.getSouth().change();
if (this.getSouth().getNearBombNo() == 0) {
if (set.contains(this.getSouth()) == false)
this.getSouth().moveon(set);
}
set.add(this.getSouth());
}
if (this.getSouthWest() != null && this.getSouthWest().isBomb == false) {
this.getSouthWest().change();
if (this.getSouthWest().getNearBombNo() == 0) {
if (set.contains(this.getSouthWest()) == false)
this.getSouthWest().moveon(set);
}
set.add(this.getSouthWest());
}
if (this.getWest() != null && this.getWest().isBomb == false) {
this.getWest().change();
if (this.getWest().getNearBombNo() == 0) {
if (set.contains(this.getWest()) == false)
this.getWest().moveon(set);
}
set.add(this.getWest());
}
}
public Diamond getEast() {
return east;
}
public int getNo() {
return no;
}
public Diamond getNorth() {
return north;
}
public Diamond getNorthEast() {
return northEast;
}
public Diamond getNorthWest() {
return northWest;
}
public Diamond getSouth() {
return south;
}
public Diamond getSouthEast() {
return southEast;
}
public Diamond getSouthWest() {
return southWest;
}
public Diamond getWest() {
return west;
}
public boolean isBomb() {
return isBomb;
}
public boolean isChange() {
return isChange;
}
public void setBomb(boolean isBomb) {
this.isBomb = isBomb;
}
public void setChange(boolean isChange) {
this.isChange = isChange;
}
public void setDiamonds(Diamond[] diamonds) {
this.diamonds = diamonds;
}
public void setEast(Diamond east) {
this.east = east;
}
public void setNo(int no) {
this.no = no;
}
public void setNorth(Diamond north) {
this.north = north;
}
public void setNorthEast(Diamond northEast) {
this.northEast = northEast;
}
public void setNorthWest(Diamond northWest) {
this.northWest = northWest;
}
public void setSouth(Diamond south) {
this.south = south;
}
public void setSouthEast(Diamond southEast) {
this.southEast = southEast;
}
public void setSouthWest(Diamond southWest) {
this.southWest = southWest;
}
public void setWest(Diamond west) {
this.west = west;
}
}
基于Java语言的打地鼠的小游戏源代码是什么?
public void mouseClicked(MouseEvent e) {
Object source = e.getSource(); // 获取事件源,即地鼠标签
if (source instanceof JLabel) { // 如果事件是标签组件
JLabel mouse = (JLabel) source; // 强制转换为JLabel标签
mouse.setIcon(null); // 取消标签图标
}
});
this.getContentPane().add(mouses[i]); // 添加显示地鼠的标签到窗体
mouses[0].setLocation(253, 300); // 设置每个标签的位置
mouses[1].setLocation(333, 250);
mouses[2].setLocation(388, 296);
mouses[3].setLocation(362, 364);
mouses[4].setLocation(189, 353);
mouses[5].setLocation(240, 409);
final JLabel backLabel = new JLabel(); // 创建显示背景的标签
backLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
this.setBounds(100, 100, img.getIconWidth(), img.getIconHeight());
backLabel.setIcon(img); // 添加背景到标签
this.getContentPane().add(backLabel); // 添加背景标签到窗体
/**
* 线程的核心方法
*/
public void run() {
while (true) { // 使用无限循环
try {
Thread.sleep(3000); // 使线程休眠3秒
int index = (int) (Math.random() * 6); // 生成随机的地鼠索引
if (mouses[index].getIcon() == null) { // 如果地鼠标签没有设置图片
mouses[index].setIcon(imgMouse); // 为该标签添加地鼠图片
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
求一份抽奖游戏(Java写的代码)
import java.util.Scanner;
public class f {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入抽奖号码上限:");
int max = scan.nextInt();
System.out.print("请输入抽奖次数:");
int n = scan.nextInt();
System.out.print("中奖号码依次为:");
for (int i = 0; i < n; i++) {
System.out.print((int) (Math.random() * max + 1) + " ");
}
}
}
求java小游戏源代码
表1. CheckerDrag.java
// CheckerDrag.java
import java.awt.*;
import java.awt.event.*;
public class CheckerDrag extends java.applet.Applet {
// 棋盘上每个小方格的尺寸
final static int SQUAREDIM = 40;
// 棋盘的尺寸 – 包括黑色的轮廓线
final static int BOARDDIM = 8 * SQUAREDIM + 2;
// 棋子的尺寸 – 方格尺寸的3/4
final static int CHECKERDIM = 3 * SQUAREDIM / 4;
// 方格的颜色为深绿色或者白色
final static Color darkGreen = new Color(0, 128, 0);
// 拖动标记 --当用户在棋子上按下鼠标按键时设为true,释放鼠标按键时设为false
boolean inDrag = false;
// 棋盘左上角的左方向坐标
int boardx;
// 棋盘左上角的上方向坐标
int boardy;
// 棋子矩形原点(左上角)的左方向坐标
int ox;
// 棋子矩形原点(左上角)的上方向坐标
int oy;
// 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移
int relx;
// 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移
int rely;
// applet绘图区域的宽度
int width;
// applet绘图区域的高度
int height;
// 图像缓冲
Image imBuffer;
// 图像缓冲相关联的图形背景
Graphics imG;
public void init() {
// 获取applet绘图区域的尺寸
width = getSize().width;
height = getSize().height;
// 创建图像缓冲
imBuffer = createImage(width, height);
// 取出图像缓冲相关联的图形背景
imG = imBuffer.getGraphics();
// 初始化棋盘的原点,使棋盘在屏幕上居中
boardx = (width - BOARDDIM) / 2 + 1;
boardy = (height - BOARDDIM) / 2 + 1;
// 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中
ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1;
oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1;
// 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
// 获取按键时的鼠标坐标
int x = e.getX();
int y = e.getY();
// 在按键时如果鼠标位于可拖动的棋子上方
// (也就是contains (x, y)返回true),则保存当前
// 鼠标坐标与棋子的原点之间的距离(始终为正值)并且
// 将拖动标志设为true(用来表明正处在拖动过程中)
if (contains(x, y)) {
relx = x - ox;
rely = y - oy;
inDrag = true;
}
}
boolean contains(int x, int y) {
// 计算棋子的中心位置
int cox = ox + CHECKERDIM / 2;
int coy = oy + CHECKERDIM / 2;
// 如果(x, y)仍处于棋子范围内则返回true
// CHECKERDIM / 2为半径
return (cox - x) * (cox - x) + (coy - y) * (coy - y) <= CHECKERDIM / 2 * CHECKERDIM / 2;
}
public void mouseReleased(MouseEvent e) {
// 当鼠标按键被释放时,如果inDrag已经为true,
// 则将其置为false(用来表明不在拖动过程中)
if (inDrag) inDrag = false;
}
});
// 向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if (inDrag) {
// 计算棋子新的原点(棋子矩形的左上角)
int tmpox = e.getX() - relx;
int tmpoy = e.getY() - rely;
// 如果棋子(至少是棋子的一部分)没有被
// 移出棋盘,则将之前计算的原点
// (tmpox, tmpoy)赋值给永久性的原点(ox, oy),
// 并且刷新显示区域(此时的棋子已经位于新坐标上)
if (tmpox >= boardx && tmpoy >= boardy && tmpox + CHECKERDIM <= boardx + BOARDDIM && tmpoy + CHECKERDIM <= boardy + BOARDDIM) {
ox = tmpox;
oy = tmpoy;
repaint();
}
}
}
});
}
public void paint(Graphics g) {
// 在棋子将要被拖动的位置上绘制棋盘
paintCheckerBoard(imG, boardx, boardy);
// 绘制即将被拖动的棋子
paintChecker(imG, ox, oy);
// 绘制图像缓冲的内容
g.drawImage(imBuffer, 0, 0, this);
}
void paintChecker(Graphics g, int x, int y) {
// 设置棋子阴影的颜色
g.setColor(Color.black);
// 绘制棋子的阴影
g.fillOval(x, y, CHECKERDIM, CHECKERDIM);
// 设置棋子颜色
g.setColor(Color.red);
// 绘制棋子
g.fillOval(x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13);
}
void paintCheckerBoard(Graphics g, int x, int y) {
// 绘制棋盘轮廓线
g.setColor(Color.black);
g.drawRect(x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1);
// 绘制棋盘
for (int row = 0; row < 8; row++) {
g.setColor(((row & 1) != 0) ? darkGreen : Color.white);
for (int col = 0; col < 8; col++) {
g.fillRect(x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM);
g.setColor((g.getColor() == darkGreen) ? Color.white : darkGreen);
}
}
}
// AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。
// 该方法从Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除
// 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来
// 防止背景被清除,从而消除了闪烁。
public void update(Graphics g) {
paint(g);
}
}