fork download
  1. package com.mygdx.game;
  2.  
  3. import com.badlogic.gdx.ApplicationAdapter;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8. import com.badlogic.gdx.graphics.g2d.Animation;
  9. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  10. import com.badlogic.gdx.Input.Keys;
  11. import java.io.File;
  12. import java.util.Scanner;
  13. import java.io.FileNotFoundException;
  14.  
  15. public class Pacman extends ApplicationAdapter {
  16. SpriteBatch batch;
  17. Texture sprites;
  18. TextureRegion pacmanTxt[][]=new TextureRegion[4][3];
  19. TextureRegion ghostsTxt[][][]= new TextureRegion[4][4][2];
  20. TextureRegion characters[]=new TextureRegion[44];
  21. Animation pacmanAnim[] = new Animation [4];
  22. Animation ghostsAnim[][] = new Animation [4][4];
  23. float ti=0;
  24. public int extX=0,extY=0;
  25. Texture world;
  26. Actor pacman;
  27. Actor ghosts[] = new Actor[4];
  28. int worldDesc[][];
  29. public int w,h;
  30.  
  31. public int getTileFromX(int x){
  32. return (x-extX)/8;}
  33.  
  34. public int getTileFromY(int y){
  35. int temp=(y-extY)/8;
  36. return h-1-temp;}
  37.  
  38. public class Actor //HERE IS THE NESTED CLASS
  39. {
  40. public static final int LEFT=0,RIGHT=2,UP=1,DOWN=3;
  41. public int x,y,d,s;
  42. Animation pose[];
  43. public Actor (int x,int y,int d,int s,Animation [] pose)
  44. {
  45. this.x=x;
  46. this.y=y;
  47. this.d=d;
  48. this.s=s;
  49. this.pose=pose;
  50. }
  51. public Actor (Animation[] pose)
  52. {
  53. this.x=0;
  54. this.y=0;
  55. this.d=0;
  56. this.s=1;
  57. this.pose=pose;
  58. }
  59. public Actor (int s,Animation[] pose)
  60. {
  61. this.x=0;
  62. this.y=0;
  63. this.d=0;
  64. this.s=s;
  65. this.pose=pose;
  66. }
  67. public int getX(){
  68. return x;}
  69. public int getY(){
  70. return y;}
  71. public int getRealX(){
  72. return x-7;}
  73. public int getRealY(){
  74. return y-7;}
  75. public Animation getMovingAnimation(){
  76. return pose[d];}
  77. public Animation getAnimation(int x){
  78. return pose[x];}
  79. public int getTileX(){
  80. return (x-extX)/8;}
  81. public int getTileY(){
  82. int temp=(y-extY)/8;
  83. return h-1-temp;}
  84. public void goLeft(){
  85. d=0;}
  86. public void goRight(){
  87. d=2;}
  88. public void goUp(){
  89. d=1;}
  90. public void goDown(){
  91. d=3;}
  92. public void walk()
  93. {
  94. switch (this.d)
  95. {
  96. case Actor.LEFT:
  97. {
  98. int valX,actX,actY,newX;
  99. actX=this.getTileX();
  100. actY=this.getTileY();
  101. newX=getTileFromX(this.x-this.s);
  102. for (valX=actX;valX>=newX;valX--)
  103. {
  104. if (!isAValidTile(valX,actY))
  105. {
  106. valX++;
  107. break;
  108. }
  109. }
  110. this.x=Math.max(this.x-this.s, valX*8+extX);
  111. break;
  112. }
  113. case Actor.RIGHT:
  114. {
  115. int valX,actX,actY,newX;
  116. actX=this.getTileX();
  117. actY=this.getTileY();
  118. newX=getTileFromX(this.x+this.s);
  119. for (valX=actX;valX<=newX;valX++)
  120. {
  121. if (!isAValidTile(valX,actY))
  122. {
  123. valX--;
  124. break;
  125. }
  126. }
  127. this.x=Math.min(this.x+this.s, (valX+1)*8-1+extX);
  128. break;
  129. }
  130. }
  131. }
  132. }
  133.  
  134. public boolean isAValidTile(int x,int y)
  135. {
  136. if ((x>=0) && (y>=0) && (x<w) && (y<h))
  137. return worldDesc[x][y]!=1;
  138. return false;
  139. }
  140. public void setAnimations()
  141. {
  142. sprites = new Texture ("sprites.png");
  143. int corr[] = {2,0,3,1};
  144. int corr2[] = {2,0,1};
  145. int i,j,k;
  146. for (i=0;i<4;i++)
  147. for (j=0;j<3;j++)
  148. pacmanTxt[i][j] = new TextureRegion(sprites,16*corr2[j],16*corr[i],16,16);
  149. for (i=0;i<4;i++)
  150. {
  151. pacmanAnim[i]=new Animation ((float) (1.0/15.0),pacmanTxt[i]);
  152. pacmanAnim[i].setPlayMode(Animation.PlayMode.LOOP_PINGPONG);
  153. }
  154. pacman = new Actor (13*8+3+4,7*8+3,0,3,pacmanAnim);
  155. for (i=0;i<4;i++)
  156. for (j=0;j<4;j++)
  157. for (k=0;k<2;k++)
  158. ghostsTxt[i][j][k]=new TextureRegion(sprites,16*2*i+16*k,16*4+16*corr[j],16,16);
  159. for (i=0;i<4;i++)
  160. for (j=0;j<4;j++)
  161. {
  162. ghostsAnim[i][j]=new Animation((float) (2.0/15.0),ghostsTxt[i][j]);
  163. ghostsAnim[i][j].setPlayMode(Animation.PlayMode.LOOP);
  164. }
  165. for (i=0;i<4;i++)
  166. ghosts[i] = new Actor (3,ghostsAnim[i]);
  167. j=0;
  168. for (i=0;i<18;i++)
  169. {
  170. characters[j]=new TextureRegion(sprites,8*16+i*8,0,8,8);
  171. j++;
  172. }
  173. for (i=0;i<18;i++)
  174. {
  175. characters[j]=new TextureRegion(sprites,8*16+i*8,8,8,8);
  176. j++;
  177. }
  178. for (i=0;i<5;i++)
  179. {
  180. characters[j]=new TextureRegion(sprites,8*16+i*8,16,8,8);
  181. j++;
  182. }
  183. for (i=0;i<2;i++)
  184. {
  185. characters[j]=new TextureRegion(sprites,8*16+18*8,i*8,8,8);
  186. j++;
  187. }
  188. characters[43]=new TextureRegion(sprites,0,0,1,1);
  189. }
  190. public void setBackground()
  191. {
  192. try
  193. {
  194. Scanner in = new Scanner (new File ("world.txt"));
  195. h = in.nextInt();
  196. w = in.nextInt();
  197. int i,j;
  198. worldDesc=new int[h][w];
  199. for (i=0;i<h;i++)
  200. {
  201. for (j=0;j<w;j++)
  202. {
  203. worldDesc[i][j]=in.nextInt();
  204. }
  205. }
  206. world = new Texture ("map.png");
  207. }
  208. {
  209.  
  210. }
  211. }
  212. public void write(String cad,int x,int y)
  213. {
  214. int i,p;
  215. cad=cad.toUpperCase();
  216. for (i=0;i<cad.length();i++)
  217. {
  218. p=43;
  219. if ((cad.charAt(i)<='9')&&(cad.charAt(i)>='0'))
  220. p=(int)cad.charAt(i)-(int)'0';
  221. if ((cad.charAt(i)<='Z')&&(cad.charAt(i)>='A'))
  222. p=(int)cad.charAt(i)-(int)'A'+10;
  223. if (cad.charAt(i)=='.')
  224. p=36;
  225. if (cad.charAt(i)=='!')
  226. p=37;
  227. batch.draw(characters[p],x+i*8,y);
  228. }
  229.  
  230. }
  231.  
  232.  
  233. @Override
  234. public void create () {
  235. batch = new SpriteBatch();
  236. setBackground();
  237. setAnimations();
  238. }
  239.  
  240. @Override
  241. public void render () {
  242. ti+=Gdx.graphics.getDeltaTime();
  243. pacman.walk();
  244. Gdx.gl.glClearColor(0, 0, 0, 1);
  245. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  246.  
  247. if (Gdx.input.isKeyJustPressed(Keys.DPAD_LEFT))
  248. {
  249. pacman.goLeft();
  250. for (int i =0;i<4;i++)
  251. ghosts[i].goLeft();
  252. }
  253. if (Gdx.input.isKeyJustPressed(Keys.DPAD_RIGHT))
  254. {
  255. pacman.goRight();
  256. for (int i =0;i<4;i++)
  257. ghosts[i].goRight();
  258. }
  259. if (Gdx.input.isKeyJustPressed(Keys.DPAD_UP))
  260. {
  261. pacman.goUp();
  262. for (int i =0;i<4;i++)
  263. ghosts[i].goUp();
  264. }
  265. if (Gdx.input.isKeyJustPressed(Keys.DPAD_DOWN))
  266. {
  267. pacman.goDown();
  268. for (int i =0;i<4;i++)
  269. ghosts[i].goDown();
  270. }
  271.  
  272. int i;
  273. batch.begin();
  274. //batch.draw(img, 0, 0);
  275. batch.draw(world,0,0);
  276. batch.draw(pacman.getMovingAnimation().getKeyFrame(ti),pacman.getRealX(),pacman.getRealY());
  277. for (i=0;i<4;i++)
  278. batch.draw(ghosts[i].getMovingAnimation().getKeyFrame(ti),120+i*20,100);
  279. write("alto: " + h,300,300+8);
  280. write("tile actual: "+pacman.getTileX()+ " , " + pacman.getTileY(),300,300);
  281. write("coordenada actual: "+pacman.x+ " , " + pacman.y,300,300-8);
  282. batch.end();
  283.  
  284. }
  285. }
  286.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:15: error: class Pacman is public, should be declared in a file named Pacman.java
