Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.geom.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.image.*;
- import java.io.*;
- import javax.imageio.*;
- public class GraphicsMenu extends GameEngine {
- // Main Function
- public static void main(String args[]) {
- createGame(new GraphicsMenu());
- }
- //-------------------------------------------------------
- // Your Program
- //-------------------------------------------------------
- enum GameState {Menu, Playing, Options};
- GameState state = GameState.Menu;
- int menuOption = 0;
- //volume of background sound
- float volume = -4;
- float volume2 = -4;
- Image play;
- Image playHighlighted;
- Image options;
- Image optionsHighlighted;
- Image exit;
- Image exitHighlighted;
- Image menuBackground;
- Image test;
- Image test2;
- Image test3;
- Image size1;
- Image size1Highlighted;
- Image size2;
- Image size2Highlighted;
- Image hud;
- AudioClip background;
- AudioClip beep;
- //Game variables
- int health;
- int tester, tester2;
- public void init() {
- setWindowSize(1100, 650);
- play = loadImage("Play1.png");
- playHighlighted = loadImage("Playhighlighted.png");
- options = loadImage("Options1.png");
- optionsHighlighted = loadImage("Optionshighlighted.png");
- exit = loadImage("Exit1.png");
- exitHighlighted = loadImage("Exithighlighted.png");
- menuBackground = loadImage("NewBackground.png");
- test = loadImage("NewBackground.png");
- test2 = loadImage("titlegame2.png");
- test3 = loadImage("backgroundtest.png");
- size1 = loadImage("mutesound.png");
- size1Highlighted = loadImage("mutesoundhighlighted.png");
- size2 = loadImage("background1.png");
- size2Highlighted = loadImage("background1highlighted.png");
- hud = loadImage("HUD.png");
- tester = 0;
- tester2 = 0;
- // pull file and start background music loop
- background = loadAudio("pvzbackground.wav");
- beep = loadAudio("beep.wav");
- startAudioLoop(background, volume);
- }
- //Updates the display
- public void update(double dt) {
- if(state == GameState.Playing) {
- health--;
- if(health <= 0) {
- state = GameState.Menu;
- }
- }
- }
- //-------------------------------------------------------
- //This gets called any time the Operating System
- //tells the program to paint itself
- public void paintComponent() {
- changeBackgroundColor(white);
- clearBackground(width(), height());
- if(state == GameState.Menu) {
- paintMenu();
- } else if(state == GameState.Options) {
- paintOptions();
- } else if(state == GameState.Playing) {
- paintGame();
- }
- }
- //Function to paint the menu
- public void paintMenu() {
- if(tester == 0){
- //draw background
- drawImage(test, 0, 0, width(), height());
- //draw title
- drawImage(test2, 310, 120);
- }
- if(tester == 1){
- //draw new background
- drawImage(test3, 0, 0);
- //draw title
- drawImage(test2, 310, 120);
- }
- //Play
- if(menuOption == 0) {
- drawImage(playHighlighted, 498, 200);
- } else {
- drawImage(play, 498, 200);
- }
- //Options
- if(menuOption == 1) {
- drawImage(optionsHighlighted, 473, 250);
- } else {
- drawImage(options, 473, 250);
- }
- //Exit
- if(menuOption == 2) {
- drawImage(exitHighlighted, 501, 300);
- } else {
- drawImage(exit, 501, 300);
- }
- }
- //Function to paint the options menu
- public void paintOptions() {
- if(tester == 0){
- //draw background
- drawImage(test, 0, 0, width(), height());
- //draw title
- drawImage(test2, 310, 120);
- }
- if(tester == 1){
- //draw new background
- drawImage(test3, 0, 0);
- //draw title
- drawImage(test2, 310, 120);
- }
- //Play
- if(menuOption == 0) {
- drawImage(size1Highlighted, 438, 200);
- } else {
- drawImage(size1, 438, 200);
- }
- //Options
- if(menuOption == 1) {
- drawImage(size2Highlighted, 384, 250);
- } else {
- drawImage(size2, 384, 250);
- }
- //Exit
- if(menuOption == 2) {
- drawImage(exitHighlighted, 501, 300);
- } else {
- drawImage(exit, 501, 300);
- }
- }
- //Function to paint the game
- public void paintGame() {
- //First part of game
- }
- public void paintHUD() {
- //Second part of game
- }
- //-------------------------------------------------------
- //Called whenever a key is pressed
- public void keyPressed(KeyEvent e) {
- if(state == GameState.Menu) {
- keyPressedMenu(e);
- playAudio(beep, volume2);
- } else if(state == GameState.Options) {
- keyPressedOptions(e);
- playAudio(beep, volume2);
- } else {
- keyPressedGame(e);
- playAudio(beep, volume2);
- }
- }
- //Called whenever a key is pressed in the menu
- public void keyPressedMenu(KeyEvent e) {
- //Up Arrow
- if(e.getKeyCode() == KeyEvent.VK_UP) {
- if(menuOption > 0) {
- menuOption--;
- }
- }
- //Down Arrow
- if(e.getKeyCode() == KeyEvent.VK_DOWN) {
- if(menuOption < 2) {
- menuOption++;
- }
- }
- //Enter Key
- if(e.getKeyCode() == KeyEvent.VK_ENTER) {
- if(menuOption == 0) {
- //Start Game
- health = 100;
- state = GameState.Playing;
- } else if(menuOption == 1) {
- //Option Menu
- state = GameState.Options;
- menuOption = 0;
- } else {
- //Exit
- System.exit(0);
- }
- }
- }
- //Called whenever a key is pressed in the options menu
- public void keyPressedOptions(KeyEvent e) {
- //Up Arrow
- if(e.getKeyCode() == KeyEvent.VK_UP) {
- if(menuOption > 0) {
- menuOption--;
- }
- }
- //Down Arrow
- if(e.getKeyCode() == KeyEvent.VK_DOWN) {
- if(menuOption < 2) {
- menuOption++;
- }
- }
- //Enter Key
- if(e.getKeyCode() == KeyEvent.VK_ENTER) {
- if(menuOption == 0) {
- //change sound
- stopAudioLoop(background);
- volume2 = -50;
- } else if(menuOption == 1) {
- //change background
- if(tester == 0){
- tester = 1;
- }else if(tester == 1){
- tester = 0;
- }
- } else {
- //Exit
- state = GameState.Menu;
- menuOption = 0;
- }
- }
- }
- //Called whenever a key is pressed in the game
- public void keyPressedGame(KeyEvent e) {
- //In the game
- if(e.getKeyCode() == KeyEvent.VK_Q ||
- e.getKeyCode() == KeyEvent.VK_ESCAPE) {
- state = GameState.Menu;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement