Ecco...
il codice è questo e dovrei riuscire a trasformarlo nello stesso gioco ma con i due giocatori che lavorino su due terminali in rete...
Animation.java
import java.awt.Image;
import java.util.ArrayList;
public class Animation {
private ArrayList scenes;
private int sceneIndex;
private long movieTime;
private long totalTime;
public Animation(){
scenes = new ArrayList();
totalTime = 0;
start();
}
public synchronized void addScene(Image i, long t){
totalTime += t;
scenes.add(new oneScene(i,totalTime));
}
public synchronized void start(){
movieTime = 0;
sceneIndex = 0;
}
public synchronized void update(long timePassed){
if(scenes.size()>1){
movieTime += timePassed;
if(movieTime>=totalTime){
movieTime = 0;
sceneIndex = 0;
}
while(movieTime > getScene(sceneIndex).endTime){
sceneIndex++;
}
}
}
public synchronized Image getImage(){
if(scenes.size()==0)
{
return null;
}else{
return getScene(sceneIndex).pic;
}
}
private oneScene getScene(int x){
return (oneScene)scenes.get(x);
}
private class oneScene{
Image pic;
long endTime;
public oneScene(Image pic,long endTime){
this.pic = pic;
this.endTime = endTime;
}
}
}
Core.java
import java.awt.*;
import java.awt.image.BufferedImage;
public abstract class Core {
private static final DisplayMode modes[] =
{
//new DisplayMode(1920,1080,32,0),
new DisplayMode(1680,1050,32,0),
//new DisplayMode(1280,1024,32,0),
new DisplayMode(800,600,32,0),
new DisplayMode(800,600,24,0),
new DisplayMode(800,600,16,0),
new DisplayMode(640,480,32,0),
new DisplayMode(640,480,24,0),
new DisplayMode(640,480,16,0),
};
private boolean running;
protected ScreenManager sm;
public void stop(){
running = false;
}
public void run(){
try{
init();
gameLoop();
}finally{
sm.restoreScreen();
}
}
public void init(){
sm = new ScreenManager();
DisplayMode dm = sm.findFirstCompatibaleMode(modes);
sm.setFullScreen(dm);
Window w = sm.getFullScreenWindow();
w.setFont(new Font("Arial",Font.PLAIN,20));
w.setBackground(Color.WHITE);
w.setForeground(Color.RED);
w.setCursor(w.getToolkit().createCustomCursor(new BufferedImage(3, 3, BufferedImage.TYPE_INT_ARGB), new Point(0, 0),"null"));
running = true;
}
public void gameLoop(){
long startTime = System.currentTimeMillis();
long cumTime = startTime;
while (running){
long timePassed = System.currentTimeMillis()-cumTime;
cumTime+= timePassed;
update(timePassed);
Graphics2D g = sm.getGraphics();
draw(g);
g.dispose();
sm.update();
try{
Thread.sleep(20);
}catch(Exception ex){}
}
}
public void update(long timePassed){}
public abstract void draw(Graphics2D g);
}
ScreenManager.java
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class ScreenManager {
private GraphicsDevice vc;
public ScreenManager(){
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = e.getDefaultScreenDevice();
}
public DisplayMode[] getCompatibleDisplayModes(){
return vc.getDisplayModes();
}
public DisplayMode findFirstCompatibaleMode(DisplayMode[] modes){
DisplayMode goodModes[] = vc.getDisplayModes();
for(int x = 0; x<modes.length;x++){
for(int y = 0;y<goodModes.length;y++){
if(displayModesMatch(modes[x],goodModes[y])){
return modes[x];
}
}
}
return null;
}
public DisplayMode getCurrentDM(){
return vc.getDisplayMode();
}
public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
return false;
}
if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
return false;
}
if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
return false;
}
return true;
}
public void setFullScreen(DisplayMode dm){
JFrame f = new JFrame();
f.setUndecorated(true);
f.setIgnoreRepaint(true);
f.setResizable(false);
vc.setFullScreenWindow(f);
if(dm != null && vc.isDisplayChangeSupported()){
try{
vc.setDisplayMode(dm);
}catch(Exception ex){}
f.createBufferStrategy(2);
}
}
public Graphics2D getGraphics(){
Window w = vc.getFullScreenWindow();
if(w != null){
BufferStrategy bs = w.getBufferStrategy();
return (Graphics2D)bs.getDrawGraphics();
}
else{
return null;
}
}
public void update(){
Window w = vc.getFullScreenWindow();
if(w != null){
BufferStrategy bs = w.getBufferStrategy();
if(!bs.contentsLost()){
bs.show();
}
}
}
public Window getFullScreenWindow(){
return vc.getFullScreenWindow();
}
public int getWidth(){
Window w = vc.getFullScreenWindow();
if(w != null){
return w.getWidth();
}else{
return 0;
}
}
public int getHeight(){
Window w = vc.getFullScreenWindow();
if(w != null){
return w.getHeight();
}else{
return 0;
}
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if(w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
public BufferedImage createCompatibaleimage(int w, int h, int t){
Window win = vc.getFullScreenWindow();
if(win != null){
GraphicsConfiguration gc = win.getGraphicsConfiguration();
return gc.createCompatibleImage(w,h,t);
}else{
return null;
}
}
}
yourClass.java
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
public class yourclass extends Core implements KeyListener, MouseListener,
MouseMotionListener {
int centrex1 = 40;
int centrey1 = 40;
int centrex2 = 600;
int centrey2 = 440;
int currentDirection1 = 1;
int currentDirection2 = 3;
int moveAmount = 5;
ArrayList<Integer> pathx1 = new ArrayList();
ArrayList<Integer> pathy1 = new ArrayList();
ArrayList<Integer> pathx2 = new ArrayList();
ArrayList<Integer> pathy2 = new ArrayList();
public void init() {
super.init();
Window w = sm.getFullScreenWindow();
w.addKeyListener(this);
w.addMouseListener(this);
w.addMouseMotionListener(this);
}
public static void main(String[] args) {
new yourclass().run();
}
public void draw(Graphics2D g) {
switch(currentDirection1){
case 0:
if (centrey1>0){
centrey1-=moveAmount;
} else {
centrey1 = sm.getHeight();
}
break;
case 1:
if (centrex1 < sm.getWidth()){
centrex1+=moveAmount;
} else {
centrex1 = 0;
}
break;
case 2:
if (centrey1 < sm.getHeight()){
centrey1+=moveAmount;
} else {
centrey1 = 0;
}
break;
case 3:
if (centrex1>0){
centrex1-=moveAmount;
} else {
centrex1 = sm.getWidth();
}
break;
}
switch(currentDirection2){
case 0:
if (centrey2>0){
centrey2-=moveAmount;
} else {
centrey2 = sm.getHeight();
}
break;
case 1:
if (centrex2 < sm.getWidth()){
centrex2+=moveAmount;
} else {
centrex2 = 0;
}
break;
case 2:
if (centrey2 < sm.getHeight()){
centrey2+=moveAmount;
} else {
centrey2 = 0;
}
break;
case 3:
if (centrex2>0){
centrex2-=moveAmount;
} else {
centrex2 = sm.getWidth();
}
break;
}
for (int x = 0;x<pathx1.size();x++){
if (((centrex1 == pathx1.get(x)) && (centrey1 == pathy1.get(x))) || ((centrex2 == pathx2.get(x)) && (centrey2 == pathy2.get(x))) || ((centrex1 == pathx2.get(x)) && (centrey1 == pathy2.get(x))) || ((centrex2 == pathx1.get(x)) && (centrey2 == pathy1.get(x)))){
System.exit(0);
}
}
pathx1.add(centrex1);
pathy1.add(centrey1);
pathx2.add(centrex2);
pathy2.add(centrey2);
g.setColor(Color.BLACK);
g.fillRect(0, 0, sm.getWidth(), sm.getHeight());
for (int x = 0;x<pathx1.size();x++){
g.setColor(Color.green);
g.fillRect(pathx1.get(x), pathy1.get(x), 10, 10);
g.setColor(Color.red);
g.fillRect(pathx2.get(x), pathy2.get(x), 10, 10);
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (currentDirection1 != 2){
currentDirection1 = 0;
}
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (currentDirection1 != 0){
currentDirection1 = 2;
}
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (currentDirection1 != 3){
currentDirection1 = 1;
}
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (currentDirection1 != 1){
currentDirection1 = 3;
}
}
if (e.getKeyCode() == KeyEvent.VK_W){
if (currentDirection2 != 2){
currentDirection2 = 0;
}
} else if (e.getKeyCode() == KeyEvent.VK_S) {
if (currentDirection2 != 0){
currentDirection2 = 2;
}
} else if (e.getKeyCode() == KeyEvent.VK_D) {
if (currentDirection2 != 3){
currentDirection2 = 1;
}
} else if (e.getKeyCode() == KeyEvent.VK_A) {
if (currentDirection2 != 1){
currentDirection2 = 3;
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent arg0) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
}