GithubHelp home page GithubHelp logo

slfagrouche / the-swinging-snake-game Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 13 KB

Snake Game is a classic game reimagined using Java Swing. Players control a growing snake that moves around the screen, eating apples to increase their score. The game becomes more challenging as the snake grows and the level increases. The game ends when the snake collides with the edges or its own body.

Java 100.00%

the-swinging-snake-game's Introduction

The-Swinging-Snake-Game

Introduction

This is an implementation of the classic Snake game built using Java Swing. The game consists of a snake that moves around the screen, eating apples and growing longer with each apple eaten. The player's score increases with each apple eaten. The game ends when the snake collides with the edges of the screen or its own body. The game also has a game over state that is displayed when the game ends. The game can be controlled using the arrow keys. The game also includes functionality for updating the game level based on the number of apples eaten and adjusting the game speed accordingly.

Classes and Methods

• The main class is the GamePanel class which is a JPanel that represents the game screen.

• It contains variables and methods for the game's logic and display such as screen size, unit size, grid size, delay between updates, coordinates of the snake and the apple, the number of apples eaten, the direction the snake is moving, and the current state of the game.

• It also contains a Timer object to handle updates and a Random object to generate random positions for the apple.

• The startGame method creates a new apple and starts the timer.

• The paintComponent method is called automatically when the panel needs to be repainted. It first calls the super.paintComponent(g) method to clear the screen and then calls the draw method in the PlayingState class to draw the game on the screen and display the level status.

• The newApple method generates a random position for the apple and checks if the level needs to be incremented.

Conclusion

This Snake Game is a classic game that has been implemented using Java Swing. The game implements the basic features of a Snake game along with the level and speed updates. The game can be controlled using the arrow keys and ends when the snake collides with the edges of the screen or its own body.

Running and Compiling the code

To compile and run the code, you need to have Java installed on your computer. Here's how you can compile and run the code: Open a text editor and copy the code into a new file. Save the file with a .java extension, for example, "SnakeGame.java". Open the terminal or command prompt and navigate to the directory where you saved the file. Compile the code by typing "javac SnakeGame.java" and press enter. Run the code by typing "java SnakeGame" and press enter. This will launch the game and you can start playing by using the arrow keys to control the snake. The goal of the game is to make the snake eat as many apples as possible without hitting the walls or its own body.

Screenshots

SnakeShot1

SnakeShot2

SnakeShot3

SnakeShot4

SnakeShot5

the-swinging-snake-game's People

Contributors

slfagrouche avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.