Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //importação das classes:
- newgame novo;
- mainmenu menu;
- PrintWriter output;
- BufferedReader reader;
- String line;
- // Parâmetros do labirinto:
- int nCol, nLin; // nº de linhas e de colunas
- int tamanho = 50; // tamanho (largura e altura) das células do labirinto
- int espacamento = 2; // espaço livre entre células
- float margemV, margemH; // margem livre na vertical e na horizontal para assegurar que as células são quadrangulares
- color corObstaculos = color(0, 200 , 250); // cor de fundo dos obstáculos
- double pontos[][];
- //Variaveis para aumentar dificuldade do jogo:
- int pontuacaoatual=0;
- int pontuacaomaxima=0;
- int nivel=1;
- // Posicao e tamanho do Pacman:
- float px, py, pRaio;
- // Posição dos fantasmas:
- float g1x, g1y, g2x, g2y, g3x, g3y, g4x, g4y;
- // Velocidade do pacman:
- float vx, vy;
- // Velocidade dos fantasmas:
- float vg1x, vg1y, vg2x, vg2y, vg3x, vg3y, vg4x, vg4y;
- float cont=-0.52;
- //Declarar a Fonte:
- PFont fonte;
- //Arrancar Menu:
- boolean mouseReleasedgame=false;
- boolean mouseReleasedexit=false;
- boolean menucalled=true;
- //Colisões Pacman-Fantasmas:
- boolean collisions=false;
- int vidas=3;
- void setup() {
- // Definir o tamanho da janela; notar que size() não aceita variáveis.
- size(1100, 570);
- background(0);
- //Criar a fonte:
- fonte = createFont("Arial",16,true);
- nCol = (int)770/tamanho;
- nLin = (int)height/tamanho;
- pontos = new double[nCol][nLin];
- // Assegurar que nº de linhas e nº de colunas é maior ou igual a 3
- assert nCol >= 5 && nLin >= 5;
- // Determinar margens para limitar a área útil do jogo
- margemV = (770 - nCol * tamanho) / 2.0;
- margemH = (height - nLin * tamanho) / 2.0;
- // Inicializar o Pacman
- px = centroX(8);
- py = centroY(8);
- pRaio = (tamanho - 20) / 2;
- // Inicializar os fantasmas
- // Fantasma Verde
- g1x = centroX(1)+2;
- g1y = centroY(1);
- // Fantasma Vermelho
- g2x = centroX(6)+2;
- g2y = centroY(5);
- // Fantasma Azul
- g3x = centroX(8);
- g3y = centroY(5);
- //specifies speeds in X and Y directions
- vx = 0;
- vy = 0;
- for(int a=0;a<nCol;a++){
- for(int b=0;b<nLin;b++){
- pontos[a][b]=0;
- }
- }
- //Declaração das classes importadas:
- novo = new newgame();
- menu = new mainmenu();
- //
- importHighscore();
- }
- void draw(){
- background(0);
- //arrancar o menu e as suas opções
- arrancarmenu();
- //println(mouseX,"++++++",mouseY);
- //println(vidas);
- }
- void arrancarmenu(){
- if (menucalled){
- menu.menudesign();
- }
- if (mousePressed==true && mouseX>502 && mouseY>243 && mouseX<642 && mouseY<273){
- mouseReleasedgame=true;
- }
- if (mousePressed==true && mouseX>487 && mouseY>358 && mouseX<603 && mouseY<394){
- mouseReleasedexit=true;
- }
- if(mouseReleasedgame==true){
- menucalled=false;
- novo.desenharLabirinto();
- novo.desenharPontos();
- novo.pacman();
- novo.fantasmas();
- novo.keypressed();
- novo.desenharBarraLateral();
- novo.BarraLateraldoJogo();
- novo.pontuacoeseniveis();
- novo.colisoes();
- novo.updateHighscore();
- }
- if(mouseReleasedexit==true){
- exit();
- }
- }
- class mainmenu{
- void menudesign(){
- //titulo
- textFont(fonte,96);
- fill(232, 239, 40);
- text("PACMAN",350,100);
- //new game
- textFont(fonte,26);
- fill(232, 239, 40);
- text("New Game",470,270);
- //scores
- textFont(fonte,26);
- fill(232, 239, 40);
- text("Scores",493,330);
- //exit
- textFont(fonte,26);
- fill(232, 239, 40);
- text("Exit",510,390);
- }
- }
- class newgame{
- //Funções responsaveis pelo jogo:
- void pacman(){
- float angle1, angle2, angle3, angle4, angle5, angle6, angle7, angle8;
- angle1=2.25;
- angle2=7.35;
- angle3=5.15;
- angle4=10.45;
- angle5=3.67;
- angle6=8.90;
- angle7=0.52;
- angle8=5.76;
- if (px==centroX(8) && py==centroY(8) && vx==0 && vy==0){
- desenharPacman(angle7,angle8);
- }
- if (keyCode==DOWN){
- desenharPacman(angle1,angle2);
- }
- if (keyCode==UP){
- desenharPacman(angle3,angle4);
- }
- if (keyCode==LEFT){
- desenharPacman(angle5,angle6);
- }
- if (keyCode==RIGHT){
- desenharPacman(angle7,angle8);
- }
- if (keyCode==CONTROL){
- desenharPacman(angle7,angle8);
- }
- }
- void desenharPacman(float mouthAngle,float mouthAngle2) {
- fill(232, 239, 40);
- ellipseMode(RADIUS);
- //noStroke();
- cont+=0.05;
- if (cont>=0.52){
- cont=-0.52;}
- arc(px, py, pRaio, pRaio, mouthAngle-cont, mouthAngle2+cont);
- //fill(0);
- //triangle(px, py, px+pRaio/1.828427125, py-pRaio/1.828427125, px+pRaio/1.828427125, py+pRaio/1.828427125);
- }
- void fantasmas(){
- // Fantasma Verde
- fantasma1();
- if(g1y==centroY(1)){
- vg1x=2;}
- if(g1y==centroY(1) && g1x==centroX(nCol)){
- vg1x=0;}
- if(g1x==centroX(nCol)){
- vg1y=2;}
- if(g1x==centroX(nCol) && g1y==centroY(nLin)){
- vg1y=0;}
- if(g1y==centroY(nLin)){
- vg1x=-2;}
- if(g1y==centroY(nLin) && g1x==centroX(1)){
- vg1x=0;}
- if(g1x==centroX(1)){
- vg1y=-2;}
- if(g1x==centroX(1) && g1y==centroY(1)){
- vg1y=0;}
- g1x+=vg1x;
- g1y+=vg1y;
- // Fantasma Vermelho
- fantasma2();
- if(g2y==centroY(5)){
- vg2x=2;}
- if(g2y==centroY(5) && g2x==centroX(10)){
- vg2x=0;}
- if(g2x==centroX(10)){
- vg2y=2;}
- if(g2x==centroX(10) && g2y==centroY(7)){
- vg2y=0;}
- if(g2y==centroY(7)){
- vg2x=-2;}
- if(g2y==centroY(7) && g2x==centroX(6)){
- vg2x=0;}
- if(g2x==centroX(6)){
- vg2y=-2;}
- if(g2x==centroX(6) && g2y==centroY(5)){
- vg2y=0;}
- g2x+=vg2x;
- g2y+=vg2y;
- // Fantasma Azul
- fantasma3();
- if(g3x==centroX(nCol)){
- vg3y=2;}
- if(g3x==centroX(nCol) && g3y==centroY(4)){
- vg3y=0;}
- if (g3y==centroY(4) && g3x>centroX(12)){
- vg3x=-2;}
- if (g3y==centroY(4) && g3x==centroX(13)){
- vg3x=0;}
- if (g3x==centroX(13) && g3y>centroY(2)){
- vg3y=2;}
- if(g3y==centroY(5) && g3x==centroX(13)){
- vg3y=0;}
- if(g3y==centroY(5) && g3x>centroX(11)){
- vg3x=-2;}
- if(g3x==centroX(12) && g3y==centroY(5)){
- vg3x=0;}
- if(g3x==centroX(12) && g3y>centroY(4)){
- vg3y=2;}
- if(g3y==centroY(6) && g3x==centroX(12)){
- vg3y=0;}
- if(g3y==centroY(6) && g3x>centroX(8)){
- vg3x=-2;}
- if(g3x==centroX(10) && g3y==centroY(6)){
- vg3x=0;}
- if(g3x==centroX(10) && g3y>centroY(4)){
- vg3y=2;}
- if(g3x==centroX(10) && g3y==centroY(7)){
- vg3y=0;}
- if(g3y==centroY(7) && g3x>centroX(5)){
- vg3x=-2;}
- if(g3y==centroY(7) && g3x==centroX(7)){
- vg3x=0;}
- if(g3x==centroX(7) && g3y>centroY(6)){
- vg3y=2;}
- if(g3x==centroX(7) && g3y==centroY(9)){
- vg3y=0;}
- if(g3y==centroY(9) && g3x>centroX(2)){
- vg3x=-2;}
- if(g3x==centroX(4) && g3y==centroY(9)){
- vg3x=0;}
- if(g3x==centroX(4) && g3y>centroY(8)){
- vg3y=2;}
- if(g3x==centroX(4) && g3y==centroY(11)){
- vg3y=0;}
- if(g3y==centroY(nLin)){
- vg3x=-2;}
- if(g3y==centroY(nLin) && g3x==centroX(1)){
- vg3x=0;}
- if(g3x==centroX(1)){
- vg3y=-2;}
- if(g3y==centroY(8) && g3x==centroX(1)){
- vg3y=0;}
- if(g3y==centroY(8) && g3x<centroX(4)){
- vg3x=2;}
- if(g3y==centroY(8) && g3x==centroX(3)){
- vg3x=0;}
- if(g3x==centroX(3) && g3y<centroY(9)){
- vg3y=-2;}
- if(g3x==centroX(3) && g3y==centroY(7)){
- vg3y=0;}
- if(g3y==centroY(7) && g3x<centroX(5)){
- vg3x=2;}
- if(g3y==centroY(7) && g3x==centroX(4)){
- vg3x=0;}
- if(g3x==centroX(4) && g3y<centroY(8)){
- vg3y=-2;}
- if(g3x==centroX(4) && g3y==centroY(6)){
- vg3y=0;}
- if(g3y==centroY(6) && g3x<centroX(8)){
- vg3x=2;}
- if(g3x==centroX(6) && g3y==centroY(6)){
- vg3x=0;}
- if(g3x==centroX(6) && g3y==centroY(6)){
- vg3y=-2;}
- if(g3y==centroY(5) && g3x==centroX(6)){
- vg3y=0;}
- if(g3y==centroY(5) && g3x<centroX(11)){
- vg3x=2;}
- if(g3y==centroY(5) && g3x==centroX(9)){
- vg3x=0;}
- if(g3x==centroX(9) && g3y<centroY(6)){
- vg3y=-2;}
- if(g3y==centroY(3) && g3x==centroX(9)){
- vg3y=0;}
- if(g3y==centroY(3) && g3x<centroY(14)){
- vg3x=2;}
- if(g3y==centroY(3) && g3x==centroX(12)){
- vg3x=0;}
- if(g3x==centroX(12) && g3y<centroY(4)){
- vg3y=-2;}
- if(g3x==centroX(12) && g3y==centroY(1)){
- vg3y=0;}
- if(g3y==centroY(1)){
- vg3x=2;}
- if(g3y==centroY(1) && g3x==centroX(nCol)){
- vg3x=0;}
- g3x+=vg3x;
- g3y+=vg3y;
- }
- void fantasma1(){
- fill(0, 255, 0);
- ellipseMode(RADIUS);
- ellipse(g1x, g1y-4, 14, 12);
- ellipse(g1x-10.5, g1y+9.5, 3.4, 6);
- ellipse(g1x-3.5, g1y+9.5, 3.4, 6);
- ellipse(g1x+10.5, g1y+9.5, 3.4, 6);
- ellipse(g1x+3.5, g1y+9.5, 3.4, 6);
- rectMode(RADIUS);
- rect(g1x, g1y+4, 14, 6);
- fill(255);
- ellipse(g1x-5, g1y-6, 4, 4);
- ellipse(g1x+5, g1y-6, 4, 4);
- fill(0);
- ellipse(g1x-5, g1y-6, 1, 1);
- ellipse(g1x+5, g1y-6, 1, 1);
- }
- void fantasma2(){
- fill(255, 0, 0);
- ellipseMode(RADIUS);
- ellipse(g2x, g2y-4, 14, 12);
- ellipse(g2x-10.5, g2y+9.5, 3.4, 6);
- ellipse(g2x-3.5, g2y+9.5, 3.4, 6);
- ellipse(g2x+10.5, g2y+9.5, 3.4, 6);
- ellipse(g2x+3.5, g2y+9.5, 3.4, 6);
- rectMode(RADIUS);
- rect(g2x, g2y+4, 14, 6);
- fill(255);
- ellipse(g2x-5, g2y-6, 4, 4);
- ellipse(g2x+5, g2y-6, 4, 4);
- fill(0);
- ellipse(g2x-5, g2y-6, 1, 1);
- ellipse(g2x+5, g2y-6, 1, 1);
- }
- void fantasma3(){
- fill(0, 0, 255);
- ellipseMode(RADIUS);
- ellipse(g3x, g3y-4, 14, 12);
- ellipse(g3x-10.5, g3y+9.5, 3.4, 6);
- ellipse(g3x-3.5, g3y+9.5, 3.4, 6);
- ellipse(g3x+10.5, g3y+9.5, 3.4, 6);
- ellipse(g3x+3.5, g3y+9.5, 3.4, 6);
- rectMode(RADIUS);
- rect(g3x, g3y+4, 14, 6);
- fill(255);
- ellipse(g3x-5, g3y-6, 4, 4);
- ellipse(g3x+5, g3y-6, 4, 4);
- fill(0);
- ellipse(g3x-5, g3y-6, 1, 1);
- ellipse(g3x+5, g3y-6, 1, 1);
- }
- void desenharLabirinto() {
- // desenha a fronteira da área de jogo
- fill(0);
- stroke(0, 200, 250);
- strokeWeight(1);
- rectMode(CORNER);
- rect(margemH, margemV, 770 - 2*margemH, height - 2*margemV);
- //Desenha obstáculos
- //Cantos
- desenharObstaculo(2, 2, 2, 1);
- desenharObstaculo(2, 2, 1, 2);
- desenharObstaculo(14, 2, 1, 2);
- desenharObstaculo(13, 2, 1, 1);
- desenharObstaculo(2, 9, 1, 2);
- desenharObstaculo(3, 10, 1, 1);
- desenharObstaculo(14, 9, 1, 2);
- desenharObstaculo(13, 10, 1, 1);
- //Laterais Exteriores
- desenharObstaculo(2, 5, 1, 3);
- desenharObstaculo(3, 6, 1, 1);
- desenharObstaculo(14, 5, 1, 3);
- desenharObstaculo(13, 6, 1, 1);
- //T's Superior e Inferior
- desenharObstaculo(5, 2, 7, 1);
- desenharObstaculo(5, 10, 7, 1);
- desenharObstaculo(8, 3, 1, 2);
- desenharObstaculo(8, 9, 1, 1);
- //Peças Interiores
- desenharObstaculo(4, 4, 3, 1);
- desenharObstaculo(4, 8, 3, 1);
- desenharObstaculo(10, 4, 3, 1);
- desenharObstaculo(10, 8, 3, 1);
- desenharObstaculo(5, 5, 1, 1);
- desenharObstaculo(5, 7, 1, 1);
- desenharObstaculo(11, 5, 1, 1);
- desenharObstaculo(11, 7, 1, 1);
- //Barra Interior
- desenharObstaculo(7, 6, 3, 1);
- //Linhas
- stroke(0, 200, 250);
- strokeWeight(3);
- //Cantos
- line(110, 60+2, 110, 110-2);
- line(110, 460+2, 110, 570-60-2);
- line(770-110, 60+2, 770-110, 110-2);
- line(770-110, 460+2, 770-110, 570-60-2);
- //Laterais Exteriores
- line(110, 260+2, 110, 310-2);
- line(770-110, 260+2, 770-110, 310-2);
- //T's Superior e Inferior
- line(360+2, 110, 410-2, 110);
- line(360+2, 570-110, 410-2, 570-110);
- //Peças Interiores
- line(210+2, 210, 260-2, 210);
- line(770-260+2, 210, 770-210-2, 210);
- line(210+2, 570-210, 260-2, 570-210);
- line(770-260+2, 570-210, 770-210-2, 570-210);
- }
- void desenharBarraLateral(){
- stroke(0, 200, 250);
- strokeWeight(1);
- rectMode(CORNERS);
- rect(770, 10, 1090, 560);
- }
- /* Desenha um obstáculo interno de um labirinto:
- x: índice da célula inicial segundo eixo dos X - gama (1..nCol)
- y: índice da célula inicial segundo eixo dos Y - gama (1..nLin)
- numC: nº de colunas (células) segundo eixo dos X (largura do obstáculo)
- numL: nº de linhas (células) segundo eixo dos Y (altura do obstáculo)
- */
- void desenharObstaculo(int x, int y, int numC, int numL) {
- float x0, y0, larg, comp;
- x0 = margemH + (x-1) * tamanho;
- y0 = margemV + (y-1) * tamanho;
- larg = numC * tamanho;
- comp = numL * tamanho;
- fill(corObstaculos);
- stroke(254);
- rect(x0, y0, larg, comp);
- }
- /*
- Desenhar pontos nas células vazias (que não fazem parte de um obstáculo).
- Esta função usa a cor de fundo no ecrã para determinar se uma célula está vazia ou se faz parte de um obstáculo.
- */
- void desenharPontos() {
- float cx, cy;
- ellipseMode(CENTER);
- fill(255);
- noStroke();
- // Insere um ponto nas células vazias
- for(int i=1; i<=nCol; i++){
- for(int j=1; j<=nLin; j++) {
- cx = centroX(i);
- cy = centroY(j);
- color c = get((int)cx, (int)cy);
- if(pontos[i-1][j-1]==0) {
- if(c != corObstaculos) {
- fill(255);
- ellipse(cx, cy, pRaio/2+3, pRaio/2+3);
- }
- }
- }
- }
- }
- void keypressed(){
- float pacU, pacD, pacR, pacL;
- pacU=py-27;
- pacD=py+27;
- pacR=px+27;
- pacL=px-27;
- if (keyCode==DOWN){
- vy=2;
- vx=0;
- if(get((int)px, (int)pacD)==color(0, 200 , 250)){
- vy=0;
- }
- pontos[(int)((px)/50)][(int)(((py-20)/50))]=2;
- }
- if (keyCode==UP){
- vx=0;
- vy=-2;
- if(get((int)px, (int)pacU)==color(0, 200 , 250)){
- vy=0;
- }
- pontos[(int)((px)/50)][(int)(((py)/50))]=2;
- }
- if (keyCode==LEFT){
- vx=-2;
- vy=0;
- if(get((int)pacL, (int)py)==color(0, 200 , 250)){
- vx=0;
- }
- pontos[(int)((px)/50)][(int)(((py)/50))]=2;
- }
- if (keyCode==RIGHT){
- vx=2;
- vy=0;
- if(get((int)pacR, (int)py)==color(0, 200 , 250)){
- vx=0;
- }
- pontos[(int)((px-20)/50)][(int)(((py)/50))]=2;
- }
- if (keyCode==CONTROL){
- vx=0;
- vy=0;
- }
- py+=vy;
- px+=vx;
- if (key == ESC || key == ESC) {
- output.flush();
- output.close();
- exit();
- }
- }
- void margens(){
- //Margem direita
- if(px >= centroX(nCol)){
- px=centroX(nCol);
- }
- //Margem esquerda
- if(px <= centroX(1)){
- px=centroX(1);
- }
- //Margem superior
- if(py <= centroY(1)){
- py=centroY(1);
- }
- //Margem inferior
- if(py >= centroY(nLin)){
- py=centroY(nLin);
- }
- }
- void BarraLateraldoJogo(){
- //Desenhar o titulo Pontos:
- textFont(fonte,26);
- fill(232, 239, 40);
- text("Pontos:",887,70);
- //Desenhar a pontuação
- textFont(fonte,26);
- fill(232, 239, 40);
- text(pontuacaoatual,920,120);
- //Desenhar o titulo Melhor Pontuação:
- textFont(fonte,26);
- fill(232, 239, 40);
- text("Melhor Pontuação:",825,170);
- //Desenhar o nivel:
- textFont(fonte,26);
- fill(232, 239, 40);
- text(pontuacaomaxima,920,220);
- //Desenhar o titulo Vidas:
- textFont(fonte,26);
- fill(232, 239, 40);
- text("Vidas:",895,270);
- //Desenhar o nivel:
- textFont(fonte,26);
- fill(232, 239, 40);
- text(vidas,920,320);
- //Desenhar o titulo Nivel:
- textFont(fonte,26);
- fill(232, 239, 40);
- text("Nivel:",895,370);
- //Desenhar o nivel:
- textFont(fonte,26);
- fill(232, 239, 40);
- text(nivel,920,420);
- }
- void pontuacoeseniveis(){
- pontuacaoatual=pontuacao(pontos);
- if(pontuacaoatual==109){
- for(int a=0;a<nCol;a++){
- for(int b=0;b<nLin;b++){
- pontos[a][b]=0;
- }
- }
- nivel=nivel+1;
- }
- }
- void colisoes(){
- if(get((int)px,(int) py)==color(255, 0, 0)){
- collisions=true;
- }
- if(get((int)px,(int) py)==color(0, 255, 0)){
- collisions=true;
- }
- if(get((int)px,(int) py)==color(0, 0, 255)){
- collisions=true;
- }
- if(collisions==true){
- px=centroX(8);
- py=centroY(8);
- keyCode=CONTROL;
- vidas-=1;
- }
- collisions=false;
- }
- void updateHighscore() {
- if (pontuacaomaxima < pontuacaoatual) {
- pontuacaomaxima = pontuacaoatual;
- //ficheiro pontuação máxima:
- output = createWriter("highscore.txt");
- output.println(pontuacaomaxima);
- output.close();
- }
- }
- }
- void importHighscore() {
- reader = createReader("highscore.txt");
- if (reader == null) {
- pontuacaomaxima = 0;
- return;
- }
- try {
- line = reader.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- line = null;
- }
- if (line != null) {
- pontuacaomaxima= int(line);
- }
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // transformar o índice de uma célula em coordenada no ecrã
- float centroX(int col) {
- return margemH + (col - 0.5) * tamanho;
- }
- // transformar o índice de uma célula em coordenada no ecrã
- float centroY(int lin) {
- return margemV + (lin - 0.5) * tamanho;
- }
- int pontuacao(double pontos[][]){
- int cont=0;
- for(int i=1; i<=nCol; i++){
- for(int j=1; j<=nLin; j++) {
- if (pontos[i-1][j-1]==2){
- cont=cont+1;
- }
- }
- }
- return cont;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement