java小游戲課程設計
❶ 求Java課程設計—小游戲(含源代碼)
http://hi..com/srxboys/item/8ce4743da1adc991c2cf29c4
Tank——坦克大戰(簡潔版)源代碼-------(此文檔是自己在韓順平教程總結而來)
*功能:1.防止敵人的坦克重疊運動
*(決定把判斷是否碰撞的函數寫到EnemyTank類)
*2.可以分關
*2.1(做一個開始的Panel,它是一個空的)
*2.2開始字體閃爍
*3.可以在玩游戲的時候,暫停和繼續
*3.1當用戶點擊暫停時,子彈的速度和坦克速度設為0,並讓坦克的方向
*不要發生變化。
*4.可以記錄玩家的成績
*4.1用文件流的方式(小游戲)[大游戲是用的資料庫<cs,bs>結構,三國]
*4.2單寫一個記錄類,完成對玩家的記錄
*4.3先完成保存共擊毀了多少輛敵人坦克的功能
*4.4存檔退出遊戲,可以記錄當時的敵人的坦克坐標,並可以恢復
*5.java如何操作聲音文件
*/
❷ 用JAVA編寫一個小游戲
前天寫的猜數字游戲,yongi控制猜測次數,有詳細解析,用黑窗口可以直接運行,
我試驗過了,沒問題
import javax.swing.Icon;
import javax.swing.JOptionPane;
public class CaiShuZi4JOptionPane {
/**
* @param args
*/
public static void main(String[] args) {
Icon icon = null;
boolean bl = false;
int put = 0;
int c = (int) (((Math.random())*100)+1); //獲取一個1-100的隨機數
System.out.println("你獲取的隨機數是:"+c); //列印你的隨機數字
String str1 = (String) JOptionPane.showInputDialog(null,"請輸入你的猜測數字(1-100): ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入"); //第一次輸入你的猜測數字
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次游戲"); //如果你點取消那麼本次游戲結束
}else{
bl = num(str1); //判斷是輸入的是不是數字或者是整數
if(true==bl){ //如果是數字的話進入與隨機數比較的程序
System.out.println("你輸入的數字是:"+str1); //列印你輸入的數字
put = Integer.valueOf(str1);
for(int i = 4;i > 0;i--){ //i是你可以猜測的次數
if(put==c){
JOptionPane.showMessageDialog(null, "恭喜你猜對了,正確答案是:"+c+"。"); //如果你猜對了就直接結束循環
break;
}else if(put>c){ //如果輸大了就讓你再次從新輸入
str1 = (String) JOptionPane.showInputDialog(null,"你的輸入過大。你還有"+i+"次機會,請重新輸入: ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次輸入");
break;
}else{
bl =num(str1);
if(true==bl){
put = Integer.valueOf(str1);
}else{
JOptionPane.showMessageDialog(null, "你的輸入不正確,請重新輸入");
}
}
}else if(put<c){ //如果你輸小了也讓你從新輸入
str1 = (String) JOptionPane.showInputDialog(null,"你的輸入過小。你還有"+i+"次機會,請重新輸入: ","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
if(str1==null){
JOptionPane.showMessageDialog(null, "你已經取消了本次輸入");
break;
}else{
bl =num(str1);
if(true==bl){
put = Integer.valueOf(str1);
}else{
JOptionPane.showMessageDialog(null, "你的輸入不正確,請重新輸入");
}
}
}
}
}else if(bl==false){ //這個 是你第一次如果填寫的不是數字的話也會結束本次游戲
JOptionPane.showMessageDialog(null, "請您下次按要求填寫。本次游戲結束");
}
if(true==bl && c!=put){ //如果你i次都沒猜對,那麼就直接告訴你這個數十什麼
JOptionPane.showMessageDialog(null, "很遺憾你沒能猜對,這個數字是:"+c+".");
}
}
}
public static boolean num(String value){ //一個靜態方法,判斷你輸入的是不是數字
try {
Integer.parseInt(value);
return true;
} catch (Exception e) {
return false;
}
}
}
❸ JAVA小游戲程序代碼
這個是比較有名的那個煙花,不知道你有沒有用:
建個工程,以為類即可
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class Fireworks extends Applet implements MouseListener,Runnable
{
int x,y;
int top,point;
/**
*對小程序進行變數和顏色的初始化。
*/
public void init()
{
x = 0;
y = 0;
//設置背景色為黑色
setBackground(Color.black);
addMouseListener(this);
}
public void paint(Graphics g)
{
}
/**
*使該程序可以作為應用程序運行。
*/
public static void main(String args[]) {
Fireworks applet = new Fireworks();
JFrame frame = new JFrame("TextAreaNew");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(800,400);
applet.init();
applet.start();
frame.setVisible(true);
}
/**
*程序主線程,對一個煙花進行繪制。
*/
public void run()
{
//變數初始化
Graphics g1;
g1 = getGraphics();
int y_move,y_click,x_click;
int v;
x_click = x;
y_click = y;
y_move = 400;
v = 3;
int r,g,b;
while(y_move > y_click)
{
g1.setColor(Color.black);
g1.fillOval(x_click,y_move,5,5);
y_move -= 5;
r = (((int)Math.round(Math.random()*4321))%200)+55;
g = (((int)Math.round(Math.random()*4321))%200)+55;
b = (((int)Math.round(Math.random()*4321))%200)+55;
g1.setColor(new Color(r,g,b));
g1.fillOval(x_click,y_move,5,5);
for(int j = 0 ;j<=10;j++)
{
if(r>55) r -= 20;
if(g>55) g -= 20;
if(b>55) b -=20;
g1.setColor(new Color(r,g,b));
g1.fillOval(x_click,y_move+j*5,5,5);
}
g1.setColor(Color.black);
g1.fillOval(x_click,y_move+5*10,5,5);
try
{
Thread.currentThread().sleep(v++);
} catch (InterruptedException e) {}
}
for(int j=12;j>=0;j--)
{
g1.setColor(Color.black);
g1.fillOval(x_click,y_move+(j*5),5,5);
try
{
Thread.currentThread().sleep((v++)/3);
} catch (InterruptedException e) {}
}
y_move = 400;
g1.setColor(Color.black);
while(y_move > y_click)
{
g1.fillOval(x_click-2,y_move,9,5);
y_move -= 5;
}
v = 15;
for(int i=0;i<=25;i++)
{
r = (((int)Math.round(Math.random()*4321))%200)+55;
g = (((int)Math.round(Math.random()*4321))%200)+55;
b = (((int)Math.round(Math.random()*4321))%200)+55;
g1.setColor(new Color(r,g,b));
g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i);
if(i<23)
{
g1.drawOval(x_click-3*(i+1),y_click-3*(i+1),6*(i+1),6*(i+1));
g1.drawOval(x_click-3*(i+2),y_click-3*(i+2),6*(i+2),6*(i+2));
}
try
{
Thread.currentThread().sleep(v++);
} catch (InterruptedException e) {}
g1.setColor(Color.black);
g1.drawOval(x_click-3*i,y_click-3*i,6*i,6*i);
}
}
/**
*對滑鼠事件進行監聽。
*臨聽其滑鼠按下事件。
*當按下滑鼠時,產生一個新線程。
*/
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
Thread one;
one = new Thread(this);
one.start();
one = null;
}
/**
*實現MouseListener接中的方法。為一個空方法。
*/
public void mouseReleased(MouseEvent e)
{
}
/**
*實現MouseListener接中的方法。為一個空方法。
*/
public void mouseEntered(MouseEvent e)
{
}
/**
*實現MouseListener接中的方法。為一個空方法。
*/
public void mouseExited(MouseEvent e)
{
}
/**
*實現MouseListener接中的方法。為一個空方法。
*/
public void mouseClicked(MouseEvent e)
{
}
}
❹ 用java web小游戲源代碼。期末結課老師讓做,急用,謝了
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class MainClass extends JFrame {
ControlSnake control;
Toolkit kit;
Dimension dimen;
public static void main(String[] args) {
new MainClass("my snake");
}
public MainClass(String s) {
super(s);
control = new ControlSnake();
control.setFocusable(true);
kit = Toolkit.getDefaultToolkit();
dimen = kit.getScreenSize();
add(control);
setLayout(new BorderLayout());
setLocation(dimen.width / 3, dimen.height / 3);// dimen.width/3,dimen.height/3
setSize(FWIDTH, FHEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public static final int FWIDTH = 315;
public static final int FHEIGHT = 380;
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class ControlSnake extends JPanel implements ActionListener {
Random rand;
ArrayList<Point> list, listBody;
String str, str1;
static boolean key;
int x, y, dx, dy, fx, fy, flag;
int snakeBody;
int speed;
public ControlSnake() {
snakeBody = 1;
str = "上下左右方向鍵控制 P鍵暫停...";
str1 = "現在的長度為:" + snakeBody;
key = true;
flag = 1;
speed = 700;
rand = new Random();
list = new ArrayList<Point>();
listBody = new ArrayList<Point>();
x = 5;
y = 5;
list.add(new Point(x, y));
listBody.add(list.get(0));
dx = 10;
dy = 0;
fx = rand.nextInt(30) * 10 + 5;// 2
fy = rand.nextInt(30) * 10 + 5;// 2
setBackground(Color.BLACK);
setSize(new Dimension(318, 380));
final Timer time = new Timer(speed, this);
time.start();
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 37) {
dx = -10;
dy = 0;
} else if (e.getKeyCode() == 38) {
dx = 0;
dy = -10;
} else if (e.getKeyCode() == 39) {
dx = 10;
dy = 0;
} else if (e.getKeyCode() == 40) {
dx = 0;
dy = 10;
} else if (e.getKeyCode() == 80) {
if (flag % 2 == 1) {
time.stop();
}
if (flag % 2 == 0) {
time.start();
}
flag++;
}
}
});
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 400, 400);
g.setColor(Color.DARK_GRAY);
g.drawLine(3, 3, 305, 3);
g.drawLine(3, 3, 3, 305);
g.drawLine(305, 3, 305, 305);
g.drawLine(3, 305, 305, 305);
g.setColor(Color.PINK);
for (int i = 0; i < listBody.size(); i++) {
g.fillRect(listBody.get(i).x, listBody.get(i).y, 9, 9);
}
g.fillRect(x, y, 9, 9);
g.setColor(Color.ORANGE);
g.fillRect(fx, fy, 9, 9);
g.setColor(Color.DARK_GRAY);
str1 = "現在的長度為:" + snakeBody;
g.drawString(str, 10, 320);
g.drawString(str1, 10, 335);
}
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
if (makeOut() == false) {
JOptionPane.showMessageDialog(null, "重新開始......");
speed = 700;
snakeBody = 1;
x = 5;
y = 5;
list.clear();
list.add(new Point(x, y));
listBody.clear();
listBody.add(list.get(0));
dx = 10;
dy = 0;
}
addPoint(x, y);
if (x == fx && y == fy) {
speed = (int) (speed * 0.8);//速度增加參數
if (speed < 200) {
speed = 100;
}
fx = rand.nextInt(30) * 10 + 5;// 2
fy = rand.nextInt(30) * 10 + 5;// 2
snakeBody++;// 2
} // 2
repaint();
}
public void addPoint(int xx, int yy) {
// 動態的記錄最新發生的50步以內的移動過的坐標
// 並畫出最新的snakeBody
if (list.size() < 100) {//蛇身長度最長為100
list.add(new Point(xx, yy));
} else {
list.remove(0);
list.add(new Point(xx, yy));
}
if (snakeBody == 1) {
listBody.remove(0);
listBody.add(0, list.get(list.size() - 1));
} else {
listBody.clear();
if (list.size() < snakeBody) {
for (int i = list.size() - 1; i > 0; i--) {
listBody.add(list.get(i));
}
} else {
for (int i = list.size() - 1; listBody.size() < snakeBody; i--) {
listBody.add(list.get(i));
}
}
}
}
public boolean makeOut() {
if ((x < 3 || y < 3) || (x > 305 || y > 305)) {
return false;
}
for (int i = 0; i < listBody.size() - 1; i++) {
for (int j = i + 1; j < listBody.size(); j++) {
if (listBody.get(i).equals(listBody.get(j))) {
return false;
}
}
}
return true;
}
}
/*貪吃蛇代碼*/
❺ 用java製作一個小游戲 教學
static Scanner in =new Scanner(System.in);
public static int aaa(){
int c = 0;
while(true){
try {
if(c>999&&c<10000){
break;
}else{
// System.out.println("請輸入4位整數");
c= in.nextInt();
if(c>999&&c<10000){
break;
}else{
System.out.println("輸入有誤,請重新輸入4位整數");
}
}
} catch (Exception e) {
System.out.println("請輸入整數");
c= in.nextInt();
}
}
//in.close();
return c;
}
public static void cai(){
//Scanner sa =new Scanner(System.in);
int haoma=(int)(Math.random()*10000);
if(haoma<999)
{
haoma = Integer.parseInt(String.valueOf(haoma)+"0");
}
System.out.println(haoma);
System.out.println("請輸入一位4位整數");
int aa = 0;
while(true){
aa= aaa();
String pd=String.valueOf(aa);
if(pd.length()!=4){
aa = aaa();
}else{
break;
}
}
while(true){
if(aa==haoma){
System.out.println("你猜對了,可以去買彩票了");
}else{
System.out.println("抱歉 , 你猜錯了");
}
System.out.println("是否繼續1繼續 其他結束");
try {
int ss = in.nextInt();
if(ss==1){
cai();
}else{
break;
}
} catch (Exception e) {
in.close();
break;
}
}
}
public static void main(String[] args) {
System.out.println("歡迎來到猜號小游戲");
cai();
System.out.println("over");
}
❻ 用JAVA編一個小游戲
我也想學
教教我吧
我是手游忠實的愛好者
有什麼游戲我也可以試玩哦
❼ 求一個簡單又有趣的JAVA小游戲代碼
那你就自己做個猜數字好了
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("請輸入一個100以內的整數:");
for (int i=0;i<=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shu>num)
System.out.println("輸入的數大了,輸小點的!");
else if (shu<num)
System.out.println("輸入的數小了,輸大點的!");
else {
System.out.println("恭喜你,猜對了!");
if (i<=2)
System.out.println("你真是個天才!");
else if (i<=6)
System.out.println("還將就,你過關了!");
else if (i<=8)
System.out.println("但是你還……真笨!");
else
System.out.println("你和豬沒有兩樣了!");
break;}
}
}
}