Comment dessiner une table de jeu en java
Fermé
mahouch
-
17 juil. 2012 à 19:20
HackTrack Messages postés 618 Date d'inscription vendredi 26 juillet 2002 Statut Membre Dernière intervention 13 juillet 2013 - 21 juil. 2012 à 10:13
HackTrack Messages postés 618 Date d'inscription vendredi 26 juillet 2002 Statut Membre Dernière intervention 13 juillet 2013 - 21 juil. 2012 à 10:13
A voir également:
- Comment dessiner une table de jeu en java
- Table ascii - Guide
- Dans la table des matières de ce document, le chapitre 6 et ses 2 sections n'apparaissent pas. trouvez l'erreur dans la structure du document et corrigez-la. mettez à jour la table des matières. quel est le mot formé par les lettres en majuscules de la table des matières après sa mise à jour ? - Forum Word
- Table des matières word - Guide
- Jeux java itel ✓ - Forum Jeux vidéo
- Java runtime - Télécharger - Langages
1 réponse
HackTrack
Messages postés
618
Date d'inscription
vendredi 26 juillet 2002
Statut
Membre
Dernière intervention
13 juillet 2013
972
21 juil. 2012 à 10:13
21 juil. 2012 à 10:13
Salut,
voici un début. A toi de l'adapter.
PS: j'ai inclus tout dans une seule classe (la vue, le modèle et le contrôle) par facilité pour toi. Mais il vaut mieux séparer la vue du modèle et du contrôleur.
;-)
HackTrack
voici un début. A toi de l'adapter.
PS: j'ai inclus tout dans une seule classe (la vue, le modèle et le contrôle) par facilité pour toi. Mais il vaut mieux séparer la vue du modèle et du contrôleur.
package hacktrack.games;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BillardDemo extends JFrame {
private Set<Ball> balls;
private Dimension billardSize;
private int ballSize;
private int billardBorderWidth;
private Rectangle playZone;
private JPanel billard;
public BillardDemo() {
super("Demo billard");
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
}
private void init() {
Container c = getContentPane();
billardSize = new Dimension(640, 360);
billardBorderWidth = 20;
ballSize=20;
playZone = new Rectangle(billardBorderWidth, billardBorderWidth,
(int) billardSize.getWidth() - billardBorderWidth * 2,
(int) billardSize.getHeight() - billardBorderWidth * 2);
balls = new HashSet<BillardDemo.Ball>();
balls.add(createBall(Color.RED));
balls.add(createBall(Color.WHITE));
balls.add(createBall(Color.WHITE));
billard = new JPanel() {
@Override
public void paint(Graphics g) {
g.setColor(new Color(128, 64, 0));
g.fillRect(0, 0, (int) billardSize.getWidth(),
(int) billardSize.getHeight());
g.setColor(new Color(0, 128, 0));
g.fillRect((int) playZone.getX(), (int) playZone.getY(),
(int) playZone.getWidth(), (int) playZone.getHeight());
if (balls != null) {
for (Ball ball : balls) {
g.setColor(new Color(0,83,0));
g.fillOval(ball.getX()+2, ball.getY()+2, ballSize, ballSize);
g.setColor(ball.getColor());
g.fillOval(ball.getX(), ball.getY(), ballSize, ballSize);
}
}
}
};
billard.setPreferredSize(billardSize);
c.add(billard);
new MoverThread().start();
}
private Ball createBall(Color color) {
int x = (int) (playZone.getX()+ballSize + Math.random() * playZone.getWidth()-ballSize);
int y = (int) (playZone.getY()+ballSize + Math.random() * playZone.getHeight()-ballSize);
double direction = Math.random() * Math.PI * 2;
double speed = Math.random() * 2 + 2;
return new Ball(color, x, y, direction, speed);
}
class Ball {
private Color color;
private int x;
private int y;
private double direction;
private double speed;
public Ball(Color color, int x, int y, double direction, double speed) {
this.color = color;
this.x = x;
this.y = y;
this.direction = direction;
this.speed = speed;
}
public Color getColor() {
return color;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public double getDirection() {
return direction;
}
public double getSpeed() {
return speed;
}
}
class MoverThread extends Thread {
private boolean isRunning;
public MoverThread() {
isRunning = false;
}
@Override
public void run() {
isRunning = true;
while (isRunning) {
moveBalls();
billard.repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void moveBalls() {
for (Ball ball : balls) {
int newX = (int) (ball.getX() + Math.cos(ball.getDirection())
* ball.getSpeed());
int newY = (int) (ball.getY() + Math.sin(ball.getDirection())
* ball.getSpeed());
if (playZone.contains(newX, newY) &&playZone.contains(newX+ballSize, newY+ballSize)) {// && playZone.contains(newX-ballSize, newY-ballSize)
ball.setX((int) newX);
ball.setY((int) newY);
}else{
//Changer la direction de la balle en focntion du bord qu'elle a touché.
//Un bel exercice que je te laisse faire... ;-) Hacktrack
}
}
}
}
public static void main(String[] args) {
BillardDemo demo = new BillardDemo();
demo.pack();
demo.setVisible(true);
}
}
;-)
HackTrack