first commit

This commit is contained in:
Stefano Rossi 2025-07-10 03:26:17 +02:00
commit 49d028b3d9
Signed by: chadmin
GPG key ID: 9EFA2130646BC893
60 changed files with 3704 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/notpacman.iml
.idea

11
CHANGELOG Normal file
View file

@ -0,0 +1,11 @@
CHANGELOG v1.2.0
-Main menu with background animation and music
-2 playable maps based on the true pacman version
-Background music and dead animation
-2 alternative modalities:
- Auto pacman: Randomly spawned ghosts will try to catch PacMan, and he will automatically move away from them.
- Survival game: Randomly spawned ghosts will try to catch PacMan. You need to escape from them. You can place and remove blocks from the map. Ghost will increase speed with time. The more you survive the more you will score.

26
LICENSE Normal file
View file

@ -0,0 +1,26 @@
# DON'T BE A DICK PUBLIC LICENSE
> Version 1.1, December 2016
> Copyright (C) 2024 Rossi Stefano
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document.
> DON'T BE A DICK PUBLIC LICENSE
> TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
1. Do whatever you like with the original work, just don't be a dick.
Being a dick includes - but is not limited to - the following instances:
1a. Outright copyright infringement - Don't just copy this and change the name.
1b. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick.
1c. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick.
2. If you become rich through modifications, related works/services, or supporting the original work,
share the love. Only a dick would make loads off this work and not buy the original work's
creator(s) a pint.
3. Code is provided with no warranty. Using somebody else's code and bitching when it goes wrong makes
you a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix back.

3
applet.policy Normal file
View file

@ -0,0 +1,3 @@
grant {
permission java.security.AllPermission;
};

73
build.xml Normal file
View file

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="PacMan" default="default" basedir=".">
<description>Builds, tests, and runs the project PacMan.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="PacMan-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

28
build/GrigliaApplet.html Normal file
View file

@ -0,0 +1,28 @@
<HTML>
<HEAD>
<TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>
<!--
*** GENERATED applet HTML launcher - DO NOT EDIT IN 'BUILD' FOLDER ***
If you need to modify this HTML launcher file (e.g., to add applet parameters),
copy it to where your applet class is found in the SRC folder. If you do this,
the IDE will use it when you run or debug the applet.
Tip: To exclude an HTML launcher from the JAR file, use exclusion filters in
the Packaging page in the Project Properties dialog.
For more information see the online help.
-->
<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>
<P>
<APPLET codebase="classes" code="pacman/GrigliaApplet.class" width=350 height=200></APPLET>
</P>
<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>
</HTML>

View file

View file

BIN
build/classes/battle.mid Normal file

Binary file not shown.

BIN
build/classes/ciboBase.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

BIN
build/classes/dead.mid Normal file

Binary file not shown.

BIN
build/classes/fantB.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

BIN
build/classes/fantO.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

BIN
build/classes/fantP.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

BIN
build/classes/fantR.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

BIN
build/classes/loading.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
build/classes/main.mid Normal file

Binary file not shown.

BIN
build/classes/movFant.wav Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
build/classes/pacmanDx.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
build/classes/pacmanGiu.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 357 B

BIN
build/classes/pacmanSx.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 347 B

BIN
build/classes/pacmanUp.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 355 B

BIN
build/classes/poweredBy.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
build/classes/win.mid Normal file

Binary file not shown.

BIN
images/L2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

BIN
images/notpacmanl1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
images/notpacmanmenu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
images/survuv.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

3
manifest.mf Normal file
View file

@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

1799
nbproject/build-impl.xml Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,8 @@
build.xml.data.CRC32=a7646d26
build.xml.script.CRC32=63e60ced
build.xml.stylesheet.CRC32=f85dc8f2@1.111.0.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=a7646d26
nbproject/build-impl.xml.script.CRC32=08b65571
nbproject/build-impl.xml.stylesheet.CRC32=12e0a6c2@1.111.0.48

View file

View file

@ -0,0 +1,8 @@
compile.on.save=true
do.depend=false
do.jar=true
do.jlink=false
javac.debug=true
javadoc.preview=true
jlink.strip=false
user.properties.file=C:\\Users\\Administrator\\AppData\\Roaming\\NetBeans\\22\\build.properties

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group/>
</open-files>
</project-private>

View file

@ -0,0 +1,103 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
application.title=PacMan
application.vendor=BBC
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
build.dir=build
build.generated.dir=${build.dir}/generated
build.generated.sources.dir=${build.dir}/generated-sources
# Only compile against the classpath explicitly listed here:
build.sysclasspath=ignore
build.test.classes.dir=${build.dir}/test/classes
build.test.results.dir=${build.dir}/test/results
# Uncomment to specify the preferred debugger connection transport:
#debug.transport=dt_socket
debug.classpath=\
${run.classpath}
debug.modulepath=\
${run.modulepath}
debug.test.classpath=\
${run.test.classpath}
debug.test.modulepath=\
${run.test.modulepath}
# This directory is removed when the project is cleaned:
dist.dir=dist
dist.jar=${dist.dir}/PacMan.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
includes=**
jar.archive.disabled=${jnlp.enabled}
jar.compress=false
jar.index=${jnlp.enabled}
javac.classpath=
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.external.vm=false
javac.modulepath=
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
javac.source=1.7
javac.target=1.7
javac.test.classpath=\
${javac.classpath}:\
${build.classes.dir}
javac.test.modulepath=\
${javac.modulepath}
javac.test.processorpath=\
${javac.test.classpath}
javadoc.additionalparam=
javadoc.author=false
javadoc.encoding=${source.encoding}
javadoc.html5=false
javadoc.noindex=false
javadoc.nonavbar=false
javadoc.notree=false
javadoc.private=false
javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
jlink.launcher=false
jlink.launcher.name=PacMan
jnlp.applet.class=pacman.GrigliaApplet
jnlp.applet.height=300
jnlp.applet.width=300
jnlp.codebase.type=no.codebase
jnlp.descriptor=application
jnlp.enabled=false
jnlp.mixed.code=default
jnlp.offline-allowed=false
jnlp.signed=false
jnlp.signing=
jnlp.signing.alias=
jnlp.signing.keystore=
main.class=pacman.GrigliaApplet
manifest.file=manifest.mf
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=Zulu_8.0.412_8
run.classpath=\
${javac.classpath}:\
${build.classes.dir}
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=-Djava.security.policy=applet.policy -Xms64m -Xmx256m
run.modulepath=\
${javac.modulepath}
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
run.test.modulepath=\
${javac.test.modulepath}
source.encoding=UTF-8
src.dir=src
test.src.dir=test

16
nbproject/project.xml Normal file
View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.java.j2seproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
<name>PacMan</name>
<explicit-platform explicit-source-supported="true"/>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
</test-roots>
</data>
</configuration>
</project>

66
readme.md Normal file
View file

@ -0,0 +1,66 @@
# Definitely Not PacMan
Latest version: v1.2.0
![ezgif-2-75e5b259bd.gif](images/ezgif-2-75e5b259bd.gif)
## Description
This project started with the study of the Manhattan algorithm which is the
AI used by the ghosts to find the shortest path to reach PacMan.
You can move PacMan with the keyboard `wasd` pattern.
This is the first map (L1):
![Scheme](images/notpacmanl1.png)
The second map is based on pacman 2 (L2):
![L2.png](images/L2.png)
The third option the `survival game`:
![survuv.png](images/survuv.png)
This is a different modality where you add and remove the blocks of map.
THe goal is to survive as long as possible. With time, the game gets harder
by adding ghosts, removing blocks and increasing the speed of the ghosts.
I never implemented The `Auto pacman` modality, which was meant to program
the pacman to play by itself. And survive the longest possible.
## ChangeLog
[CHANGELOG](CHANGELOG)
## Requirements
- Java (6 to) 8 (max, recommended)
- NetBeans (highly recommended)
Java Applets have been dropped with Java 9. So you need an older versione to run this project.
Java 8 is the preferred version. Until Java 6 should be ok.
Problems running the project are expected, applets are deprecated and not supported by modern browsers.**
This is a NetBeans Project. Using NetBeans isn't mandatory but recommended for the built-in applet runner.
You can run the GrigliaApplet.java class with netbeans.
[NetBeans Instructions](https://netbeans.apache.org/tutorial/main/kb/docs/java/javase-jdk8/)
Alternatively, you can use IntellijJ IDEA with the [Applet runner plugin](https://plugins.jetbrains.com/plugin/16682-applet-runner) (not tested).
## Installation and run
Clone the repo:
```bash
git clone https://stefanorossi@bitbucket.org/stefanorossi/definetlynotpacman.git
```
Run `SuppaMarioApplet.java` with your preferred IDE (NetBeans is recommended) using java 8.
## License
All my work is released under [DBAD](https://www.dbad-license.org/) license.

56
src/pacman/Cella.java Normal file
View file

@ -0,0 +1,56 @@
/*
* Questa classe serve impostare se una cella è un muro o è libera, e memorizza
* un valore intero
*/
package pacman;
/**
*
* @author BBC
*/
public class Cella {
boolean muro; //false = libera, true = muro
int passi; //memorizzo qui la distanza in passi da pacman
///////////////////////////////////////////
//COSTRUTTORI
///////////////////////////////////////////
public Cella(){
this.muro = false;
this.passi = 0;
}
///////////////////////////////////////////
//GET DI TUTTI GLI ATTRIBUTI
///////////////////////////////////////////
public boolean isMuro(){
if(this.muro == true) {
return true;
}
else {
return false;
}
}
public int getPassi(){
return this.passi;
}
///////////////////////////////////////////
//SET RESET DI TUTTI GLI ATTRIBUTI
///////////////////////////////////////////
public void setStato(String stato){
if(stato == "libera") {
this.muro = false;
}
else {
this.muro = true;
}
}
public void setPassi(int passi){
this.passi = passi;
}
public void resetPassi(){
this.passi = 0;
}
}

105
src/pacman/Cibo.java Normal file
View file

@ -0,0 +1,105 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pacman;
/**
*
* @author BBC
*/
public class Cibo {
///////////////////////////////////////////
//ATTRIBUTI
///////////////////////////////////////////
private int riga; //riga in è
private int colonna; //colonna in cui è
private short ttl; //time to live, tempo dopo la quale si annullerà
private short ttlrel; //memorizzazione ttl relativo
private int valore; //valore del cibo
private boolean mangiato; //true se il cibo è gia stato mangiato
///////////////////////////////////////////
//COSTRUTTORI
///////////////////////////////////////////
public Cibo(){
this.riga = 0;
this.colonna = 0;
this.ttlrel = 0;
this.valore = 200; //assegna una valore casuale da 1000 a 5000 a tep di 1000
this.ttl = 0;
this.mangiato = false;
}
public Cibo(int r, int c){
this.riga = r;
this.colonna = c;
this.ttlrel = 0;
this.valore = 200; //assegna una valore casuale da 1000 a 5000 a tep di 1000
this.ttl = 0;
this.mangiato = false;
}
///////////////////////////////////////////
//GETTER
///////////////////////////////////////////
public int getRiga(){
return this.riga;
}
public int getColonna(){
return this.colonna;
}
public short getTimeToLive(){
return this.ttl;
}
public short getTimeToLiveRel(){
return this.ttlrel;
}
public int getValore(){
return this.valore;
}
public boolean isMangiato(){
return this.mangiato;
}
///////////////////////////////////////////
//SETTER
///////////////////////////////////////////
public void setRiga(int riga){
this.riga = riga;
}
public void setColonna(int colonna){
this.colonna = colonna;
}
public void setTimeToLive(short ttl){
this.ttl = ttl;
}
public void setTimeToLiveRel(short ttlrel){
this.ttlrel = ttlrel;
}
public void setValore(int valore){
this.valore = valore;
}
public void mangiato(){
this.mangiato = true;
}
public void nonMangiato(){
this.mangiato = false;
}
///////////////////////////////////////////
//METODI
///////////////////////////////////////////
public void setValoreRandom(){
this.valore = randomValue();
}
public int randomValue(){
//numero casuale da 1000 a 5000 a step di 1000 che è il punteggio
return (int)(Math.round(Math.random() * 4) + 1) * 500;
}
public void setTtlRandom(){
this.ttl = randomTtl();
}
public short randomTtl(){
//numero casuale da 5 a 10 e sarebbero i secondi di vita
return (short)(Math.round(Math.random() * 5) + 5);
}
}

115
src/pacman/Fantasmino.java Normal file
View file

@ -0,0 +1,115 @@
/*
* Questa classe serve creare un fantasmino
*/
package pacman;
import java.awt.Color;
/**
*
* @author BBC
*/
public class Fantasmino {
private int posR; //la pos attuale sulla riga
private int posC; //la pos attuale sulla colonna
private int nextR; //la pos successiva sulla riga
private int nextC; //la pos successiva sulla colonna
private Color colore; //memorizzo il colore
///////////////////////////////////////////
//COSTRUTTORI
///////////////////////////////////////////
public Fantasmino() {
this.posR = 0;
this.posC = 0;
this.nextR = 0;
this.nextC = 0;
this.colore = randomColor();
}
public Fantasmino(int r, int c) {
this.posR = r;
this.posC = c;
this.nextR = r;
this.nextC = c;
this.colore = randomColor(); //Quando viene chiamato questo costruttore
//viene dato un colore casuale
}
//ritorno un colore casuale tra quelli possibili (num da 0 a 4)
private Color randomColor(){
int num = (int)Math.round(Math.random() * 3);
if(num == 0){
return Color.BLUE;
}
else if(num == 1){
return Color.RED;
}
else if(num == 2){
return Color.PINK;
}
else{
return Color.ORANGE;
}
}
///////////////////////////////////////////
//SET DI TUTTI GLI ATTRIBUTI
///////////////////////////////////////////
public void setPosR(int r){
this.posR = r;
}
public void setPosC(int c){
this.posC = c;
}
public void setNextR(int c){
this.nextR = c;
}
public void setNextC(int c){
this.nextC = c;
}
public void setPos(int posR, int posC){
this.posR = posR;
this.posC = posC;
}
public void setNextPos(int posR, int posC){
this.nextR = nextR;
this.nextC = nextC;
}
public void setColor(Color c){
this.colore = c;
}
///////////////////////////////////////////
//GET DI TUTTI GLI ATTRIBUTI
///////////////////////////////////////////
public int getPosR(){
return this.posR;
}
public int getPosC(){
return this.posC;
}
public int getNextR(){
return this.nextR;
}
public int getNextC(){
return this.nextC;
}
public Color getColor(){
return this.colore;
}
///////////////////////////////////////////
//METODI DI SPOSTAMENTO
///////////////////////////////////////////
public void goLeft(){
this.posC--;
}
public void goRight(){
this.posC++;
}
public void goUp(){
this.posR--;
}
public void goDown(){
this.posR++;
}
}

View file

@ -0,0 +1,59 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pacman;
/**
*
* @author BBC
*/
public class GestioneCibo {
private Cibo[][] cibi;
private int cntCiboRimasto;
///////////////////////////////////////////
//COSRUTTORI
///////////////////////////////////////////
public GestioneCibo(){
}
public GestioneCibo(int r, int c, Griglia g){
cibi = new Cibo[r][c];
cntCiboRimasto = r*c;
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
//creo un oggetto cibo alle coordinate
cibi[i][j] = new Cibo(i, j);
////se è già presente un muro lo setto come mangiato altrimenti rimane non mangiato
if(g.isMuro(i, j)){
cibi[i][j].mangiato();
cntCiboRimasto--;
}
}
}
}
///////////////////////////////////////////
//SETTER
///////////////////////////////////////////
public void ciboMangiato(int r, int c){
cibi[r][c].mangiato();
cntCiboRimasto--;
}
public void ciboNonMangiato(int r, int c){
cibi[r][c].nonMangiato();
cntCiboRimasto++;
}
///////////////////////////////////////////
//GETTER
///////////////////////////////////////////
public boolean ciboIsMangiato(int r, int c){
return cibi[r][c].isMangiato();
}
public int getCiboRimasto(){
return this.cntCiboRimasto;
}
public int getValore(int r, int c){
return cibi[r][c].getValore();
}
}

192
src/pacman/Griglia.java Normal file
View file

@ -0,0 +1,192 @@
/*
* Qui creo la cells che verra stampata
*/
package pacman;
/**
*
* @author BBC
*/
public class Griglia {
private Cella[][] cells;
///////////////////////////////////////////
//COSTRUTTORI
///////////////////////////////////////////
public Griglia(){
}
public Griglia(int r, int c){
cells = new Cella[r][c];
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
cells[i][j] = new Cella();
}
}
}
///////////////////////////////////////////
//GET DI TUTTI GLI ATTRIBUTI
///////////////////////////////////////////
public boolean isMuro(int r, int c){
return this.cells[r][c].isMuro();
}
public int getPassi(int r, int c){
return this.cells[r][c].getPassi();
}
///////////////////////////////////////////
//SET E TOGGLE DI TUTTI GLI ATTRIBUTI
///////////////////////////////////////////
public void setStato(int r, int c, String stato){
this.cells[r][c].setStato(stato);
}
public void setPassi(int r, int c){
}
public void toggleStato(int r, int c){
if(this.cells[r][c].isMuro() == true){
this.cells[r][c].setStato("libera");
}
else{
this.cells[r][c].setStato("muro");
}
}
///////////////////////////////////////////
//METODI
///////////////////////////////////////////
//ciclo che genera i muri per una griglia 11x15 ovvero del liv 1 di pacman
public void generaMuriPacman(){
//inserisco i muri nella griglia
for(int r = 0; r < cells.length; r++){
for(int c = 0; c < cells[0].length; c++){
//metto i muri ai bordi
if(r == 0 || c == 0 || r == cells.length-1 || c == cells[0].length-1){
cells[r][c].setStato("muro");
}
//metto i muri alla prima e alla penultima riga
if((r == 1 || r == cells.length-2) && (c == 5 || c == 14)){
cells[r][c].setStato("muro");
}
//metto i muri alla seconda e alla terzultima riga
if((r == 2 || r == cells.length-3) && (c == 2 || c == 3 || c == 5 || c == 14 || c == 16 || c == 17 || (c>6 && c<13))){
cells[r][c].setStato("muro");
}
//metto i muri alla terza e alla qartultima riga
if((r == 3 || r == cells.length-4) && (c == 2 || c == 17)){
cells[r][c].setStato("muro");
}
//metto i muri alla quarta e alla quintultima riga in piu aggiungo 2 quardati in mezzo alla quintultima riga
if((r == 4 || r == cells.length-5) && (c == 2 || c == 4 || c == 5 || c == 7 || c == 8 || c == 11 || c == 12 || c == 14 || c == 15 || c == 17 || (r == cells.length-5 && (c == 9 || c == 10)))){
cells[r][c].setStato("muro");
}
//metto i muri alla riga centrale
if((r == 5) && (c == 7 || c == 12)){
cells[r][c].setStato("muro");
}
}
}
}
//ciclo che genera i muri per una griglia 16x21 ovvero del liv 2 di pacman
public void generaMuriPacmanL2(){
//inserisco i muri nella griglia
for(int r = 0; r < cells.length; r++){
for(int c = 0; c < cells[0].length; c++){
//metto i muri ai bordi
if(r == 0 || c == 0 || r == cells.length-1 || c == cells[0].length-1){
cells[r][c].setStato("muro");
}
if(r == 1 && c == 10 ){
cells[r][c].setStato("muro");
}
if(r == 2 && c == 10 ){
cells[r][c].setStato("muro");
}
if(r == 2 && (c == 2 || c == 3 || c == 4 || c == 6 || c == 7 || c == 8 || c == 10 || c == 12 || c == 13 || c == 14 || c == 16 || c == 17 || c == 18) ){
cells[r][c].setStato("muro");
}
if(r == 4 && (c == 2 || c == 3 || c == 4 || c == 6 || c == 8 || c == 9 || c == 10 || c == 11 || c == 12 || c == 14 || c == 16 || c == 17 || c == 18) ){
cells[r][c].setStato("muro");
}
if(r == 5 && (c == 6 || c == 10 || c == 14) ){
cells[r][c].setStato("muro");
}
if(r == 6 && (c == 1 || c == 2 || c == 3 || c == 4 || c == 6 || c == 7 || c == 8 || c == 10 || c == 12 || c == 13 || c == 14 || c == 16 || c == 17 || c == 18 || c == 19) ){
cells[r][c].setStato("muro");
}
if(r == 8 && (c == 8 || c == 13) ){
cells[r][c].setStato("muro");
}
if(r == 9 && (c == 1 || c == 2 || c == 3 || c == 4 || c == 6 || c == 8 || c == 9 || c == 10 || c == 11 || c == 12 || c == 13 || c == 15 || c == 17 || c == 18 || c == 19) ){
cells[r][c].setStato("muro");
}
if(r == 11 && ((c >= 8 && c <= 13) || (c >= 1 && c <= 4) || (c >= 16 && c <= 19))){
cells[r][c].setStato("muro");
}
if(r == 13 && (c == 2 || c == 3 || c == 5 || c == 6 || c == 7 || c == 9 || c == 10 || c == 12 || c == 13 || c == 14 || c == 17 || c == 18) ){
cells[r][c].setStato("muro");
}
}
}
}
public void generaMuriBordo(){
for(int r = 0; r < cells.length; r++){
for(int c = 0; c < cells[0].length; c++){
//metto i muri ai bordi
if(r == 0 || c == 0 || r == cells.length-1 || c == cells[0].length-1){
cells[r][c].setStato("muro");
}
}
}
}
public void mappatura(PacMan bbcman){
int posRighe = bbcman.getPosR();
int posColonne = bbcman.getPosC();
resetPassi();
int passi = 0;
calcolaPassiCellaAdiacente(posRighe, posColonne, passi);
cells[posRighe][posColonne].setPassi(0);
}
private void resetPassi(){
for(int r = 0; r < cells.length; r++){
for(int c = 0; c < cells[0].length; c++){
cells[r][c].resetPassi();
}
}
}
private void calcolaPassiCellaAdiacente(int posRighe, int posColonne, int passi){
//imposto il valore di questa come i passi calcolati e incremento di 1
cells[posRighe][posColonne].setPassi(passi);
passi++;
//aggiungo un controllo di out of bound in questo caso sulla colonna massima
if(posColonne < cells[0].length-1){
//se non è un muro apro la cella a DESTRA se non è un muro o se la cella ha un num sup uguale a se stessa
if(!cells[posRighe][posColonne+1].isMuro() && (cells[posRighe][posColonne].getPassi() < cells[posRighe][posColonne+1].getPassi() || cells[posRighe][posColonne+1].getPassi() == 0)) {
calcolaPassiCellaAdiacente(posRighe, posColonne+1, passi); //incremento la colonna di 1 temporaneo
}
}
//aggiungo un controllo di out of bound in questo caso sulla riga massima
if(posRighe < cells.length-1){
//se non è un muro apro la cella INFERIORE se non è un muro o se la cella ha un num sup uguale a se stessa
if(!cells[posRighe+1][posColonne].isMuro() && (cells[posRighe][posColonne].getPassi() < cells[posRighe+1][posColonne].getPassi() || cells[posRighe+1][posColonne].getPassi() == 0)) {
calcolaPassiCellaAdiacente(posRighe+1, posColonne, passi); //incremento la colonna di 1 temporaneo
}
}
//aggiungo un controllo di out of bound in questo caso sulla riga minima
if(posRighe > 0){
//se non è un muro apro la cella SUPERIORE se non è un muro o se la cella ha un num sup uguale a se stessa
if(!cells[posRighe-1][posColonne].isMuro() && (cells[posRighe][posColonne].getPassi() < cells[posRighe-1][posColonne].getPassi() || cells[posRighe-1][posColonne].getPassi() == 0)) {
calcolaPassiCellaAdiacente(posRighe-1, posColonne, passi);
}
}
//aggiungo un controllo di out of bound in questo caso sulla colonna minima
if(posColonne > 0){
//se non è un muro apro la cella SINISTRA se non è un muro o se la cella ha un num sup uguale a se stessa
if(!cells[posRighe][posColonne-1].isMuro() && (cells[posRighe][posColonne].getPassi() < cells[posRighe][posColonne-1].getPassi() || cells[posRighe][posColonne-1].getPassi() == 0)) {
calcolaPassiCellaAdiacente(posRighe, posColonne-1, passi);
}
}
}
}

