Witam.
Muszę napisać na zaliczenie grafiki komputerowej grę "kółko i krzyżyk".
Wymagania:
Gra w kółko i krzyżyk napisana w Javie:
- krasnal wtacza kółka na ich pozycje na planszy
- żółw wpycha krzyżyki
Kółka i krzyżyki rysowane wektorowo. Figury obracają się podczas przesuwania (obrót musi być widoczny, czyli w przypadku kółek wypełnienie jakimś gradientem lub teksturą). Co do krzyżyków, to zamiast obrotu mogą być skalowalne, powiększać się wraz ze zbliżaniem do swojej pozycji na planszy.
Niestety java nie jest moją mocną stroną, dlatego chciałem się do was zwrócić o jakąkolwiek pomoc. Najbardziej zależy mi chociażby na tym, aby postacie przemieszczały się w wyznaczony obszar planszy.
I może ktoś potrafi wytłumaczyć, jak wykorzystać transformacje afiniczne do obrotu i ruchu figur ???? (niestety na ćwiczeniach nie było o tym mowy).
Muszę cokolwiek mieć na niedzielę, ale niestety nawał pracy i nauki z innych przedmiotów nie pozwoliły mi porządnie przysiąść do tego zadania.
Będę ogromnie wdzięczny za jakąkolwiek pomoc.
Pod spodem mój kod :-(
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.util.*;
public class TicTacToe extends JFrame
{
private static class GameBoard extends Canvas
{
private static final int EMPTY = 0,X_PIECE = 1,O_PIECE = 2;
private int[][] gameGrid = new int[3][3]; // Game grid
private boolean xTurn; // Is it X's turn?
private Random random; // Random generator
private boolean gameRunning; // Is a game in progress?
private int gameResult; // Result of game
Image image =new ImageIcon("images/leprechaun1.jpg").getImage();
int x1=0,y1=430;
Image image1 = new ImageIcon("images/turtle.jpg").getImage();
int x2=510, y2=430;
AffineTransform at = new AffineTransform();
/* Handles mouse input in the window.*/
private class GameListener implements MouseListener
{
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e)
{
// Check the location of the click
int xPos = e.getPoint().x;
int yPos = e.getPoint().y;
// Check to see if it is in game board
if(xPos > 120 && yPos > 120 && xPos < 120+120*3 && yPos < 120+120*3)
{
// Check to see if game is running
if(gameRunning == false)
{
// New game
wipeGrid();
gameRunning = true;
repaint();
return;
}
// Check for an empty place
if(gameGrid[xPos/120-1][yPos/120-1] != EMPTY)
return; // Not empty, can't place piece
// Place a piece for the current turn and then alternate
if(xTurn)
{
gameGrid[xPos/120-1][yPos/120-1] = X_PIECE;
xTurn = false;
}
else
{
gameGrid[xPos/120-1][yPos/120-1] = O_PIECE;
xTurn = true;
}
// Check for a winning game
gameResult = gameOver();
if(gameResult != 0)
{
// Game has ended!
gameRunning = false;
}
// Cause an update
repaint();
}
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
/* Creates a new game board.*/
public GameBoard()
{
// Set the size and background color
setPreferredSize(new Dimension(640,550));
setBackground(Color.WHITE);
// Add mouse listener
addMouseListener(new GameListener());
// Create a new random
random = new Random();
// Wipe grid
wipeGrid();
// Set game status
gameRunning = true;
}
/* Clears the game board of all pieces.
This function also sets a random turn for the next player.*/
public void wipeGrid()
{
// Wipes the entire grid
for(int y = 0;y < 3;y++)
for(int x = 0;x < 3;x++)
gameGrid[x][y] = EMPTY;
// Set player
if(random.nextInt(100) < 50)
xTurn = true;
else
xTurn = false;
}
/* Checks to see if the game is over.
@return If the game is over or not. 0 = Not over, 1 = X wins, 2 = O wins, 3 = tie
*/
int gameOver()
{
// Check for a match
for(int x = 0;x < 3;x++) // Rows
if(gameGrid[x][0] == gameGrid[x][1] && gameGrid[x][1] == gameGrid[x][2])
return gameGrid[x][0];
for(int y = 0;y < 3;y++) // Columns
if(gameGrid[0][y] == gameGrid[1][y] && gameGrid[1][y] == gameGrid[2][y])
return gameGrid[0][y];
// Diagonal 1
if(gameGrid[0][0] == gameGrid[1][1] && gameGrid[1][1] == gameGrid[2][2])
return gameGrid[0][0];
if(gameGrid[2][0] == gameGrid[1][1] && gameGrid[0][2] == gameGrid[1][1])
return gameGrid[2][0];
// Check for tie
for(int y = 0;y < 3;y++)
for(int x = 0;x < 3;x++)
if(gameGrid[x][y] == 0)
return 0; // Not a tie because there is an empty space
// The game is a tie
return 3;
}
/* Paints the game board.*/
public void paint(Graphics gDC)
{
Graphics2D g = (Graphics2D) gDC;
// Clear old stuff out
g.clearRect(0,0,getWidth(),getHeight());
// Draw lines
g.setStroke(new BasicStroke(10));
g.setColor(Color.BLACK);
for(int y = 1;y < 3;y++)
g.drawLine(120,y*120+120,120+120*3,y*120+120);
for(int x = 1;x < 3;x++)
g.drawLine(x*120+120,120,x*120+120,120+120*3);
//images
g.drawImage(image, x1, y1, this);
g.drawImage(image1, x2, y2, this);
// Draw piece
for(int y = 0;y < 3;y++)
{
for(int x = 0;x < 3;x++)
{
if(gameGrid[x][y] == X_PIECE)
{
//krzyżyki się rozsypały, poprawić
g.setColor(Color.BLUE);
g.drawLine(120+x*120,120+y*120,120+x*120+120,120+y*120+120);
g.drawLine(120+120+x*120,120+y*120,120+x*120,120+y*120+120);
}
if(gameGrid[x][y] == O_PIECE)
{
g.setColor(Color.RED);
g.drawOval(125+x*120,125+y*120,110,110);
for (int i=0; i<320; i++)
{
y1 -= 1;
g.drawImage(image, x1,y1, this);
try{
Thread.sleep(5);
}catch(Exception ex){}
}
for (int i=0;i<320;i++)
{
y1 += 1;
g.drawImage(image, x1,y1, this);
try{
Thread.sleep(5);
}catch(Exception ex){}
}
}
}
}
// Check for turns
g.setColor(Color.BLACK);
Font font = new Font("Arial", Font.BOLD, 20);
g.setFont(font);
if(gameRunning)
{
// Turn message
if(xTurn)
g.drawString("It is player X's turn.",20,40);
else
g.drawString("It is player O's turn.",20,40);
}
else
{
// End message
if(gameResult == X_PIECE)
g.drawString("Player X won!",20,40);
if(gameResult == O_PIECE)
g.drawString("Player O won!",20,40);
if(gameResult == 3)
g.drawString("Tie game!",20,40);
// Prompt message
g.drawString("Click to start a new game.",20,60);
}
}
}
/* Starts the game of Tic Tac Toe.
@param args This is ignored.*/
public static void main(String[] args)
{
// Create the window for tic tac toe
TicTacToe ticTacToe = new TicTacToe();
ticTacToe.setTitle("Tic Tac Toe");
ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add the game board
GameBoard gameBoard = new GameBoard();
ticTacToe.add(gameBoard);
// Pack and show
ticTacToe.pack();
ticTacToe.setLocationRelativeTo(null);
ticTacToe.setVisible(true);
}
}