Bonjour,
Hier en TP j'ai commencé à apprendre à utiliser les fonctions de bases de libgdx comme create(), render(), resize(int, int), pause(), resume(), et dispose() afin de créer un jeu Pacman au bout d'un semestre. Cependant, étant débutant j'aurais besoin qu'on m'explique clairement à quoi sert chaque fonction, qui appelle qui où et quand, etc... Voici le code des 7 classes présentes (la plupart des classes ne font que quelques lignes et encore, des fonctions vides) :
MainActivity.java :
PacmanGame.java :
Code : 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 package fr.univ_lorraine; import android.os.Bundle; import com.badlogic.gdx.backends.android.AndroidApplication; import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; import fr.univ_lorraine.pacman.PacmanGame; public class MainActivity extends AndroidApplication { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration(); cfg.useGL20 = true; initialize(new PacmanGame(), cfg); } }
World.java :
Code : 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 package fr.univ_lorraine.pacman; import com.badlogic.gdx.Game; import fr.univ_lorraine.pacman.model.World; import fr.univ_lorraine.pacman.view.GameScreen; public class PacmanGame extends Game { private World world; private GameScreen gamescreen; public void create() { world = new World(); gamescreen = new GameScreen(world); this.setScreen(gamescreen); } public void render() { } public void resize(int width, int height) { } public void pause() { } public void resume() { } public void dispose() { } }
GameScreen.java :
Code : 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 package fr.univ_lorraine.pacman.model; public class World { private int nbL, nbC; private Pacman pac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 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1}, {1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} }; public World() { nbL = 28; nbC = 31; pacman = new Pacman(48, 48); // positions initiales (taille bloc) } public int getNbL() { return nbL; } public int getNbC() { return nbC; } public Pacman getPacman() { return pacman; } public int getMaze(int i, int j) { return maze[i][j]; } public boolean estUnBlock(int i, int j) { return maze[i][j] == 1; } }
GameElement.java :
Code : 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 package fr.univ_lorraine.pacman.view; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import fr.univ_lorraine.pacman.model.Block; import fr.univ_lorraine.pacman.model.World; public class GameScreen implements Screen { private World world; private SpriteBatch spriteBatch; public GameScreen(World world) { this.world = world; } public void create() { this.show(); } public void render(float delta) { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); Block tmp = new Block(0, 0); spriteBatch.begin(); for(int i = 0; i < world.getNbL(); i++) { for(int j = 0; j < world.getNbC(); j++) { if(world.estUnBlock(i, j)) { spriteBatch.draw(tmp.getTexture(), i * 48, j * 48, 48, 48); // positions des blocs * taille bloc } } } spriteBatch.draw(world.getPacman().getTexture(), world.getPacman().getX(), world.getPacman().getY(), 13, 13); spriteBatch.end(); } public void resize(int width, int height) { } public void pause() { } public void resume() { } public void dispose() { } public void show() { spriteBatch = new SpriteBatch(); } public void hide() { } }
Pacman.java :
Code : 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 package fr.univ_lorraine.pacman.model; import com.badlogic.gdx.graphics.Texture; public class GameElement { private int x, y; protected int largeur, hauteur; protected Texture texture; public GameElement(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } public int getLargeur() { return largeur; } public int getHauteur() { return hauteur; } public Texture getTexture() { return texture; } public void setX(int nouveauX) { x = nouveauX; } public void setY(int nouveauY) { y = nouveauY; } public void setLargeur(int nouvelleLargeur) { largeur = nouvelleLargeur; } public void setHauteur(int nouvelleHauteur) { hauteur = nouvelleHauteur; } public void setTexture(Texture nouvelleTexture) { texture = nouvelleTexture; } }
Block.java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package fr.univ_lorraine.pacman.model; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; public class Pacman extends GameElement { public Pacman(int x, int y) { super(x, y); largeur = 13; hauteur = 13; texture = new Texture(Gdx.files.internal("data/pacmanLeft.png")); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package fr.univ_lorraine.pacman.model; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; public class Block extends GameElement { public Block(int x, int y) { super(x, y); largeur = 48; hauteur = 48; texture = new Texture(Gdx.files.internal("data/bloc.png")); } }
Mon problème se situerait plutôt dans les classes censées afficher les objets (GameScreen.java et PacmanGame.java). J'ai essayé de coder assez clairement mais en exécutant j'obtiens un écran noir alors que je suis censé obtenir un labyrinthe (voire maze[][] de World), ou même de simples carrés, histoire de ne pas avoir un écran noir... Quelqu'un pourrait m'aider à comprendre ce qui ne va pas ?
Merci d'avance.
Partager