View file

@ -0,0 +1,957 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* Questo programma è ottimizzato per essere 100x600
*/
package pacman;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.MediaTracker;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
/**
*
* @author BBC
*/
public class GrigliaApplet extends Applet implements Runnable, MouseListener, MouseMotionListener, ActionListener, KeyListener {
///////////////////////////////////////////
//INIZIALIZZO LE VARIABILI GENERALI
///////////////////////////////////////////
private int numRighe;
private int numColonne; //
private int lato;
private int contextW;
private int contextH;
private int rCella;
private int cCella;
private boolean initPacmanLikeGame = false; //se questa variabile è vera viene inizializzato il gioco in formato pacman
private boolean initLoadInitiale = true; //flag di esecuzione del caricamento degli elementi iniziali
private double punteggio = 0;
private double punteggioRel = 0; // serve a memorizzare il punteggio gia utilizzato per il calcolo di aumento velocita fantasmini
private Thread thMovGhosts = new Thread(this);
private long msMovGhosts = 1500; // iniziano facendo un passo ogni 3 secodni
private Thread thWallLamp = new Thread(this);
;
private boolean primoCicloMainMenu = true;
private Thread thAggMainMenu = new Thread(this);
private short partenzaPi; //dove randomizzo se iniziale la posizione di pacman a sx a dx sopra o sotto
private boolean mainMenuEnabled = true;
int xPi = -250, yPi = -250; //pos pacman princ inizialmente -250 cosi viene ricalcolata subito dal primo cilco
private AudioClip songBase; // inizio
private AudioClip songYouLoose; // loose
private AudioClip songMainMenu; // song del menu iniziale
private AudioClip songYouWin; //song per quando si vince
private AudioClip songCiboMangiato; //effetto sonoro pewr qunado viene mangiato un pallino
private AudioClip songMovFant; //song per i fantasmini si muovono
private boolean initGame = true; //variabile di flag per i cicli di ninit del giogo vero dopo il menu
private Griglia griglia;
private PacMan bbcman;
private GestioneCibo menu;
private Fantasmino[] ghosts;
private int ghostsCNT = 0;
private int maxGhosts = 4;
private Color bgColor = Color.BLACK;
private Color wallColor = Color.BLUE;
private Color streetColor = Color.BLACK;
Image imgBuff; //dichiarazione oggetti per doppio buffering
Graphics grafBuff;
//gioco casual
private boolean initCasualGame = false;
private Button bCasualGame;
// gioco con pacman automatico
private boolean initAutoPacmanGame = false;
private boolean autoPacman = false; // viene impostata a true quando parte il gioco con pacman automatico
// gioco con pacman liv2
private boolean initPacmanLikeGameL2 = false;
private Button bStartPacManl2;
//DICHIARO LE VARIABILI IMG PER I FANTASMINI E PER PACMAN
Image imgPoweredBy;
MediaTracker mtPacMan;
Image imgPacMan;
MediaTracker mtFant;
Image imgFantRed;
Image imgFantOrange;
Image imgFantBlue;
Image imgFantPink;
MediaTracker mtCibo;
Image imgCiboBase;
Image imgPi;
Image imgFiB;
Image imgFiR;
Image imgFiG;
Image imgFiP;
//Image imgScaledToDraw; //qui memorizzo l immagine scalata in modo da disegnarla
Image imgLoadingGif;
///////////////////////////////////////////
//INIZIALIZZO GLI ELEMENTI DEL PANNELLINO
///////////////////////////////////////////
private Button bStartPacManl1;
private Button bStartAutoPacmanl1;
private Button bAvanti;
private Button bImposta;
private TextField tfRighe;
private TextField tfColonne;
private Label lRighe;
private Label lColonne;
private Label lCntFantasmini;
private Label lPunteggio;
///////////////////////////////////////////
//INIT
///////////////////////////////////////////
@Override
public void init() {
//ingrandisco il conteto grafico e inizio la song del menu
this.setSize(1000, 599);
//creazione oggetti per doppio buffering
imgBuff = createImage(2000, 1000);
grafBuff = imgBuff.getGraphics();
//attivo le interfacce per questo applet
this.addMouseListener(this);
this.addMouseMotionListener(this);
//per utilizzare il keylistener devo dare il focus al contesto grafico
this.setFocusable(true);
this.requestFocus();
///////////////////////////////////////////
//IMPOSTO GLI ELEMENTI DEL PANNELLINO
///////////////////////////////////////////
bAvanti = new Button("Avanti"); // assegno la label Avanti al pulsante avanti
bAvanti.addActionListener(this); // assegno l action listener al pulsante bAvanti
bStartPacManl1 = new Button("Pacman L1");
bStartPacManl1.addActionListener(this);
bStartPacManl2 = new Button("Pacman L2");
bStartPacManl2.addActionListener(this);
bStartAutoPacmanl1 = new Button("Auto Pacman");
bStartAutoPacmanl1.addActionListener(this);
bCasualGame = new Button("Survival Game");
bCasualGame.addActionListener(this);
bAvanti.hide();
tfRighe = new TextField("11");
tfColonne = new TextField("20");
lRighe = new Label("Righe: ");
lColonne = new Label("Colonne: ");
bImposta = new Button("Imposta");
bImposta.addActionListener(this);
tfRighe.enable(false);
tfColonne.enable(false);
bImposta.hide();
lCntFantasmini = new Label("Fantasmini rimasti: " + (maxGhosts - ghostsCNT));
lPunteggio = new Label("Punteggio: " + (int) punteggio);
//lPunteggio.setSize(lPunteggio.getWidth() + 50, lPunteggio.getHeight());
bbcman = new PacMan();
ghosts = new Fantasmino[50]; //lo dichiaro piu grande e ridimensiono dopo il massimo di fantasmini che inizialemente è 4
}
public void destroy() {
songBase.stop();
songMainMenu.stop();
songYouLoose.stop();
}
///////////////////////////////////////////
//PAINT
///////////////////////////////////////////
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g) {
//memorizzo whidth e height del contesto grafico
//sottraggo 50px in modo da non sovrascrivere il menu
contextW = this.getWidth();
contextH = this.getHeight() - 50;
//Carico tutti gli elementi necessari
if (initLoadInitiale) {
//carico immagine loading NON LA CARICA NON SO PERCHE, LA DISEGNA SE TROVA UN ERRORE DOPO G.DWARIMAGE
grafBuff.fillRect(0, 0, this.getWidth(), this.getHeight());
imgLoadingGif = getImage(getCodeBase(), "loading.gif");
grafBuff.drawImage(imgLoadingGif, 200, 100, this);
g.drawImage(imgBuff, 0, 0, this);
System.out.print("Caricamento immagini...");
mtPacMan = new MediaTracker(this);
mtPacMan.addImage(imgLoadingGif, 0); //per non creare un altro mt lo assegno temporaneamente a quello di pacman
imgPacMan = getImage(getCodeBase(), "pacmanDx.gif");
mtPacMan.addImage(imgPacMan, 0);
imgPi = getImage(getCodeBase(), "pacman.gif");
mtPacMan.addImage(imgPi, 1);
mtFant = new MediaTracker(this);
imgFantRed = getImage(getCodeBase(), "fantR.png");
imgFantBlue = getImage(getCodeBase(), "fantB.png");
imgFantOrange = getImage(getCodeBase(), "fantO.png");
imgFantPink = getImage(getCodeBase(), "fantP.png");
imgFiB = getImage(getCodeBase(), "fantB.png");
imgFiR = getImage(getCodeBase(), "fantR.png");
imgFiG = getImage(getCodeBase(), "fantO.png");
imgFiP = getImage(getCodeBase(), "fantP.png");
mtPacMan.addImage(imgFantRed, 0);
mtPacMan.addImage(imgFantBlue, 1);
mtPacMan.addImage(imgFantPink, 2);
mtPacMan.addImage(imgFantOrange, 3);
mtCibo = new MediaTracker(this);
imgCiboBase = getImage(getCodeBase(), "ciboBase.gif");
imgPoweredBy = getImage(getCodeBase(), "poweredBy.gif");
System.out.println(" OK!");
//aggiungo i pulsanti di scelta del livello
this.add(bStartPacManl1);
this.add(bStartPacManl2);
this.add(bStartAutoPacmanl1);
this.add(bCasualGame);
//carico le song del gioco
System.out.print("Caricamento tema musicale...");
songBase = getAudioClip(getCodeBase(), "battle.mid");
songYouLoose = getAudioClip(getCodeBase(), "dead.mid");
songMainMenu = getAudioClip(getCodeBase(), "main.mid");
songYouWin = getAudioClip(getCodeBase(), "win.mid");
songCiboMangiato = getAudioClip(getCodeBase(), "ciboMangiato.wav");
songMovFant = getAudioClip(getCodeBase(), "movFant.wav");
System.out.println(" OK!");
songMainMenu.loop();
initLoadInitiale = false;
}
//questo ciclo è vero all inizio e stampa il menu
if (mainMenuEnabled) {
grafBuff.setColor(bgColor);
grafBuff.fillRect(0, 35, this.getWidth(), this.getHeight());
bStartPacManl1.setBackground(Color.YELLOW);
bStartPacManl1.setForeground(Color.BLUE);
bStartPacManl1.setBounds(200, 200, 200, 50);
bStartPacManl2.setBackground(Color.YELLOW);
bStartPacManl2.setForeground(Color.BLUE);
bStartPacManl2.setBounds(200, 251, 200, 50);
bStartAutoPacmanl1.setBackground(Color.YELLOW);
bStartAutoPacmanl1.setForeground(Color.BLUE);
bStartAutoPacmanl1.setBounds(600, 200, 200, 50);
bCasualGame.setBackground(Color.YELLOW);
bCasualGame.setForeground(Color.BLUE);
bCasualGame.setBounds(600, 251, 200, 50);
///////////////////////////////////////////
//DISEGNO bbcman
///////////////////////////////////////////
//se è la prima volta che avvio il menu genero casualmente la partenza del pacman di sfondo e avvio il thread per il suo movimento
if (primoCicloMainMenu) {
thAggMainMenu.start();
primoCicloMainMenu = false;
partenzaPi = (short) (Math.random() * 4); // 0 = sx, 1 = dx, 2 = sup, 3 = inf
}
//disegno i fantasmini iniziali
if (partenzaPi == 0) {
//parte a sx
grafBuff.drawImage(imgFiR, xPi - 50, yPi, this);
grafBuff.drawImage(imgFiP, xPi - 100, yPi, this);
grafBuff.drawImage(imgFiG, xPi - 150, yPi, this);
grafBuff.drawImage(imgFiB, xPi - 200, yPi, this);
} else if (partenzaPi == 1) {
//parte a dx
grafBuff.drawImage(imgFiR, xPi + 50, yPi, this);
grafBuff.drawImage(imgFiP, xPi + 100, yPi, this);
grafBuff.drawImage(imgFiG, xPi + 150, yPi, this);
grafBuff.drawImage(imgFiB, xPi + 200, yPi, this);
} else if (partenzaPi == 2) {
//parte sopra
grafBuff.drawImage(imgFiR, xPi, yPi - 50, this);
grafBuff.drawImage(imgFiP, xPi, yPi - 100, this);
grafBuff.drawImage(imgFiG, xPi, yPi - 150, this);
grafBuff.drawImage(imgFiB, xPi, yPi - 200, this);
} else {
//parte sotto
grafBuff.drawImage(imgFiR, xPi, yPi + 50, this);
grafBuff.drawImage(imgFiP, xPi, yPi + 100, this);
grafBuff.drawImage(imgFiG, xPi, yPi + 150, this);
grafBuff.drawImage(imgFiB, xPi, yPi + 200, this);
}
//disegno il pacman
grafBuff.drawImage(imgPacMan, xPi + 5, yPi + 5, this);
//stampo il powered by
grafBuff.drawImage(imgPoweredBy, 380, 540, this);
g.drawImage(imgBuff, 0, -35, this);
//menu non abilitato quindi stampa il gioco
} else {
//ripulisco lo schermo
grafBuff.setColor(bgColor);
grafBuff.fillRect(0, 0, this.getWidth(), this.getHeight());
//nascondo i pulsanti del menu tranne quello del gioco scelto che però disabilito cosi viene visto nel menu in alto nel gioco
if (!initPacmanLikeGame) {
bStartPacManl1.hide();
} else {
bStartPacManl1.disable();
}
if (!initPacmanLikeGameL2) {
bStartPacManl2.hide();
} else {
bStartPacManl2.disable();
}
if (!initAutoPacmanGame) {
bStartAutoPacmanl1.hide();
} else {
bStartAutoPacmanl1.disable();
}
if (!initCasualGame) {
bCasualGame.hide();
} else {
bCasualGame.disable();
}
if (initPacmanLikeGame) {
//Trovo righe e colonne necessarie
numRighe = Integer.parseInt(tfRighe.getText());
numColonne = Integer.parseInt(tfColonne.getText());
this.setSize(1000, 600);
this.removeMouseListener(this);
this.removeMouseMotionListener(this);
//bgColor = Color.BLACK;
//wallColor = Color.BLUE;
streetColor = bgColor;
griglia = new Griglia(numRighe, numColonne);
griglia.generaMuriPacman();
menu = new GestioneCibo(numRighe, numColonne, griglia);
ghosts[ghostsCNT] = new Fantasmino(5, 8);
ghosts[ghostsCNT].setColor(Color.PINK);
ghostsCNT++;
ghosts[ghostsCNT] = new Fantasmino(5, 9);
ghosts[ghostsCNT].setColor(Color.RED);
ghostsCNT++;
ghosts[ghostsCNT] = new Fantasmino(5, 10);
ghosts[ghostsCNT].setColor(Color.BLUE);
ghostsCNT++;
ghosts[ghostsCNT] = new Fantasmino(5, 11);
ghosts[ghostsCNT].setColor(Color.ORANGE);
ghostsCNT++;
bbcman.setPos(7, 10);
calcolaMappatura(); //calcolo il numerino che indica i passi per ogni cella
thMovGhosts.start();
initPacmanLikeGame = false;
} else if (initPacmanLikeGameL2) {
this.setSize(1200, 800);
//Trovo righe e colonne necessarie
numRighe = Integer.parseInt(tfRighe.getText());
numColonne = Integer.parseInt(tfColonne.getText());
this.removeMouseListener(this);
this.removeMouseMotionListener(this);
//bgColor = Color.BLACK;
//wallColor = Color.BLUE;
streetColor = bgColor;
griglia = new Griglia(numRighe, numColonne);
griglia.generaMuriPacmanL2();
menu = new GestioneCibo(numRighe, numColonne, griglia);
ghosts[ghostsCNT] = new Fantasmino(8, 9);
ghosts[ghostsCNT].setColor(Color.PINK);
ghostsCNT++;
ghosts[ghostsCNT] = new Fantasmino(8, 10);
ghosts[ghostsCNT].setColor(Color.RED);
ghostsCNT++;
ghosts[ghostsCNT] = new Fantasmino(8, 11);
ghosts[ghostsCNT].setColor(Color.BLUE);
ghostsCNT++;
ghosts[ghostsCNT] = new Fantasmino(8, 12);
ghosts[ghostsCNT].setColor(Color.ORANGE);
ghostsCNT++;
bbcman.setPos(10, 10);
calcolaMappatura(); //calcolo il numerino che indica i passi per ogni cella
thMovGhosts.start();
initPacmanLikeGameL2 = false;
} else if (initAutoPacmanGame) {
this.setSize(600, 600);
//Trovo righe e colonne necessarie
numRighe = Integer.parseInt(tfRighe.getText());
numColonne = Integer.parseInt(tfColonne.getText());
streetColor = bgColor;
griglia = new Griglia(numRighe, numColonne);
initAutoPacmanGame = false;
} else if (initCasualGame) {
this.setSize(800, 600);
//Trovo righe e colonne necessarie
numRighe = Integer.parseInt(tfRighe.getText());
numColonne = Integer.parseInt(tfColonne.getText());
streetColor = bgColor;
griglia = new Griglia(numRighe, numColonne);
griglia.generaMuriBordo();
bbcman.setPos(1, 1);
ghosts[ghostsCNT] = new Fantasmino(numRighe - 2, numColonne - 2);
ghostsCNT++;
thMovGhosts.start();
maxGhosts = 50;
initCasualGame = false;
}
if (initGame) {
this.addKeyListener(this);
//aggiungo i pulsanti al contesto grafico
//this.add(bAvanti);
this.add(lColonne);
this.add(tfColonne);
this.add(lRighe);
this.add(tfRighe);
this.add(lCntFantasmini);
this.add(lPunteggio);
//lPunteggio.setBounds(lPunteggio.getX(), lPunteggio.getY(), lPunteggio.getWidth() + 15, lPunteggio.getHeight()); // NON VA
//richiedo il focus per il funzionamento di keylistener
this.requestFocus();
//inizio la canzone di sottofondo del gioco
songMainMenu.stop();
songBase.loop();
initGame = false;
}
//coloro il background del campo
grafBuff.setColor(bgColor);
grafBuff.fillRect(0, 35, this.getWidth(), this.getHeight());
//riscrivo il coteggio fantasmini nel menu superiore
lCntFantasmini.setText("Num fantasmini: " + ghostsCNT);
lPunteggio.setText("Punteggio: " + (int) punteggio);
///////////////////////////////////////////
//DECIDO IL LATO DEL QUDRATO IN MODOD OTTIMALE
//In piu decido gli offset per la centratura
///////////////////////////////////////////
if (numRighe > numColonne && contextH > contextW) {
lato = contextW / numColonne;
if (lato * numRighe > contextH) {
lato = contextH / numRighe;
}
} else if (numRighe > numColonne && contextH < contextW) {
lato = contextH / numRighe;
} else if (numRighe < numColonne && contextH > contextW) {
lato = contextW / numColonne;
} else { //righe < colonne && contH < contW
lato = contextH / numRighe;
if (lato * numColonne > contextW) {
lato = contextW / numColonne;
}
}
///////////////////////////////////////////
//DISEGNO LA GRIGLIA DI BASE CON CELLE
//VUOTE O MURI
///////////////////////////////////////////
grafBuff.drawLine(0, 35, this.getWidth(), 35);
for (int r = 0; r < numRighe; r++) {
for (int c = 0; c < numColonne; c++) {
if (!griglia.isMuro(r, c)) {
grafBuff.setColor(streetColor);
grafBuff.fillRect(offsetX() + (c * lato), offsetY() + (r * lato), lato, lato);
} else {
grafBuff.setColor(wallColor);
grafBuff.fill3DRect(offsetX() + (c * lato), offsetY() + (r * lato), lato, lato, true);
}
//stampo il numero di Passi nella cella
//grafBuff.setColor(Color.DARK_GRAY);
//grafBuff.drawString(Integer.toString(griglia.getPassi(r, c)), offsetX() + (c*lato), 10 + offsetY() + (r*lato));
}
}
///////////////////////////////////////////
//DISEGNO LA GRIGLIA DI BASE CON CELLE
//VUOTE O MURI
///////////////////////////////////////////
grafBuff.drawLine(0, 35, this.getWidth(), 35);
for (int r = 0; r < numRighe; r++) {
for (int c = 0; c < numColonne; c++) {
if (!griglia.isMuro(r, c)) {
grafBuff.setColor(streetColor);
grafBuff.fillRect(offsetX() + (c * lato), offsetY() + (r * lato), lato, lato);
} else {
grafBuff.setColor(wallColor);
grafBuff.fillRect(offsetX() + (c * lato), offsetY() + (r * lato), lato, lato);
}
//stampo il numero di Passi nella cella
//grafBuff.setColor(Color.DARK_GRAY);
//grafBuff.drawString(Integer.toString(griglia.getPassi(r, c)), offsetX() + (c*lato), 10 + offsetY() + (r*lato));
}
}
///////////////////////////////////////////
//DISEGNO IL CIBO
///////////////////////////////////////////
//non viene eseguito se autopacman è in aseguzione quindi si gioca senza cibo
if (!autoPacman) {
for (int r = 0; r < numRighe; r++) {
for (int c = 0; c < numColonne; c++) {
if (!autoPacman && !menu.ciboIsMangiato(r, c)) {
grafBuff.drawImage(imgCiboBase, offsetX() + (c * lato) + lato / 2, offsetY() + (r * lato) + lato / 2, this);
}
}
}
}
///////////////////////////////////////////
//DISEGNO I FANTASMINI
///////////////////////////////////////////
for (int i = 0; i < ghostsCNT; i++) {
if (ghosts[i].getColor() == Color.RED) {
//imgScaledToDraw = imgFantRed.getScaledInstance(100, 100, Image.SCALE_FAST); PROBLEMA: //http://stackoverflow.com/questions/7252983/resizing-image-java-getscaledinstance
grafBuff.drawImage(imgFantRed, offsetX() + lato * ghosts[i].getPosC(), offsetY() + lato * ghosts[i].getPosR(), this);
} else if (ghosts[i].getColor() == Color.BLUE) {
grafBuff.drawImage(imgFantBlue, offsetX() + lato * ghosts[i].getPosC(), offsetY() + lato * ghosts[i].getPosR(), this);
} else if (ghosts[i].getColor() == Color.ORANGE) {
grafBuff.drawImage(imgFantOrange, offsetX() + lato * ghosts[i].getPosC(), offsetY() + lato * ghosts[i].getPosR(), this);
} else if (ghosts[i].getColor() == Color.PINK) {
grafBuff.drawImage(imgFantPink, offsetX() + lato * ghosts[i].getPosC(), offsetY() + lato * ghosts[i].getPosR(), this);
} else {
System.out.println("Errore nel disegno del fantasmino: " + i);
}
}
///////////////////////////////////////////
//DISEGNO bbcman
///////////////////////////////////////////
grafBuff.drawImage(imgPacMan, offsetX() + lato * bbcman.getPosC() + 8, offsetY() + lato * bbcman.getPosR() + 8, this);
//visualizzo l og image creata
g.drawImage(imgBuff, 0, 0, this);
//Quando si perde
if (youLoose() && thMovGhosts.isAlive()) {
songBase.stop();
thMovGhosts.stop();
thWallLamp.start();
System.out.println("GAMEOVER!");
this.removeKeyListener(this);
songYouLoose.play();
}
//Quando si vince
if (youWin() && thMovGhosts.isAlive()) {
songBase.stop();
thMovGhosts.stop();
thWallLamp.start();
System.out.println("YOUWIN!");
this.removeKeyListener(this);
songYouWin.play();
}
}
}
///////////////////////////////////////////
//METODI GENERALI
///////////////////////////////////////////
//controlla se si è stati mangiati dai mantasmini quindi si ha perso
private boolean youLoose() {
if (cellaSenzaFant(bbcman.getPosR(), bbcman.getPosC())) {
return false;
} else {
return true;
}
}
//controlla se si ha mangiato tutti i cibi quindi si ha vinto
private boolean youWin() {
if (!autoPacman && menu.getCiboRimasto() == 0) {
return true;
} else {
return false;
}
}
private int offsetX() {
return (contextW - (lato * numColonne)) / 2;
}
private int offsetY() {
return (50 + (((contextH) - (numRighe * lato)) / 2)) - 1;
}
private void calcolaMappatura() {
griglia.mappatura(bbcman);
}
//setta le variabili globali rCella e cCella
private void trovaCella(int coordX, int coordY) {
//riga cliccata esegue solo se si ha cliccato sulla griglia
if (coordY > offsetY() && coordY < offsetY() + lato * numRighe) {
for (int i = 0; i < numRighe; i++) {
if (offsetY() + lato * i < coordY) {
rCella = i;
}
}
}
//colonna cliccata esegue solo se si ha cliccato sulla griglia
if (coordX > offsetX() && coordX < offsetX() + lato * numColonne) {
for (int i = 0; i < numColonne; i++) {
if (offsetX() + lato * i < coordX) {
cCella = i;
}
}
}
}
//ritorna true se nella cella non ci sono muri
private boolean cellaSenzaMuri(int r, int c) {
if (griglia.isMuro(r, c)) {
return false;
} else {
return true;
}
}
//ritorna true se nella cella cliccata non è gia presente un fantasmino oppure pacman
private boolean cellaIsLibera(MouseEvent e) {
trovaCella(e.getX(), e.getY());
//controllo se la cella cliccata contine un pacman
if (rCella == bbcman.getPosR() && cCella == bbcman.getPosC()) {
return false;
} //controllo che non ci siano altri fantasmini
else if (!cellaSenzaFant(rCella, cCella)) {
return false;
} else {
return menu.ciboIsMangiato(rCella, cCella);
}
}
//controlla che nella cella passata non ci sia nessun fantasmino NON FUNZIONA AL 100% in muoviFantasmini()
private boolean cellaSenzaFant(int nR, int nC) {
for (int iFa = 0; iFa < ghostsCNT; iFa++) {
if (ghosts[iFa].getPosC() == nC && ghosts[iFa].getPosR() == nR) {
return false;
}
}
return true;
}
private void muoviFantasmini() {
//Calcolo il prossimo passo di ogni singolo
for (int iF = 0; iF < ghostsCNT; iF++) {
int rFant = ghosts[iF].getPosR();
int cFant = ghosts[iF].getPosC();
//ogni if ontrolla anche se non si va aout of bound
//se è possibile vado a destra, c+1
//pois dopo controllo che non ci siano gia altri fantasmini in quella cella se no non avanza li
if (cFant < numColonne - 1 && !griglia.isMuro(rFant, cFant + 1) && griglia.getPassi(rFant, cFant) > griglia.getPassi(rFant, cFant + 1) && cellaSenzaFant(rFant, cFant + 1)) {
ghosts[iF].goRight();
punteggio += 50;
} //se è possibile vado a sinistra, c-1
else if (cFant > 0 && !griglia.isMuro(rFant, cFant - 1) && griglia.getPassi(rFant, cFant) > griglia.getPassi(rFant, cFant - 1) && cellaSenzaFant(rFant, cFant - 1)) {
ghosts[iF].goLeft();
punteggio += 50;
} //se è possibile vado in giu, r+1
else if (rFant < numRighe - 1 && !griglia.isMuro(rFant + 1, cFant) && griglia.getPassi(rFant, cFant) > griglia.getPassi(rFant + 1, cFant) && cellaSenzaFant(rFant + 1, cFant)) {
ghosts[iF].goDown();
punteggio += 50;
} //se è possibile vado in alto, r-1
else if (rFant > 0 && !griglia.isMuro(rFant - 1, cFant) && griglia.getPassi(rFant, cFant) > griglia.getPassi(rFant - 1, cFant) && cellaSenzaFant(rFant - 1, cFant)) {
ghosts[iF].goUp();
punteggio += 50;
} else {
}
}
songMovFant.play();
}
public void run() {
//avvicino i fantasmini
while (thMovGhosts.isAlive()) {
//se necessario aumento velocità
if (Math.floor((punteggio - punteggioRel) / 1000) > 0 && msMovGhosts > 400) {
//se il punteggio è multiplo di 100 diminuisco il delay tra un passo e l altro
//di 200ms per un minimo di 400ms, visto che il calcolo viene fatto solo
//quando i fantasmini si muovono, nel frattempo è possbile fare
//piu punti di un ultiplo di 100 quindi lo tengo presento nel calcolo dopo
// in piu aggiungo un fantasmino
msMovGhosts -= 100 * Math.floor((punteggio - punteggioRel) / 1000);
punteggioRel = punteggio;
}
//se non ho raggiunto il numero massimo di fantasmini ne aggiungo 1
//questa aggiunta avviene ogni volta che viene eseguito un passo e il punteggio è multiplo di: 100 per il num di fantasmini gia presenti + 1000 * num di fantasmini
if (ghostsCNT < maxGhosts && punteggio / (500 * Math.pow(ghostsCNT, 2)) > 1) {
//genero un fantasmino a una coordinata casuale che non è un muro o non ce pacman
int rNF;
int cNF;
do {
rNF = (int) (Math.random() * numRighe - 2) + 1;
cNF = (int) (Math.random() * numColonne - 2) + 1;
} while (griglia.isMuro(rNF, cNF) || (rNF == bbcman.getPosR() && cNF == bbcman.getPosC()));
ghosts[ghostsCNT] = new Fantasmino(rNF, cNF);
ghostsCNT++;
System.out.println("fant n: " + ghostsCNT);
}
try {
Thread.sleep(msMovGhosts);
muoviFantasmini();
repaint();
} catch (InterruptedException e) {
System.out.println("InterruptedExceptionGhost");
}
}
//togglo lo stato dei muri
while (thWallLamp.isAlive()) {
//togla lo stato della cella se non vi è dentro un pacman o una fantasmino
for (int r = 0; r < numRighe; r++) {
for (int c = 0; c < numColonne; c++) {
if (!(r == bbcman.getPosR() && c == bbcman.getPosC()) && cellaSenzaFant(r, c)) {
griglia.toggleStato(r, c);
}
}
}
try {
Thread.sleep(600);
repaint();
} catch (InterruptedException e) {
System.out.println("InterruptedExceptionWall");
}
}
//togglo lo stato dei muri
while (thAggMainMenu.isAlive()) {
//La seguente struttura if serve a generre la posizione iniziale del pacman del menu
if (xPi == 0 - 250 || xPi == this.getWidth() + 200 || yPi == 0 - 200 || yPi == this.getHeight() + 200) {
partenzaPi = (short) (Math.round(Math.random() * 4)); // 0 = sx, 1 = dx, 2 = sup, 3 = inf
if (partenzaPi == 0) {
//parte a sx
xPi = 1;
yPi = (int) ((Math.random() + 0.1) * (this.getHeight() - 200));
imgPacMan = getImage(getCodeBase(), "pacmanDx.gif");
} else if (partenzaPi == 1) {
//parte a dx
xPi = this.getWidth() - 1;
yPi = (int) ((Math.random() + 0.1) * (this.getHeight() - 200));
imgPacMan = getImage(getCodeBase(), "pacmanSx.gif");
} else if (partenzaPi == 2) {
//parte sopra
xPi = (int) ((Math.random() + 0.1) * (this.getWidth() - 200));
yPi = 1;
imgPacMan = getImage(getCodeBase(), "pacmanGiu.gif");
} else {
//parte sotto
xPi = (int) ((Math.random() + 0.1) * (this.getWidth() - 200));
yPi = this.getHeight() - 1;
imgPacMan = getImage(getCodeBase(), "pacmanUp.gif");
}
}
try {
Thread.sleep(5);
if (partenzaPi == 0) {
//parte a sx
xPi += 1;
} else if (partenzaPi == 1) {
//parte a dx
xPi -= 1;
} else if (partenzaPi == 2) {
//parte sopra
yPi += 1;
} else {
//parte sotto
yPi -= 1;
}
repaint();
} catch (InterruptedException e) {
System.out.println("InterruptedExceptionWall");
}
}
}
///////////////////////////////////////////
//METODI INTERFACE UTILIZZATI
///////////////////////////////////////////
public void mouseDragged(MouseEvent e) {
if (thMovGhosts.isAlive() && e.getX() > offsetX() && e.getX() < offsetX() + lato * numColonne && e.getY() > offsetY() && e.getY() < offsetY() + lato * numRighe) {
if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
if (autoPacman || cellaIsLibera(e)) {
trovaCella(e.getX(), e.getY());
griglia.setStato(rCella, cCella, "muro");
calcolaMappatura();
repaint();
}
}
}
}
public void mouseClicked(MouseEvent e) {
//imposto il focus all applet se premo nella parte sotto il menu
if (e.getX() > 50) {
this.requestFocus();
}
trovaCella(e.getX(), e.getY());
//esegue solo se si ha cliccato sulla griglia, creo o cancello muro oppure se sto eseguento il gioco con autopacman
if (autoPacman || cellaIsLibera(e)) {
if (thMovGhosts.isAlive() && e.getX() > offsetX() && e.getX() < offsetX() + lato * numColonne && e.getY() > offsetY() && e.getY() < offsetY() + lato * numRighe) {
//se clicco col sx
//togglo lo stato della cella
//in piu controllo se il thread principale del gioco è attivo
if (e.getButton() == MouseEvent.BUTTON1) {
griglia.toggleStato(rCella, cCella);
calcolaMappatura(); //ricalcolo la mappatura
System.out.println("clicksx " + rCella + " " + cCella);
repaint();
}
if (!griglia.isMuro(rCella, cCella)) {
//se clicco con la rotella del mouse e se non è gia muro, sposto pacman
if (thMovGhosts.isAlive() && e.getButton() == MouseEvent.BUTTON2) {
bbcman.setPos(rCella, cCella);
calcolaMappatura(); //ricalcolo la mappatura
repaint();
}
//se clicco con tasto dx e non è gia muro creao un fantasmino
//e se non si ha raggiunto il num massimo di fantasmino
if (thMovGhosts.isAlive() && e.getButton() == MouseEvent.BUTTON3 && ghostsCNT < maxGhosts) {
ghosts[ghostsCNT] = new Fantasmino(rCella, cCella);
ghostsCNT++;
repaint();
}
}
}
}
}
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 'a' || e.getKeyChar() == 'A') {
//aggiungo i controlli di limite della griglia e muri
if (bbcman.getPosC() - 1 >= 0) {
if (!griglia.isMuro(bbcman.getPosR(), bbcman.getPosC() - 1)) {
bbcman.goLeft();
imgPacMan = getImage(getCodeBase(), "pacmanSx.gif");
//setto come mangiato i pallino presente nella nuova posizione di pacman
//eseguo il suono di nutrimento
if (!autoPacman && !menu.ciboIsMangiato(bbcman.getPosR(), bbcman.getPosC())) {
menu.ciboMangiato(bbcman.getPosR(), bbcman.getPosC());
songCiboMangiato.play();
punteggio += menu.getValore(bbcman.getPosR(), bbcman.getPosC());
}
calcolaMappatura(); //ricalcolo la mappatura
repaint();
}
}
} else if (e.getKeyChar() == 's' || e.getKeyChar() == 'S') {
//aggiungo i controlli di limite della griglia e muri
if (bbcman.getPosR() + 1 < numRighe) {
if (!griglia.isMuro(bbcman.getPosR() + 1, bbcman.getPosC())) {
bbcman.goDown();
imgPacMan = getImage(getCodeBase(), "pacmanGiu.gif");
if (!autoPacman && !menu.ciboIsMangiato(bbcman.getPosR(), bbcman.getPosC())) {
menu.ciboMangiato(bbcman.getPosR(), bbcman.getPosC());
songCiboMangiato.play();
punteggio += menu.getValore(bbcman.getPosR(), bbcman.getPosC());
}
calcolaMappatura(); //ricalcolo la mappatura
repaint();
}
}
}
if (e.getKeyChar() == 'd' || e.getKeyChar() == 'D') {
//aggiungo i controlli di limite della griglia e muri
if (bbcman.getPosC() + 1 < numColonne) {
if (!griglia.isMuro(bbcman.getPosR(), bbcman.getPosC() + 1)) {
bbcman.goRight();
imgPacMan = getImage(getCodeBase(), "pacmanDx.gif");
if (!autoPacman && !menu.ciboIsMangiato(bbcman.getPosR(), bbcman.getPosC())) {
menu.ciboMangiato(bbcman.getPosR(), bbcman.getPosC());
songCiboMangiato.play();
punteggio += menu.getValore(bbcman.getPosR(), bbcman.getPosC());
}
calcolaMappatura(); //ricalcolo la mappatura
repaint();
}
}
} else if (e.getKeyChar() == 'w' || e.getKeyChar() == 'W') {
//aggiungo i controlli di limite della griglia e muri
if (bbcman.getPosR() - 1 >= 0) {
if (!griglia.isMuro(bbcman.getPosR() - 1, bbcman.getPosC())) {
bbcman.goUp();
imgPacMan = getImage(getCodeBase(), "pacmanUp.gif");
if (!autoPacman && !menu.ciboIsMangiato(bbcman.getPosR(), bbcman.getPosC())) {
menu.ciboMangiato(bbcman.getPosR(), bbcman.getPosC());
songCiboMangiato.play();
punteggio += menu.getValore(bbcman.getPosR(), bbcman.getPosC());
}
calcolaMappatura(); //ricalcolo la mappatura
repaint();
}
}
} else {//fa niente
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bStartPacManl1) {
mainMenuEnabled = false;
initPacmanLikeGame = true;
tfRighe.setText("11");
tfColonne.setText("20");
thAggMainMenu.stop();
repaint();
}
if (e.getSource() == bStartPacManl2) {
tfRighe.setText("16");
tfColonne.setText("21");
mainMenuEnabled = false;
initPacmanLikeGameL2 = true;
thAggMainMenu.stop();
repaint();
}
if (e.getSource() == bStartAutoPacmanl1) {
mainMenuEnabled = false;
initAutoPacmanGame = true;
autoPacman = true;
tfRighe.setText("10");
tfColonne.setText("10");
thAggMainMenu.stop();
repaint();
}
if (e.getSource() == bCasualGame) {
mainMenuEnabled = false;
initCasualGame = true;
autoPacman = true; // non è verò pero almeno non carica i cibi
tfRighe.setText("11");
tfColonne.setText("14");
thAggMainMenu.stop();
repaint();
}
}
///////////////////////////////////////////
//METODI INTERFACE NON UTILIZZATI
///////////////////////////////////////////
public void mouseMoved(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}

