Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

getPixel in Javac (pls. help me. i need this ASAP)

mikeljymsmikeljyms Member Posts: 6
We were assigned to make a Pacman-style program using Java. we are already able to move the pacman object but we couldn't make it stop when it touched the border or when it hits a pellet. hope any of you'd reply.... thanks.


mikeljyms---
«1

Comments

  • IllcoIllco Member Posts: 382
    : We were assigned to make a Pacman-style program using Java. we are already able to move the pacman object but we couldn't make it stop when it touched the border or when it hits a pellet. hope any of you'd reply.... thanks.
    :
    :
    : mikeljyms---
    :

    Please please don't do this by checking the pixel value... Check if the position of good ole Pacman is near the wall and calculate stuff with the width of Pacman.
  • mikeljymsmikeljyms Member Posts: 6
    : : We were assigned to make a Pacman-style program using Java. we are already able to move the pacman object but we couldn't make it stop when it touched the border or when it hits a pellet. hope any of you'd reply.... thanks.
    : :
    : :
    : : mikeljyms---
    : :
    :
    : Please please don't do this by checking the pixel value... Check if the position of good ole Pacman is near the wall and calculate stuff with the width of Pacman.
    :
    ---->
    We sort of assigned each point in the frame into an array of our Point class with x, y and color as its members.

    buffer.setColor(myBlue);
    if(up){
    buffer.drawLine(col1,row,col1+25,row);
    for(int m=col1;m<col1+25;i++){point[m][row].color=Color.red;}
    }
    //it no longer paints when it reaches this part.
    if(down){
    buffer.drawLine(col1,row+27,col1+25,row+27);
    for(int m=col1;m<col1+25;i++){point[m][row+27].color=Color.red;}
    }

    buffer is an image. One of my groupmates tried pixelGrabber but we didn't really understand how to use it. We're at a lost right now and we would be passing it on tomorrow 5:30 pm. (im from the philippines)

    mikeljyms------
  • IllcoIllco Member Posts: 382
    I don't pixelgrabber either but I suppose the working is explained in the API. Furthermore, I don't know where the point[i][j] array is for, but it holds Color values. Can't you use those?
    My sarcastic remark was that this is a hideous way of programming a game: you're storing your game information (positions etc) in the video memory! That's weird and leads to problems like these.
  • mikeljymsmikeljyms Member Posts: 6
    : I don't pixelgrabber either but I suppose the working is explained in the API. Furthermore, I don't know where the point[i][j] array is for, but it holds Color values. Can't you use those?
    : My sarcastic remark was that this is a hideous way of programming a game: you're storing your game information (positions etc) in the video memory! That's weird and leads to problems like these.
    :
    -------->
    sorry, we're just beginning with java. and this is only the first time that we have to program a game, most especially one that has to move and stop.... (you know what i mean).

    so, what would you suggest?

    point is an instantiation of a class we defined. its suppose to have, the color (well we we're running out of options), at a certain point i and j. i being the x component and j being the y.

    thanks.

  • IllcoIllco Member Posts: 382
    I'd suggest to keep the position of Pacman in a pair of integers (x,y) but I'm sure you don't have enough time for that anymore.

    I wouldn't know how to fix this quickly; I'd guess you'd best get that PixelGrabber stuff working.
  • mikeljymsmikeljyms Member Posts: 6
    Thanks a lot for your help. I really mean it. Though i won't be able to get the grade i want, but i'll pass. i just hope this code's understandable.

    ===> Here's our code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;

    //Global Constants that would be used in this program.
    interface ChomperConstants{

    //Colors
    Color myRed = new Color(255,0,0); //powerPellet
    Color myGreen = new Color(0,255,0); //weakChomper
    Color myBlue = new Color(0,0,255); //mazeBorder
    Color myPurple = new Color(127,0,127); //normalPellet
    Color myDarkGreen = new Color(0,127,127); //player1Flag
    Color myWhite = new Color(255,255,255); //player2Flag
    Color myYellow = new Color(255,255,0); //strongChomper
    Color myOrange = new Color(255,127,0); //outerBorder
    Color myBlack = new Color(0,0,0);

    //Maze upper right-hand coordinate values of the two mazes
    int y=50, x1=50, x2=350;

    //Player 1 control keys
    int up1=87; //W key
    int down1=83; //S key
    int left1=65; //A key
    int right1=68; //D key

    //Player 2 control keys
    int up2=38; //up key
    int down2=40; //down key
    int left2=37; //left key
    int right2=39; //right key

    //Pause key
    int pause=80; //the pause key

    int inc=2; //movement of chomper
    int delay=1; //chomper speed delay

    }//interface ChomperConstants


    //This class draws two chompers. This is also supposed to check the movement of
    //the chomper, etc.
    //This is the main class.


    class Pixel{
    int x,y;
    Color color;

    Pixel(int x, int y, Color color){
    this.x=x;
    this.y=y;
    this.color=color;
    }
    }

    public class Chomp1 extends JFrame implements ActionListener, KeyListener, ChomperConstants{
    static Chomp1 chomper;
    int a1,b1,a2,b2,key;
    int c1,d1,c2,d2;
    boolean win1, win2, strong1, hit, out;
    int move,move1,move2;
    JPanel panel1;
    JButton bPlay,bSettings,bExit;
    Image image;
    Graphics buffer;

    Pixel[][] point;


    String tempMaze[] = new String[10];
    char maze[][] = new char[10][15];
    private int row,col;
    boolean up, down, left, right;

    Chomp1(){
    super("Super Chomper");
    setSize(650,528);
    a1=a2=165;
    b1=b2=d1=d2=293;
    c1=c2=465;
    win1=win2=false;
    out=false;
    hit=false;
    move=move1=up1;
    move2=up2;
    show();
    image = createImage(645, 500);
    buffer = image.getGraphics();

    point = new Pixel[645][500];
    for(int i=0;i<point.length ; i++){
    for(int j=0;j<point[i].length;j++){
    point[i][j] = new Pixel(i,j,Color.black);
    }
    }

    class WindowDestroyer extends WindowAdapter{
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    }

    Container contentPane = getContentPane();
    contentPane.setBackground(myBlack);
    panel1 = new JPanel();
    bPlay = new JButton("Play");
    bPlay.addActionListener(this);
    panel1.add(bPlay);
    bSettings = new JButton("Settings");
    bSettings.addActionListener(this);
    panel1.add(bSettings);
    bExit = new JButton("Exit");
    bExit.addActionListener(this);
    panel1.add(bExit);
    contentPane.add(panel1,BorderLayout.SOUTH);

    addKeyListener(this);

    int rdm = (int) (Math.random())*2;
    rdm/= 2;
    if(rdm==0){strong1=true;}
    else{strong1=false;}


    tempMaze[0]="EJCJHOOGOOEHEH";
    tempMaze[1]="GOGOGOOGOOGFIG";
    tempMaze[2]="AJDJPJJDJCDCJB";
    tempMaze[3]="GOOOGEHEJDHAHG";
    tempMaze[4]="FJJJPIFPCHFBFB";
    tempMaze[5]="EJJJPHEPDIEBEB";
    tempMaze[6]="GOOOGFIFJCIAIG";
    tempMaze[7]="AJCJPJJCJDCDJB";
    tempMaze[8]="GOGOGOOGOOGEHG";
    tempMaze[9]="FJDJIOOGOOFIFI";

    for(int i=0;i<tempMaze.length;i++)
    for(int j=0;j<tempMaze[i].length();j++)
    maze[i][j]=tempMaze[i].charAt(j);

    buffer.setColor(myOrange);
    buffer.drawRect(0,0,643,483);
    buffer.setColor(myBlack);
    buffer.fillRect(0,0,643,483);

    for(int j=0,row=y;j<14;j++,row+=27){
    for(int i=0,col1=x1,col2=x2;i<10;i++,col1+=25,col2+=25){
    switch(maze[i][j]){
    case 'A': up=true;
    down=false;
    left=false;
    right=false;
    break;
    case 'B': up=false;
    down=true;
    left=false;
    right=false;
    break;
    case 'C': up=false;
    down=false;
    left=true;
    right=false;
    break;
    case 'D': up=false;
    down=false;
    left=false;
    right=true;
    break;
    case 'E': up=true;
    down=false;
    left=true;
    right=false;
    break;
    case 'F': up=true;
    down=false;
    left=false;
    right=true;
    break;
    case 'G': up=true;
    down=true;
    left=false;
    right=false;
    break;
    case 'H': up=false;
    down=true;
    left=true;
    right=false;
    break;
    case 'I': up=false;
    down=true;
    left=false;
    right=true;
    break;
    case 'J': up=false;
    down=false;
    left=true;
    right=true;
    break;
    case 'K': up=true;
    down=true;
    left=true;
    right=false;
    break;
    case 'L': up=true;
    down=true;
    left=false;
    right=true;
    break;
    case 'M': up=true;
    down=false;
    left=true;
    right=true;
    break;
    case 'N': up=false;
    down=true;
    left=true;
    right=true;
    break;
    case 'O': up=true;
    down=true;
    left=true;
    right=true;
    break;
    case 'P': up=false;
    down=false;
    left=false;
    right=false;
    break;
    }

    buffer.setColor(myBlue);
    if(up){
    buffer.drawLine(col1,row,col1+25,row);
    for(int m=col1;m<col1+25;i++){
    point[m][row].color=Color.red;
    }
    }
    //its at this point where paint stops working.
    if(down){
    buffer.drawLine(col1,row+27,col1+25,row+27);
    for(int m=col1;m<col1+25;i++){
    point[m][row+27].color=Color.red;
    }
    }
    if(left){
    buffer.drawLine(col1,row,col1,row+27);
    for(int m=row;m<row+27;i++){
    point[col1][m].color=Color.red;
    }
    }
    if(right){
    buffer.drawLine(col1+25,row,col1+25,row+27);
    for(int m=row;i<row+27;i++){
    point[col+25][m].color=Color.red;
    }
    }

    buffer.setColor(myPurple);
    if(maze[i][j]!='O'){
    buffer.fillOval(col1+10,row+12,5,5);
    }

    buffer.setColor(myBlue);
    if(up){
    buffer.drawLine(col2,row,col2+25,row);
    for(int m=col2;m<col2+25;i++){
    point[m][row].color=myBlue;
    }
    }
    if(down){
    buffer.drawLine(col2,row+27,col2+25,row+27);
    // for(int m=col2;m<col2+25;i++){
    // point[m][row+27].color=myBlue;
    // }
    }
    if(left){
    buffer.drawLine(col2,row,col2,row+27);
    // for(int m=row;m<row+27;i++){
    // point[col2][m].color=myBlue;
    // }
    }
    if(right){
    buffer.drawLine(col2+25,row,col2+25,row+27);
    // for(int m=row;m<row+27;i++){
    // point[col+25][m].color=myBlue;
    // }
    }

    //This draws the normal pellets.
    buffer.setColor(myPurple);
    if(maze[i][j]!='O'){
    buffer.fillOval(col2+10,row+12,5,5);
    }
    }
    }

    //This draws the power pellets at each corner.
    row=423;
    col=300;
    buffer.setColor(myRed);
    buffer.fillOval(x1+10,y+10,10,10);
    buffer.fillOval(x1+10,row-12,10,10);
    buffer.fillOval(col-15,y+10,10,10);
    buffer.fillOval(col-15,row-12,10,10);

    col=600;
    buffer.setColor(myRed);
    buffer.fillOval(x2+10,y+10,10,10);
    buffer.fillOval(x2+10,row-12,10,10);
    buffer.fillOval(col-15,y+10,10,10);
    buffer.fillOval(col-15,row-12,10,10);



    if(strong1){buffer.setColor(myYellow);}
    else{buffer.setColor(myGreen);}
    buffer.fillOval(a1,b1,15,15);

    if(strong1){buffer.setColor(myYellow);}
    else{buffer.setColor(myYellow);}
    buffer.fillOval(c1,d1,15,15);

    }// constructor

    public void keyPressed(KeyEvent e){
    move = e.getKeyCode();
    }

    public void keyTyped(KeyEvent e){
    }

    public void keyReleased(KeyEvent e){
    }

    public void paint(Graphics g){
    //if(hit==false){
    if((move==up1)||(move==down1)||(move==left1)||(move==right1)){move1=move;}
    if((move==up2)||(move==down2)||(move==left2)||(move==right2)){move2=move;}

    if(move1==up1){
    b2-=inc;
    }if(move1==down1){
    b2+=inc;
    }if(move1==left1){
    a2-=inc;
    }if(move1==right1){
    a2+=inc;
    }
    if(move2==up2){
    d2-=inc;
    }if(move2==down2){
    d2+=inc;
    }if(move2==left2){
    c2-=inc;
    }if(move2==right2){
    c2+=inc;
    }


    buffer.setColor(myBlack);
    buffer.fillOval(a1,b1,15,15);

    buffer.setColor(myYellow);
    buffer.fillOval(a2,b2,15,15);

    buffer.setColor(myBlack);
    buffer.fillOval(c1,d1,15,15);

    buffer.setColor(myYellow);
    buffer.fillOval(c2,d2,15,15);

    g.drawImage(image, 0, 0, this);

    repaint(delay);

    //if(point[a2][b2].color==myBlue){hit=true;}

    a1=a2;
    b1=b2;
    c1=c2;
    d1=d2;

    //}



    }

    public void update(Graphics g){
    paint(g);
    }

    public void actionPerformed(ActionEvent e){
    if(e.getSource() == bPlay){

    }
    if(e.getSource() == bSettings){

    }
    if(e.getSource() == bExit){
    System.exit(0);
    }
    }

    public static void main(String args[]){
    chomper = new Chomp1();
    chomper.addWindowListener(
    new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    }
    );
    chomper.setVisible(true);

    }
    }

  • IllcoIllco Member Posts: 382
    It's quite hard to read your code because it is messy, but this is what I got from it. You use the variables [b]a1 b1 c1 d1[/b] for holding the positions or something. At least this what you update in the [b]paint()[/b] method. You could, before updating the positions, check wether the new position is valid (according to the playing field). So, for example, if [b]a1 < 0[/b] (left outside the playing field), then the move is not allowed. Don't move there and don't update.
  • mikeljymsmikeljyms Member Posts: 6
    : It's quite hard to read your code because it is messy, but this is what I got from it. You use the variables [b]a1 b1 c1 d1[/b] for holding the positions or something. At least this what you update in the [b]paint()[/b] method. You could, before updating the positions, check wether the new position is valid (according to the playing field). So, for example, if [b]a1 < 0[/b] (left outside the playing field), then the move is not allowed. Don't move there and don't update.
    :
    ------>
    i know its messy...
    but what about the part when pacman is in the maze. when would we know when pacman should stop. should we get every coordinate just to know if it should stop?

    Thanks a lot.... Your a big help.

  • IllcoIllco Member Posts: 382
    : i know its messy...
    : but what about the part when pacman is in the maze. when would we know when pacman should stop. should we get every coordinate just to know if it should stop?
    :
    : Thanks a lot.... Your a big help.
    :
    :

    I doubt wether I'm helping or not, but it's fine with me. I don't really get your question, but let me state the solution more generally:

    In SOME way you keep where Pacman either IS or IS GOING (by what I know it's in a1,b1,c1,d1 in the paint() method). Check if the next step is allowed. I don't know how to put it in more clearly.
  • mikeljymsmikeljyms Member Posts: 6
    : : i know its messy...
    : : but what about the part when pacman is in the maze. when would we know when pacman should stop. should we get every coordinate just to know if it should stop?
    : :
    : : Thanks a lot.... Your a big help.
    : :
    : :
    :
    : I doubt wether I'm helping or not, but it's fine with me. I don't really get your question, but let me state the solution more generally:
    :
    : In SOME way you keep where Pacman either IS or IS GOING (by what I know it's in a1,b1,c1,d1 in the paint() method). Check if the next step is allowed. I don't know how to put it in more clearly.
    :
    ----->
    you've helped me more than you'll ever know. enks!
«1
Sign In or Register to comment.