Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. import java.awt.image.*;
  6. import java.io.*;
  7. import javax.imageio.*;
  8.  
  9. public class GraphicsMenu extends GameEngine {
  10. // Main Function
  11. public static void main(String args[]) {
  12. createGame(new GraphicsMenu());
  13. }
  14.  
  15. //-------------------------------------------------------
  16. // Your Program
  17. //-------------------------------------------------------
  18.  
  19. enum GameState {Menu, Playing, Options};
  20. GameState state = GameState.Menu;
  21.  
  22. int menuOption = 0;
  23.  
  24. //volume of background sound
  25. float volume = -4;
  26. float volume2 = -4;
  27.  
  28. Image play;
  29. Image playHighlighted;
  30. Image options;
  31. Image optionsHighlighted;
  32. Image exit;
  33. Image exitHighlighted;
  34.  
  35. Image menuBackground;
  36. Image test;
  37. Image test2;
  38. Image test3;
  39.  
  40. Image size1;
  41. Image size1Highlighted;
  42. Image size2;
  43. Image size2Highlighted;
  44.  
  45. Image hud;
  46.  
  47. AudioClip background;
  48. AudioClip beep;
  49. //Game variables
  50. int health;
  51. int tester, tester2;
  52.  
  53. public void init() {
  54. setWindowSize(1100, 650);
  55. play = loadImage("Play1.png");
  56. playHighlighted = loadImage("Playhighlighted.png");
  57. options = loadImage("Options1.png");
  58. optionsHighlighted = loadImage("Optionshighlighted.png");
  59. exit = loadImage("Exit1.png");
  60. exitHighlighted = loadImage("Exithighlighted.png");
  61.  
  62. menuBackground = loadImage("NewBackground.png");
  63. test = loadImage("NewBackground.png");
  64. test2 = loadImage("titlegame2.png");
  65. test3 = loadImage("backgroundtest.png");
  66.  
  67. size1 = loadImage("mutesound.png");
  68. size1Highlighted = loadImage("mutesoundhighlighted.png");
  69. size2 = loadImage("background1.png");
  70. size2Highlighted = loadImage("background1highlighted.png");
  71.  
  72. hud = loadImage("HUD.png");
  73.  
  74. tester = 0;
  75. tester2 = 0;
  76. // pull file and start background music loop
  77. background = loadAudio("pvzbackground.wav");
  78. beep = loadAudio("beep.wav");
  79. startAudioLoop(background, volume);
  80. }
  81.  
  82.  
  83. //Updates the display
  84. public void update(double dt) {
  85. if(state == GameState.Playing) {
  86. health--;
  87.  
  88. if(health <= 0) {
  89. state = GameState.Menu;
  90. }
  91. }
  92. }
  93.  
  94. //-------------------------------------------------------
  95. //This gets called any time the Operating System
  96. //tells the program to paint itself
  97. public void paintComponent() {
  98. changeBackgroundColor(white);
  99. clearBackground(width(), height());
  100.  
  101. if(state == GameState.Menu) {
  102. paintMenu();
  103. } else if(state == GameState.Options) {
  104. paintOptions();
  105. } else if(state == GameState.Playing) {
  106. paintGame();
  107. }
  108. }
  109.  
  110. //Function to paint the menu
  111. public void paintMenu() {
  112. if(tester == 0){
  113. //draw background
  114. drawImage(test, 0, 0, width(), height());
  115. //draw title
  116. drawImage(test2, 310, 120);
  117. }
  118.  
  119. if(tester == 1){
  120. //draw new background
  121. drawImage(test3, 0, 0);
  122. //draw title
  123. drawImage(test2, 310, 120);
  124. }
  125. //Play
  126. if(menuOption == 0) {
  127. drawImage(playHighlighted, 498, 200);
  128. } else {
  129. drawImage(play, 498, 200);
  130. }
  131.  
  132. //Options
  133. if(menuOption == 1) {
  134. drawImage(optionsHighlighted, 473, 250);
  135. } else {
  136. drawImage(options, 473, 250);
  137. }
  138.  
  139. //Exit
  140. if(menuOption == 2) {
  141. drawImage(exitHighlighted, 501, 300);
  142. } else {
  143. drawImage(exit, 501, 300);
  144. }
  145. }
  146.  
  147. //Function to paint the options menu
  148. public void paintOptions() {
  149. if(tester == 0){
  150. //draw background
  151. drawImage(test, 0, 0, width(), height());
  152. //draw title
  153. drawImage(test2, 310, 120);
  154. }
  155.  
  156. if(tester == 1){
  157. //draw new background
  158. drawImage(test3, 0, 0);
  159. //draw title
  160. drawImage(test2, 310, 120);
  161. }
  162.  
  163. //Play
  164. if(menuOption == 0) {
  165. drawImage(size1Highlighted, 438, 200);
  166. } else {
  167. drawImage(size1, 438, 200);
  168. }
  169.  
  170. //Options
  171. if(menuOption == 1) {
  172. drawImage(size2Highlighted, 384, 250);
  173. } else {
  174. drawImage(size2, 384, 250);
  175. }
  176.  
  177. //Exit
  178. if(menuOption == 2) {
  179. drawImage(exitHighlighted, 501, 300);
  180. } else {
  181. drawImage(exit, 501, 300);
  182. }
  183. }
  184.  
  185. //Function to paint the game
  186. public void paintGame() {
  187. //First part of game
  188.  
  189. }
  190.  
  191. public void paintHUD() {
  192. //Second part of game
  193.  
  194. }
  195.  
  196. //-------------------------------------------------------
  197. //Called whenever a key is pressed
  198. public void keyPressed(KeyEvent e) {
  199. if(state == GameState.Menu) {
  200. keyPressedMenu(e);
  201. playAudio(beep, volume2);
  202. } else if(state == GameState.Options) {
  203. keyPressedOptions(e);
  204. playAudio(beep, volume2);
  205. } else {
  206. keyPressedGame(e);
  207. playAudio(beep, volume2);
  208. }
  209. }
  210.  
  211. //Called whenever a key is pressed in the menu
  212. public void keyPressedMenu(KeyEvent e) {
  213. //Up Arrow
  214. if(e.getKeyCode() == KeyEvent.VK_UP) {
  215. if(menuOption > 0) {
  216. menuOption--;
  217. }
  218. }
  219. //Down Arrow
  220. if(e.getKeyCode() == KeyEvent.VK_DOWN) {
  221. if(menuOption < 2) {
  222. menuOption++;
  223. }
  224. }
  225. //Enter Key
  226. if(e.getKeyCode() == KeyEvent.VK_ENTER) {
  227. if(menuOption == 0) {
  228. //Start Game
  229. health = 100;
  230. state = GameState.Playing;
  231. } else if(menuOption == 1) {
  232. //Option Menu
  233. state = GameState.Options;
  234. menuOption = 0;
  235. } else {
  236. //Exit
  237. System.exit(0);
  238. }
  239. }
  240. }
  241.  
  242. //Called whenever a key is pressed in the options menu
  243. public void keyPressedOptions(KeyEvent e) {
  244. //Up Arrow
  245. if(e.getKeyCode() == KeyEvent.VK_UP) {
  246. if(menuOption > 0) {
  247. menuOption--;
  248. }
  249. }
  250. //Down Arrow
  251. if(e.getKeyCode() == KeyEvent.VK_DOWN) {
  252. if(menuOption < 2) {
  253. menuOption++;
  254. }
  255. }
  256. //Enter Key
  257. if(e.getKeyCode() == KeyEvent.VK_ENTER) {
  258. if(menuOption == 0) {
  259. //change sound
  260. stopAudioLoop(background);
  261. volume2 = -50;
  262. } else if(menuOption == 1) {
  263. //change background
  264. if(tester == 0){
  265. tester = 1;
  266. }else if(tester == 1){
  267. tester = 0;
  268. }
  269.  
  270. } else {
  271. //Exit
  272. state = GameState.Menu;
  273. menuOption = 0;
  274. }
  275. }
  276. }
  277.  
  278. //Called whenever a key is pressed in the game
  279. public void keyPressedGame(KeyEvent e) {
  280. //In the game
  281. if(e.getKeyCode() == KeyEvent.VK_Q ||
  282. e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  283. state = GameState.Menu;
  284. }
  285. }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement