-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
106 lines (94 loc) · 3 KB
/
Game.java
File metadata and controls
106 lines (94 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//----------------------------------------------------------------------
// Author: Micheal Oyenekan
// Date : 10/23/2020 (Completed)
// File : Game.java
// Description: This program implements a Mario game. It a includes a
// Mario character that can run and jump when prompted by the user. There
// are also tubes included in this game, which can either be loaded from
// a json file and added during gameplay. Mario can collide with any of
// the tubes unless he is on top of them or under them.
//-----------------------------------------------------------------------
// NOTE: The information in tubetest.json is still in the file, so when
// the program is run, a print statement appears saying "You've loaded
// a tube at (300, 306)".
//-----------------------------------------------------------------------
//Importing java classes to be used.
import javax.swing.JFrame;
import java.awt.Toolkit;
//----------------------------------------------
// The principle class of the program is Game.
// This class contains all the components and
// aspects of the program.
//----------------------------------------------
public class Game extends JFrame
{
//Declaring the member variables.
Model model;
Controller controller;
View view;
//----------------------------------------------
// Constructor for the Game class. The constructor
// initializes the model, controller, and the view.
//----------------------------------------------
public Game()
{
//Initialization of member variables.
model = new Model();
controller = new Controller(model);
view = new View(controller, model);
//Setting the initial look of the panel.
this.setTitle("Fireball Mario");
this.setSize(800, 500);
this.setFocusable(true);
this.getContentPane().add(view);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//Adding the event listeners.
view.addMouseListener(controller);
this.addKeyListener(controller);
// try{
// Json j = Json.load("map.json");
// model.unmarshal(j);
// }
// catch (Exception e)
// {
// e.printStackTrace();;
// System.out.print("'There was an error loading your map.'");
// System.exit(0);
// }
}
//----------------------------------------------
// The run method regularly updates the screen
// as the turtle moves.
//----------------------------------------------
public void run()
{
while(true)
{
controller.update();
view.repaint(); //Indirectly calls View.paintComponent
model.update();
Toolkit.getDefaultToolkit().sync(); //Updates screen
//Go to sleep for 50 milliseconds
try
{
Thread.sleep(50); //20 frames per second
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}
//----------------------------------------------
// Main initializes a game object and runs it.
//----------------------------------------------
public static void main(String[] args)
{
Json j = Json.load("map.json");
//Tube t = new Tube(j);
Game g = new Game();
g.run();
}
}