Toon posts:

[java] geanimeerde pacman

Pagina: 1
Acties:
  • 47 views sinds 30-01-2008

Verwijderd

Topicstarter
code:
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package pacman;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class pacman_version2 extends Applet {
Pacman pac;
Puntjes[] points;
int score=0;

public void init() {
setBackground(Color.black);
pac = new Pacman(7,7,this);

points = new Puntjes[210];
int aantal=0;
  for (int rows=0; rows<14; rows++) {
    for (int cols=0; cols<15; cols++) {
     points[aantal] = new Puntjes(15+(cols*20), 15+rows*20,this);
    if (aantal<210) {
    aantal++;
    }
    }
  }
  addKeyListener(new inputKeys());
}
 public void start() {
  if (pac == null) {
   if (getGraphics() !=null ) {
     pac = new Pacman(7,7,this);
     pac.start();
   }
  }
 }
public void paint(Graphics g) {
 //border
 g.setColor(Color.gray);
 g.fillRect(0,0,490,5);
 g.fillRect(0,294,490,5);
 g.fillRect(0,0,5,300);
 g.fillRect(310,0,5,300);
 // eind border
 //teken logo
 Image logo = getImage(getDocumentBase(),"images/logo.png");
 g.drawImage(logo,316,10,this);
// teken puntjes
 for(int aant=0; aant<210; aant++) {
  points[aant].drawPoints(g);
 }
 collisions();
}

public void collisions() {
 // check collision with points
 for(int aant=0; aant<210; aant++) {
  if (points[aant].checkCollision(pac)==true) {
   if (points[aant].getVisible()==true) {
    points[aant].setVisible(false);
    score++;
   }
  }
 }
}
class inputKeys extends KeyAdapter implements KeyListener {
    public void keyPressed( KeyEvent e){
      int key = e.getKeyCode();
      switch(key){
        case KeyEvent.VK_DOWN:
          pac.goDown(); collisions();
          break;
        case KeyEvent.VK_UP:
          pac.goUp(); collisions();
          break;
        case KeyEvent.VK_RIGHT:
          pac.goRight(); collisions(); // ga naar rechts
          break;
        case KeyEvent.VK_LEFT:
          pac.goLeft(); collisions(); // ga naar links
          break;
        case KeyEvent.VK_PAGE_UP:
          break;
        case KeyEvent.VK_PAGE_DOWN:
          break;
      }
   }
  }
 void sleep(int millisec){
   try { Thread.sleep( millisec); }
   catch( InterruptedException e){}
 }
}
////////////////////////////////////////////////////////////////
//**************************************************************
////////////////////////////////////////////////////////////////
// puntjes
 class Puntjes {
  private int x,y,width,height;
  private Applet applet;
  private boolean visible;
  private Pacman pacman;
   public Puntjes(int startx,int starty,Applet applet) { //constructor
     this.x = startx;
     this.y = starty;
     this.width = 5;
     this.height = 5;
     this.applet = applet;
     this.visible = true;
   }
   public void drawPoints(Graphics g) {
    Image point = applet.getImage(applet.getDocumentBase(),"images/point.png");
    if (visible==true) {
     g.drawImage(point,x,y,applet);
    }
   }
   // check collision
   public boolean checkCollision(Pacman pacman) {
   boolean retval=false;
    if ((pacman.x<x)&&((pacman.x+pacman.width)>(x+width))) {
     if ((pacman.y<y)&&((pacman.y+pacman.height)>(y+height))) {
      // collision detected
      retval=true;
     }
    }
     return retval;
   }
   // setters
   public void setVisible(boolean set) {
    this.visible = set;
   }
   // getters
   public boolean getVisible() {
    return this.visible;
   }
 }


// geanimeerde pacman
class Pacman extends Thread {
private boolean doorgaan;
private Graphics g, gB;
private Applet applet;
private Image buffer;
private Image[] imageRij;
public int x,y,width,height,speed;
private String richting;

 public Pacman(int startx,int starty, Applet applet) {
  this.g = applet.getGraphics();
  this.doorgaan = true;
  this.applet = applet;
  this.x = startx;
  this.y = starty;
  this.width = 20;
  this.height = 20;
  this.speed = 20;
  this.richting = "right";
  imageRij = new Image[11];
   for(int i=0; i<=10; i++) {
    imageRij[i] = applet.getImage(applet.getDocumentBase(), "images/pac"+richting+"000"+ i + ".png");
    //System.out.print(applet.getDocumentBase() + "images/pac"+richting+"000"+ i + ".png");
   }
 }
 public void  run() {
  int i=0;
  Image frame;
  while (doorgaan) {
   frame = imageRij[i % imageRij.length];
   if (frame !=null) {
      g.drawImage(frame,x,y,applet);
   }
   slaap(30);
   i++;
  }
 }

 private void slaap(int millisec) {
  try {
   Thread.sleep(millisec);
  } catch (InterruptedException e) {}
 }
   // move functies
  public void goRight() { // ga naar rechts
   if (!(x>=287)) {
    this.x +=speed;
    slaap(100);
    richting = "right";
    applet.repaint();
   }
  }
  public void goLeft() { // ga naar links
   if (!(x<=7)) {
    this.x -=speed;
    slaap(100);
    richting = "left";
    applet.repaint();
   }
  }
  public void goUp() { // ga naar boven
   if (!(y<=7)) {
    this.y -=speed;
    slaap(100);
    richting = "up";
    applet.repaint();
   }
  }
  public void goDown() { // ga naar beneden
   if (!(y>=267)) {
    this.y +=speed;
    slaap(100);
    richting = "down";
    applet.repaint();
   }
  }
}


Ik wil mijn pacman geanimeerd op het beeld zien, alleen ik krijg niks te zien.
Kan iemand mij helpen? Ook ideeen over stukken codes, zou ik graag willen horen.

Bij voorbaat dank,

Alex

  • D2k
  • Registratie: Januari 2001
  • Laatst online: 10:19

D2k

nix te zien is nogal kaal
wat zie je niet?
krijg je errors?
wat heb je al aan debug werk gedaan?

Doet iets met Cloud (MS/IBM)


  • whoami
  • Registratie: December 2000
  • Laatst online: 10:21
Ik vind - net als D2k - dat je eerst zelf eens mag uitzoeken wat het probleem juist is. Eens debuggen en kijken waarom het niet werkt enzo.

Dit lijkt imho veel te veel op een 'ik plaats mijn code hier, jullie gaan het fixen' topic. Dat is het trouwens.
Welkom in P&W (FAQ-15/10/2002)
Welkom in P&W -> Quickstart (update 2/10/2002)

https://fgheysels.github.io/


Dit topic is gesloten.