- 博客(1)
- 资源 (6)
- 收藏
- 关注
转载 VS2008TeamSuite90DayTrial到期解决方法
<br /><br /> 我突然发现vs2008提示“试用版即将到期”。按照惯例找到“控制面板”->“添加或删除程序”->“Microsoft Visual Studio Team System 2008 Team Suite - 简体中文”,单击“更改/删除”。弹出了vs2008安装程序界面,但是进度条走到一半的时候,它提示“安装组件失败,取消安装”。我没遇到过这种情况,所以一时不知所措。<br /><br />解决方法:<br />在“添加或删除程序”中选择“显示更新”,这时你会看到在“Mic
2011-02-14 11:08:00
709
自己用java制作贪吃蛇小游戏
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class snate extends JFrame implements KeyListener,Runnable
{
JLabel j;
Canvas j1;
public static final int canvasWidth = 200;
public static final int canvasHeight = 300;
public static final int nodeWidth = 10;
public static final int nodeHeight = 10;
//SnakeModel se=null;
//222222
//
boolean[][] matrix;
LinkedList nodeArray = new LinkedList();//表
Node food;//节点
int maxX;
int maxY;
int direction = 2;
boolean running = false;
int timeInterval = 200;
double speedChangeRate = 0.75;
boolean paused = false;
int score = 0;
int countMove = 0;
// UP and DOWN should be even
// RIGHT and LEFT should be odd
public static final int UP = 2;
public static final int DOWN = 4;
public static final int LEFT = 1;
public static final int RIGHT = 3;
snate()
{
super();
//setSize(500,400);
Container c=getContentPane();
j=new JLabel("Score:");
c.add(j,BorderLayout.NORTH);
j1=new Canvas();
j1.setSize(canvasWidth+1,canvasHeight+1);
j1.addKeyListener(this);
c.add(j1,BorderLayout.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
JLabel j2;
j2 = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);
p1.add(j2, BorderLayout.NORTH);
j2 = new JLabel("ENTER or R or S for start;", JLabel.CENTER);
p1.add(j2, BorderLayout.CENTER);
j2 = new JLabel("SPACE or P for pause",JLabel.CENTER);
p1.add(j2, BorderLayout.SOUTH);
c.add(p1,BorderLayout.SOUTH);
addKeyListener(this);
pack();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// begin();
//
//2222222
//
this.gs = gs;
this.maxX = maxX;
this.maxY = maxY;
// initial matirx
matrix = new boolean[maxX][];
for(int i=0; i<maxX; ++i){
matrix[i] = new boolean[maxY];
Arrays.fill(matrix[i],false);
}
// initial the snake
int initArrayLength = maxX > 20 ? 10 : maxX/2;
for(int i = 0; i < initArrayLength; ++i){
int x = maxX/2+i;
int y = maxY/2;
nodeArray.addLast(new Node(x, y));
matrix[x][y] = true;
}
food = createFood();
matrix[food.x][food.y] = true;
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
{
//se.changeDirection(SnakeModel.UP);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
//se.changeDirection(SnakeModel.DOWN);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
//se.changeDirection(SnakeModel.LEFT);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
//se.changeDirection(SnakeModel.RIGHT);
}
if(e.getKeyCode()==KeyEvent.VK_R||e.getKeyCode()==KeyEvent.VK_S||e.getKeyCode()==KeyEvent.VK_ENTER)
{
}
}
public void keyTyped(KeyEvent e)
{}
public void keyReleased(KeyEvent e)
{}
public void repaint()
{
Graphics g = j1.getGraphics();
//背景
g.setColor(Color.red);
g.fillRect(0,0,canvasWidth,canvasHeight);
//蛇
//g.setColor(Color.BLUE);
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(10,10,10,10);
}
//
//222222
//
public void changeDirection(int newDirection){
if (direction % 2 != newDirection % 2){
direction = newDirection;
}
}
public boolean moveOn(){
Node n = (Node)nodeArray.getFirst();
int x = n.x;
int y = n.y;
switch(direction){
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
if ((0 <= x && x < maxX) && (0 <= y && y < maxY)){
if (matrix[x][y]){
if(x == food.x && y == food.y){
nodeArray.addFirst(food);
int scoreGet = (10000 - 200 * countMove) / timeInterval;
score += scoreGet > 0? scoreGet : 10;
countMove = 0;
food = createFood();
matrix[food.x][food.y] = true;
return true;
}
else
return false;
}
else{
nodeArray.addFirst(new Node(x,y));
matrix[x][y] = true;
n = (Node)nodeArray.removeLast();
matrix[n.x][n.y] = false;
countMove++;
return true;
}
}
return false;
}
public void run(){
running = true;
while (running){
try{
Thread.sleep(timeInterval);
}
catch(Exception e){
break;
}
if(!paused){
if (moveOn()){
gs.repaint();
}
else{
JOptionPane.showMessageDialog(
null,
"you failed",
"Game Over",
JOptionPane.INFORMATION_MESSAGE);
break;
}
}
}
running = false;
}
private Node createFood(){
int x = 0;
int y = 0;
do{
Random r = new Random();
x = r.nextInt(maxX);
y = r.nextInt(maxY);
}while(matrix[x][y]);
return new Node(x,y);
}
public void speedUp(){
timeInterval *= speedChangeRate;
}
public void speedDown(){
timeInterval /= speedChangeRate;
}
public void changePauseState(){
paused = !paused;
}
public String toString(){
String result = "";
for(int i=0; i<nodeArray.size(); ++i){
Node n = (Node)nodeArray.get(i);
result += "[" + n.x + "," + n.y + "]";
}
return result;
}
}
class Node{
int x;
int y;
Node(int x, int y){
this.x = x;
this.y = y;
}
}
public static void main(String[] args)
{
//Graphics g=j1.getGraphics();
snate s=new snate();
//s.draw_something(g);
//s.setVisible(true);
}
}
2008-10-20
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人