JAVA Dinasoaur
JAVA Dinasoaur
“CHROME-DINOSOUR-GAME”
By
Software requirements:
Operating System: Windows, Linux.
Software’s: Java SDK 2, Borland JBuilder , Eclipse,vscode.
Problem Statement:
The objective of this project is to implement a basic version of the Chrome Dinosaur
game using Java. The game will feature a graphical user interface, displaying a
dinosaur character that the player controls to avoid obstacles such as cacti and flying
birds. Players can make the dinosaur jump by pressing the spacebar and duck using
the down arrow key. Obstacles will be generated randomly and move from the right
to the left of the screen at varying intervals. A scoring system will be implemented,
awarding points for each successfully avoided obstacle and displaying the current
score at the top of the window. Collision detection will determine if the dinosaur
collides with an obstacle, leading to a game-over screen that shows the final score
and offers the option to restart.
Theory:
Introduction
The Chrome Dinosaur Game is a quintessential example of effective game design,
blending simple mechanics with engaging psychological principles. As an endless
runner, it invites players to control a T-Rex, navigating increasingly challenging
obstacles, which promotes a balance between skill and challenge. The game's
incremental difficulty keeps players motivated, tapping into the theory of instant
gratification as they receive immediate feedback through scoring. Its minimalist
pixel art style and simple sound effects evoke nostalgia, enhancing emotional
engagement.
By utilizing straightforward controls, the game is accessible to a wide audience,
making it easy to pick up and play. Additionally, its offline functionality transforms
moments of internet connectivity loss into opportunities for fun, thereby enriching
the overall user experience and turning a frustrating situation into an enjoyable
distraction.
Project Description:
The Chrome Dinosaur Game is a whimsical offline feature integrated into the
Google Chrome browser, designed to entertain users during internet outages. Players
control a pixelated T-Rex as it runs through a desert landscape, skillfully avoiding
obstacles like cacti and birds. The primary objectives of the game are to engage
players with addictive gameplay, offering intuitive mechanics that are accessible to
all ages and skill levels. The game features dynamic difficulty, where speed and
complexity gradually increase, enhancing the challenge and keeping players
invested. A scoring system tracks players' achievements, fostering a sense of
accomplishment and encouraging competition
Implementation
1. Requirements Gathering
Game Mechanics:
Control a T-Rex character.
Jump over obstacles (cacti and birds).
Score points based on distance traveled and obstacles avoided.
Difficulty Levels:
2. Architecture Design
1. Game Engine
Core Logic: Manages game loop, states (running, paused, game over).
2. Game Objects
Player (T-Rex): Controls position, velocity, and jump mechanics.
Obstacles: Cacti and birds with properties for position and speed.
3. Collision Detection
Detects interactions between the T-Rex and obstacles, triggering scoring and
game over events.
3. Technology Stack
While the original Chrome Dinosaur Game is built with web technologies, if you
were to create a similar game using Java, the technology stack would look different.
Here’s a suggested stack:
1. Programming Language
o Java:
The primary language for developing the game, enabling object-
oriented programming and robust application structure.
2. Development Environment
o IDE:
Use an integrated development environment like Eclipse or
IntelliJ IDEA for coding, debugging, and project management .
4. Feature Implementation
1. Game Start and Initialization
Start Screen:
o Display a simple "Press Space to Start" message.
o Initialize game variables (score, speed, obstacles).
2. Character Control
T-Rex Movement:
o Implement a T-Rex class with properties like position and velocity.
o Use event listeners to detect key presses (e.g., spacebar for jump).
Jump Mechanic:
o Use a simple physics formula to simulate jumping (gravity effect)
when the spacebar is pressed.
3. Obstacle Generation
Obstacle Class:
o Create an Obstacle class with properties for type (cactus, bird),
position, and speed.
Random Generation:
o Use a timer to periodically generate obstacles at random intervals and
positions.
5.User Interface Design
Start Screen:
Background: Simple desert landscape.
Title: "Chrome Dino Game" in a bold, pixelated font.
Instructions: "Press Space to Start" with an animated T-Rex.
Game Interface:
Background: Scrolling desert theme.
T-Rex: Pixelated character with running and jumping animations.
Obstacles: Distinct designs for cacti and birds.
Score Display: Clear, prominent score at the top left.
6. Testing
Functional Testing:
Verify T-Rex jumping and ducking mechanics.
Check correct scoring based on distance and obstacles.
Ensure game over conditions trigger properly.
Usability Testing:
Assess clarity and intuitiveness of UI elements.
Confirm instructions are easy to understand.
Future Enhancements
Multiplayer Mode:
Introduce a competitive mode where players can race against each other in
real-time.
Power-Ups:
Add power-ups that players can collect to gain temporary abilities (e.g.,
invincibility, speed boosts).
Level Variations:
Implement different environments (e.g., night mode, rain) that introduce
unique obstacles and aesthetics.
References:
W3schools
The CODINGTRAIN
Source Code:
APP.JAVA
import javax.swing.*;
public class App {
public static void main(String[] args) throws Exception {
int boardWidth = 750;
int boardHeight = 250;
JFrame frame = new JFrame("Chrome Dinosaur");
// frame.setVisible(true);
frame.setSize(boardWidth, boardHeight);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ChromeDinosaur chromeDinosaur = new ChromeDinosaur();
frame.add(chromeDinosaur);
frame.pack();
chromeDinosaur.requestFocus();
CHROME.JAVA
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
//images
Image dinosaurImg;
Image dinosaurDeadImg;
Image dinosaurJumpImg;
Image cactus1Img;
Image cactus2Img;
Image cactus3Img;
class Block {
int x;
int y;
int width;
int height;
Image img;
//dinosaur
Block dinosaur;
//cactus
int cactus1Width = 34;
int cactus2Width = 69;
int cactus3Width = 102;
//physics
int velocityX = -12; //cactus moving left speed
int velocityY = 0; //dinosaur jump speed
int gravity = 1;
Timer gameLoop;
Timer placeCactusTimer;
public ChromeDinosaur() {
setPreferredSize(new Dimension(boardWidth, boardHeight));
setBackground(Color.lightGray);
setFocusable(true);
addKeyListener(this);
//dinosaur
dinosaur = new Block(dinosaurX, dinosaurY, dinosaurWidth, dinosaurHeight,
dinosaurImg);
//cactus
cactusArray = new ArrayList<Block>();
//game timer
gameLoop = new Timer(1000/60, this); //1000/60 = 60 frames per 1000ms
(1s), update
gameLoop.start();
void placeCactus() {
if (gameOver) {
return;
}
//cactus
for (int i = 0; i < cactusArray.size(); i++) {
Block cactus = cactusArray.get(i);
g.drawImage(cactus.img, cactus.x, cactus.y, cactus.width, cactus.height,
null);
}
//score
g.setColor(Color.black);
g.setFont(new Font("Courier", Font.PLAIN, 32));
if (gameOver) {
g.drawString("Game Over: " + String.valueOf(score), 10, 35);
}
else {
g.drawString(String.valueOf(score), 10, 35);
}
}
if (dinosaur.y > dinosaurY) { //stop the dinosaur from falling past the ground
dinosaur.y = dinosaurY;
velocityY = 0;
dinosaur.img = dinosaurImg;
}
//cactus
for (int i = 0; i < cactusArray.size(); i++) {
Block cactus = cactusArray.get(i);
cactus.x += velocityX;
if (collision(dinosaur, cactus)) {
gameOver = true;
dinosaur.img = dinosaurDeadImg;
}
//score
score++;
}
@Override
public void actionPerformed(ActionEvent e) {
move();
repaint();
if (gameOver) {
placeCactusTimer.stop();
gameLoop.stop();
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
// System.out.println("JUMP!");
if (dinosaur.y == dinosaurY) {
velocityY = -17;
dinosaur.img = dinosaurJumpImg;
}
if (gameOver) {
//restart game by resetting conditions
dinosaur.y = dinosaurY;
dinosaur.img = dinosaurImg;
velocityY = 0;
cactusArray.clear();
score = 0;
gameOver = false;
gameLoop.start();
placeCactusTimer.start();
}
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
}
OUTPUT:
Conclusion:
The Chrome Dinosaur Game stands out as a simple yet engaging offline feature
that transforms a frustrating internet outage into a fun and addictive experience.
Through its intuitive gameplay, players control a pixelated T-Rex, skillfully
navigating obstacles while aiming for high scores. The game's design emphasizes
accessibility, with straightforward mechanics suitable for all ages and skill levels .