67
src/pacman/PacMan.java Normal file
View file

@ -0,0 +1,67 @@
/*
* Questa classe gestisce il pac-man
*/
package pacman;
/**
*
* @author BBC
*/
public class PacMan {
private int posR;
private int posC;
///////////////////////////////////////////
//COSTRUTTORI
///////////////////////////////////////////
public PacMan(){
}
public PacMan(int r, int c){
this.posR = r;
this.posC = c;
}
///////////////////////////////////////////
//SET DI TUTTI GLI ATTRIBUTI
///////////////////////////////////////////
public void setPosR(int posR){
this.posR = posR;
}
public void setPosC(int posC){
this.posC = posC;
}
public void setPos(int posR, int posC){
this.posR = posR;
this.posC = posC;
}
///////////////////////////////////////////
//GET DI TUTTI GLI ATTRIBUTI
///////////////////////////////////////////
public int getPosR(){
return this.posR;
}
public int getPosC(){
return this.posC;
}
///////////////////////////////////////////
//METODI GENERALI
///////////////////////////////////////////
///////////////////////////////////////////
//METODI DI SPOSTAMENTO
///////////////////////////////////////////
public void goLeft(){
this.posC--;
}
public void goRight(){
this.posC++;
}
public void goUp(){
this.posR--;
}
public void goDown(){
this.posR++;
}
}

BIN
src/pacman/pmsongs/Rage.mid Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.