public class Pacman extends ApplicationAdapter {
       ^
Main.java:3: error: package com.badlogic.gdx does not exist
import com.badlogic.gdx.ApplicationAdapter;
                       ^
Main.java:4: error: package com.badlogic.gdx does not exist
import com.badlogic.gdx.Gdx;
                       ^
Main.java:5: error: package com.badlogic.gdx.graphics does not exist
import com.badlogic.gdx.graphics.GL20;
                                ^
Main.java:6: error: package com.badlogic.gdx.graphics does not exist
import com.badlogic.gdx.graphics.Texture;
                                ^
Main.java:7: error: package com.badlogic.gdx.graphics.g2d does not exist
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
                                    ^
Main.java:8: error: package com.badlogic.gdx.graphics.g2d does not exist
import com.badlogic.gdx.graphics.g2d.Animation;
                                    ^
Main.java:9: error: package com.badlogic.gdx.graphics.g2d does not exist
import com.badlogic.gdx.graphics.g2d.TextureRegion;
                                    ^
Main.java:10: error: package com.badlogic.gdx.Input does not exist
import com.badlogic.gdx.Input.Keys;
                             ^
Main.java:15: error: cannot find symbol
public class Pacman extends ApplicationAdapter {
                            ^
  symbol: class ApplicationAdapter
Main.java:16: error: cannot find symbol
	SpriteBatch batch;
	^
  symbol:   class SpriteBatch
  location: class Pacman
Main.java:17: error: cannot find symbol
	Texture sprites;
	^
  symbol:   class Texture
  location: class Pacman
Main.java:18: error: cannot find symbol
	TextureRegion pacmanTxt[][]=new TextureRegion[4][3];
	^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:19: error: cannot find symbol
	TextureRegion ghostsTxt[][][]= new TextureRegion[4][4][2];
	^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:20: error: cannot find symbol
	TextureRegion characters[]=new TextureRegion[44];
	^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:21: error: cannot find symbol
	Animation pacmanAnim[] = new Animation [4];
	^
  symbol:   class Animation
  location: class Pacman
Main.java:22: error: cannot find symbol
	Animation ghostsAnim[][] = new Animation [4][4];
	^
  symbol:   class Animation
  location: class Pacman
Main.java:25: error: cannot find symbol
	Texture world;
	^
  symbol:   class Texture
  location: class Pacman
Main.java:42: error: cannot find symbol
		Animation pose[];
		^
  symbol:   class Animation
  location: class Pacman.Actor
Main.java:43: error: cannot find symbol
		public Actor (int x,int y,int d,int s,Animation [] pose)
		                                      ^
  symbol:   class Animation
  location: class Pacman.Actor
Main.java:51: error: cannot find symbol
		public Actor (Animation[] pose)
		              ^
  symbol:   class Animation
  location: class Pacman.Actor
Main.java:59: error: cannot find symbol
		public Actor (int s,Animation[] pose)
		                    ^
  symbol:   class Animation
  location: class Pacman.Actor
Main.java:75: error: cannot find symbol
		public Animation getMovingAnimation(){
		       ^
  symbol:   class Animation
  location: class Pacman.Actor
Main.java:77: error: cannot find symbol
		public Animation getAnimation(int x){
		       ^
  symbol:   class Animation
  location: class Pacman.Actor
Main.java:18: error: cannot find symbol
	TextureRegion pacmanTxt[][]=new TextureRegion[4][3];
	                                ^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:19: error: cannot find symbol
	TextureRegion ghostsTxt[][][]= new TextureRegion[4][4][2];
	                                   ^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:20: error: cannot find symbol
	TextureRegion characters[]=new TextureRegion[44];
	                               ^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:21: error: cannot find symbol
	Animation pacmanAnim[] = new Animation [4];
	                             ^
  symbol:   class Animation
  location: class Pacman
Main.java:22: error: cannot find symbol
	Animation ghostsAnim[][] = new Animation [4][4];
	                               ^
  symbol:   class Animation
  location: class Pacman
Main.java:142: error: cannot find symbol
		sprites = new Texture ("sprites.png");
		              ^
  symbol:   class Texture
  location: class Pacman
Main.java:148: error: cannot find symbol
				pacmanTxt[i][j] = new TextureRegion(sprites,16*corr2[j],16*corr[i],16,16);
				                      ^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:151: error: cannot find symbol
			pacmanAnim[i]=new Animation ((float) (1.0/15.0),pacmanTxt[i]);
			                  ^
  symbol:   class Animation
  location: class Pacman
Main.java:152: error: package Animation does not exist
			pacmanAnim[i].setPlayMode(Animation.PlayMode.LOOP_PINGPONG);
			                                   ^
Main.java:158: error: cannot find symbol
				   ghostsTxt[i][j][k]=new TextureRegion(sprites,16*2*i+16*k,16*4+16*corr[j],16,16);
				                          ^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:162: error: cannot find symbol
				ghostsAnim[i][j]=new Animation((float) (2.0/15.0),ghostsTxt[i][j]);
				                     ^
  symbol:   class Animation
  location: class Pacman
Main.java:163: error: package Animation does not exist
				ghostsAnim[i][j].setPlayMode(Animation.PlayMode.LOOP);
				                                      ^
Main.java:170: error: cannot find symbol
			characters[j]=new TextureRegion(sprites,8*16+i*8,0,8,8);
			                  ^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:175: error: cannot find symbol
			characters[j]=new TextureRegion(sprites,8*16+i*8,8,8,8);
			                  ^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:180: error: cannot find symbol
			characters[j]=new TextureRegion(sprites,8*16+i*8,16,8,8);
			                  ^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:185: error: cannot find symbol
			characters[j]=new TextureRegion(sprites,8*16+18*8,i*8,8,8);
			                  ^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:188: error: cannot find symbol
		characters[43]=new TextureRegion(sprites,0,0,1,1);
		                   ^
  symbol:   class TextureRegion
  location: class Pacman
Main.java:206: error: cannot find symbol
			world = new Texture ("map.png");
			            ^
  symbol:   class Texture
  location: class Pacman
Main.java:234: error: method does not override or implement a method from a supertype
        @Override
        ^
Main.java:236: error: cannot find symbol
		batch = new SpriteBatch();
		            ^
  symbol:   class SpriteBatch
  location: class Pacman
Main.java:241: error: method does not override or implement a method from a supertype
	@Override
	^
Main.java:243: error: package Gdx does not exist
                ti+=Gdx.graphics.getDeltaTime();
                       ^
Main.java:245: error: package Gdx does not exist
		Gdx.gl.glClearColor(0, 0, 0, 1);
		   ^
Main.java:246: error: cannot find symbol
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		               ^
  symbol:   variable GL20
  location: class Pacman
Main.java:246: error: package Gdx does not exist
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		   ^
Main.java:248: error: cannot find symbol
                if (Gdx.input.isKeyJustPressed(Keys.DPAD_LEFT))
                                               ^
  symbol:   variable Keys
  location: class Pacman
Main.java:248: error: package Gdx does not exist
                if (Gdx.input.isKeyJustPressed(Keys.DPAD_LEFT))
                       ^
Main.java:254: error: cannot find symbol
                if (Gdx.input.isKeyJustPressed(Keys.DPAD_RIGHT))
                                               ^
  symbol:   variable Keys
  location: class Pacman
Main.java:254: error: package Gdx does not exist
                if (Gdx.input.isKeyJustPressed(Keys.DPAD_RIGHT))
                       ^
Main.java:260: error: cannot find symbol
                if (Gdx.input.isKeyJustPressed(Keys.DPAD_UP))
                                               ^
  symbol:   variable Keys
  location: class Pacman
Main.java:260: error: package Gdx does not exist
                if (Gdx.input.isKeyJustPressed(Keys.DPAD_UP))
                       ^
Main.java:266: error: cannot find symbol
                if (Gdx.input.isKeyJustPressed(Keys.DPAD_DOWN))
                                               ^
  symbol:   variable Keys
  location: class Pacman
Main.java:266: error: package Gdx does not exist
                if (Gdx.input.isKeyJustPressed(Keys.DPAD_DOWN))
                       ^
57 errors
stdout
Standard output is empty