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; } }
Partager