IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

avec Java Discussion :

Pong rien ne bouge


Sujet :

avec Java

  1. #1
    Membre averti Avatar de Neolex
    Homme Profil pro
    Recherche emploi Securité informatique
    Inscrit en
    Avril 2011
    Messages
    243
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Recherche emploi Securité informatique

    Informations forums :
    Inscription : Avril 2011
    Messages : 243
    Points : 333
    Points
    333
    Par défaut Pong rien ne bouge
    Bonjour à tous , voilà j'essaye de créer un pong en java ...
    Je ne l'ai pas fini mais je bloque sur quelque chose , les deux rackets et la balle s'affichent bien mais rien ne bouge ...

    J'ai pourtant bien crée et lancé les méthodes Move de chaque objet ...
    Voici mon code :

    gameBoard :
    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    package pong;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
    @SuppressWarnings("serial")
    public class GameBoard extends JPanel implements ActionListener , KeyListener , FocusListener	{
     
    	Ball ball;
    	Racket racketg , racketd ;
    	Player playerg , playerd;
     
    	Timer timer;
     
     
     
    	GameFrame gfrm ;
     
    	public GameBoard(GameFrame fr)
    	{
    		gfrm = fr ;
     
    		ball = new Ball(300, 300);
     
    		racketd = new Racket(575, 300);
    		racketg = new Racket(0,300);
     
    		timer = new Timer(25, this);
     
    		this.addKeyListener(this);
    		gfrm.addFocusListener(this);
    		this.setFocusable(true);
     
    		this.setBackground(Color.black);
    	}
     
    	public void paint(Graphics g)
    	{
    		super.paint(g);
     
    		racketd.Draw(g);
    		racketg.Draw(g);
     
    		ball.Draw(g);
    	}
     
     
     
    	@Override
    	public void keyPressed(KeyEvent arg0) {
    		if(arg0.getKeyCode() == KeyEvent.VK_Z)
    		{
    			racketg.up = true;
    		}
    		if(arg0.getKeyCode() == KeyEvent.VK_S)
    		{
    			racketg.down = true;
    		}
    		if(arg0.getKeyCode() == KeyEvent.VK_UP)
    		{
    			racketd.up = true;
    		}
    		if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
    		{
    			racketd.down = true;
    		}
     
    		repaint();
    	}
     
    	@Override
    	public void keyReleased(KeyEvent arg0) {
    		if(arg0.getKeyCode() == KeyEvent.VK_Z)
    		{
    			racketg.up = false;
    		}
    		if(arg0.getKeyCode() == KeyEvent.VK_S)
    		{
    			racketg.down = false;
    		}
    		if(arg0.getKeyCode() == KeyEvent.VK_UP)
    		{
    			racketd.up = false;
    		}
    		if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
    		{
    			racketd.down = false;
    		}
     
     
    		repaint();
     
    	}
     
    	@Override
    	public void keyTyped(KeyEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		ball.Move();
    		racketd.Move();
    		racketg.Move();
     
    		repaint();
     
    	}
     
    	@Override
    	public void focusGained(FocusEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void focusLost(FocusEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
     
     
    }

    Ball
    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    package pong;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
     
    public class Ball {
     
    	int x , vx =6;
    	int y,	vy =-3 ;
     
    	public Ball(int Startx,int Starty)
    	{
    		this.x = Startx;
    		this.y = Starty;
    	}
     
    	public void Draw(Graphics g)
    	{
    		g.setColor(Color.white);
    		g.fillOval(x, y, 20, 20);
    	}
     
    	public void Move()
    	{
    		x+=vx;
    		y+=vy;
    	}
     
    	public Rectangle getBounds()
    	{
    		Rectangle Box = new Rectangle(x, y, 20, 20);
    		return Box;
    	}
     
     
     
    	/* Getters and setter */
     
     
    	public int getX() {
    		return x;
    	}
     
    	public void setX(int x) {
    		this.x = x;
    	}
     
    	public int getVx() {
    		return vx;
    	}
     
    	public void setVx(int vx) {
    		this.vx = vx;
    	}
     
    	public int getY() {
    		return y;
    	}
     
    	public void setY(int y) {
    		this.y = y;
    	}
     
    	public int getVy() {
    		return vy;
    	}
     
    	public void setVy(int vy) {
    		this.vy = vy;
    	}
     
     
    }

    Racket
    Code java : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
    package pong;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
     
    public class Racket {
    	int x , y ;
    	int vitesse = 10 ;
    	public boolean up = false ;
    	public boolean down = false;
     
     
     
    	public Racket(int startx,int starty)
    	{
    		this.x = startx;
    		this.y = starty;
    	}
     
    	public void Draw(Graphics g)
    	{
    		g.setColor(Color.white);
    		g.fillRect(x, y, 25, 80);
    	}
     
    	public Rectangle getBounds()
    	{
    		Rectangle Box = new Rectangle(x, y, 25, 80);
    		return Box ;
    	}
     
    	public void Move()
    	{
    		if(up)
    		{
    			y -= vitesse;
    		}
    		if(down)
    		{
    			y += vitesse;
    		}
    	}
     
     
    	// getter and setters 
    	public int getY() {
    		return y;
    	}
     
    	public void setY(int y) {
    		this.y = y;
    	}
     
    }

  2. #2
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 074
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 074
    Points : 7 978
    Points
    7 978
    Par défaut
    Normal, tu as oublié de démarrer le timer :

    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  3. #3
    Membre averti Avatar de Neolex
    Homme Profil pro
    Recherche emploi Securité informatique
    Inscrit en
    Avril 2011
    Messages
    243
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : Recherche emploi Securité informatique

    Informations forums :
    Inscription : Avril 2011
    Messages : 243
    Points : 333
    Points
    333
    Par défaut
    Je me sens vraiment idiot là ...

    Merci beaucoup , désolé du dérangement ...

  4. #4
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 074
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 074
    Points : 7 978
    Points
    7 978
    Par défaut
    Aucun souci, au plaisir.
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. postback et rien ne bouge
    Par topolino dans le forum ASP.NET
    Réponses: 4
    Dernier message: 26/08/2009, 09h19
  2. Fonction qui s'active lorsqu'un Form bouge
    Par Xavier dans le forum C++Builder
    Réponses: 3
    Dernier message: 22/05/2003, 12h54
  3. Ne rien afficher
    Par rockbiker dans le forum DirectX
    Réponses: 3
    Dernier message: 20/05/2003, 18h02
  4. [debutante] [JDBComboBox]rien à l'affichage
    Par Lina dans le forum JBuilder
    Réponses: 3
    Dernier message: 22/11/2002, 13h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo