Virtual Expo 2024

Serpentine Saga

Envision
CompSoc

       SERPENTINE SAGA!
MENTEES : AKHIL SATISHWARAN, ATHUL RAJEEV ,HARSHWARDHAN GOUD , HASINI JAISHETTY,                 MUBHASHIR AFZAL , MEGHARAJ S NAIK, R ADITYA , RIJUL RAMAN AND SHISHIR                                                         
MENTORS : ADITYA BHAGWAT , TARUN DJ , SANJAY BHAT

Meet Link: https://meet.google.com/igi-fext-uyq


                  INTRODUCTION:

SERPENTINE SAGA(SLITHER.IO) is a multiplayer online video game available for Ios,android and web browsers, developed by Steve Howse. Players control an avatar resembling a snake, which consumes multi-colored pellets, both from other players and ones that naturally spawn on the map in the game, to grow in size. The objective of the game is to grow the longest snake in the server. Born in the era of browser-based entertainment, it quickly carved out a niche in the hearts of millions worldwide.

One of the game's most appealing aspects is its simplicity. With intuitive controls that only require using the arrow keys or mouse to navigate.The clone made here is basically the mouse version where the camera moves with the slither making it very interesting , players of all ages and skill levels can quickly grasp the mechanics and start playing. Yet, despite its straightforward gameplay, the game offers a depth of strategy that keeps players engaged. Whether it's outmaneuvering opponents to trap them, using speed boosts to escape danger, or strategically positioning oneself to capture pellets, every move in SERPENTINE SAGA can make the difference between success and failure.

Players here find themselves in this dynamic environment filled with other snakes controlled by real people from around the world. The unpredictability of human opponents adds a thrilling element of challenge, as players must constantly adapt their strategies to outwit and outmaneuver their adversaries. Furthermore, Slither.io's minimalist graphics and vibrant colors contribute to its charm, creating a visually appealing experience that is both retro and modern simultaneously.

The concepts on implementation of game is step by step explained below , the game of SERPENTINE SAGA is implemented in PYTHON language using PYGAME
(a popular Python library designed for creating video games) which is very easy to access using any compiler.


APPROACH:

 

I) SINGLE PLAYER MECHANICS:

 

The following outlines the development approach for creating a single-player version of Slither.io using the Pygame library. 

The general approach outlined in this report covers fundamental game development aspects such as player input, rendering, collision detection, scoring, leaderboard,  and game over conditions.




 

Main Game Class:

 

The Game class serves as the central hub and controller of the entire game environment in the Slither.io clone. It encapsulates essential functionalities and manages interactions between various game components, ensuring seamless gameplay. By handling tasks such as initialization, updating game state, and rendering visuals, the Game class provides a structured framework for organizing game logic.

 

It sets up the window dimensions and objects, including the player, orbs, and camera, while also handling event-driven updates such as player input and orb collisions. The class continuously adjusts the camera to keep the player's snake centered on the screen. Rendering methods display the game's visuals, including the player's snake and orbs


 

Player Class:

 

The Player class serves as the representation of the player-controlled snake in the Slither.io clone, responsible for managing its movement, appearance, and interaction with the game world.It encapsulates crucial functionalities such as handling player input, updating the snake's position, and rendering its visual representation on the screen.

 

During initialization, it sets up essential properties such as the snake's position, texture, speed, and score. The update method allows for two modes of control: either by keyboard input or mouse control. Keyboard input updates the snake's position based on pressed keys, while mouse control calculates movement direction based on mouse position, adjusting the snake's coordinates accordingly. The draw method renders the snake's visual representation on the screen, including its texture and trailing segments. 

 

The snake's length gets updated dynamically as it consumes food orbs. When the snake eats a food orb, its length increases by adding new segments to the segments list.



 

Orb Class:

 

This class manages the behavior of individual food orbs, including their movement, collision detection with the player's snake, and rendering on the game screen.

 

Through its initialization method, the class sets up essential properties like velocity vectors for movement and rectangle objects to define each orb's position. During gameplay, the update method orchestrates the orb's behavior by adjusting its position based on its velocity vector and detecting collisions with the player's snake. Upon collision, the player's score is updated, signaling the removal of the orb. The draw method ensures the accurate rendering of orbs on the game screen, utilizing transformed coordinates for precise placement.



 

Camera Class:

 

The main use case of this class is to continuously update the camera coordinates to match the player’s as the player’s screen must always display the snake in the center. 

This also avoids the involvement of relative velocity for the other objects such as orbs and other snakes. 

 

Through its update method, the Camera class continuously adjusts its coordinates to align with the player's snake position, synchronizing the camera's view with the player's movements. Additionally, the translate_coordinates method transforms input coordinates into new ones based on the current camera position, facilitating accurate rendering of game objects on the screen relative to the player's perspective.



 

II) MULTIPLAYER MECHANICS:

 

The following outlines the techniques adopted to convert the above mentioned single player version into a multiplayer game with a client-server model.  To ensure a smooth game-play with lesser lag, the heavy lifting tasks such as collision detection of a player with any food orbs or other player has been done on the client side. However, it is generally a better practice if this is done on the server side to prevent malicious clients from sending inaccurate messages.

Data:

Data sent/received by server and client is structured as a header(of constant size) which contains the size of the payload and the payload itself which contains the actual data to be sent/received. This method ensures that the entire payload is received (a loop is run until the specified number of bytes is received).

 

Serializing helps to convert the data to be sent into byte streams that can be easily transferred over a network. The data (python objects like lists and dictionaries) are serialized with pickle before sending them.

Transmission:

The protocol used for data transmission is TCP/IP using IPv4. TCP/IP stands for Transmission Control Protocol/Internet Protocol.It's a suite of communication protocols used to interconnect network devices on the internet. TCP/IP is the most widely used protocol suite and is the foundation for the internet.

The TCP/IP model is based on a five-layer model for networking. From bottom (closest to the hardware) to top (closest to the user), these are:

  1. Physical Layer: 

Deals with the physical characteristics of the transmission medium

  1. Data Link Layer: 

This layer is responsible for node-to-node data transfer. It also detects and corrects errors that may occur in the Physical layer.

  1. Network Layer: 

This layer is responsible for the delivery of packets from the source host to the destination host based on their IP addresses.

  1. Transport Layer:

This layer provides transparent transfer of data between hosts and is responsible for end-to-end error recovery and flow control. TCP operates at this layer.

  1. Application Layer: 

This layer includes applications or processes that use transport layer protocols to deliver the data to destination computers. The python socket works in this layer.

 

Alternatives like UDP (User Datagram Protocol) can be used instead of TCP in the transport layer. UDP is a connectionless protocol unlike TCP. It offers better speed than TCP at the cost of reliability and error-checking. This makes it a viable alternative for multiplayer game development.

 

Non-blocking python sockets have been used in the application layer. This allows the sockets to receive data without blocking the program flow. Alternatively multithreading or multiprocessing also can be used with blocking sockets to implement the application layer.


 

Server Side Architecture:

Socket class:

The Socket class is a wrapper around the python socket that makes it more convenient to send  and receive data in the rest of the code.It handles the tasks of accepting new clients,serializing the data, creating the header and ensuring that all the data is sent/received.

init method:
  • Initialize ip,port
  • Initialize TCP/IP socket.
  • Set socket to non blocking and bind to ip:port
  • Listen for incoming connections on the socket
acceptNewClient method:
  • Accept client through socket
  • Return the client object

 

send method:
  • Takes data and client as parameters
  • Serializes the data
  • Creates a header containing the length of the serialized data.
  • Runs the sendall() method of the client to send the header to the client
  • Runs the sendall() method of the client to send the data to the client

 

receive method:
  • Received the header.
  • Runs a loop to receive data until the number of bytes mentioned in the header have been received.
  • Returns the data sent by a client after deserializing it.

 

GameSever class:

The GameServer class is used to initialize an instance of the game server which will handle all the game events. It uses the above mentioned Socket class for sending/receiving data.

 

It uses a dictionary to store the state of every player as values and the client object of every player as the key. The player state includes coordinates of player segments, unique ID of the player, score. A list of orbs is maintained. This list consists of tuples of coordinates of every orb in the game. It is initialized with random coordinates.

 

When the server runs, first it checks if any new clients are ready to join the game and adds them to the player dictionary .Then it loops over every existing client to get its state, orbs consumed if any, and any collisions with other players. This data is used to update the dictionary of players and list of orbs.If any orb is consumed, a new orb is created at random. Any player with a collision is removed. Finally it broadcasts the state of every player and the list of orbs to every connected client.

 

A unique ID is given to every player in the game to help it distinguish itself from other players and to make it convenient to display scores. Considering that only a finite number of players will be connecting, the nth player to connect to the server receives ‘n’ as the UID.

init method:
  • Initialize object of Socket class,dictionary of players and list of orbs.
  • Execute the main loop.
mainLoop method:
  • Runs an infinite loop that handles all clients.
  • Receives data from clients and processes them
  • Accepts new players
  • Broadcasts states of a player to every other player
  • Updates the state of orbs and broadcasts it to every player

Client Side Architecture:

Socket class:

The Socket class is a wrapper from the python socket that makes it more convenient to send  and receive data in the rest of the code. It implements the send/recevice method similar to the server side socket class but also implements a close method to end the connection when the game is closed or a collision is encountered on the client side.

init method:
  • Initialize ip,port of server and socket object
  • Set socket to non blocking and bind to ip,port
  • Connect the socket to the server at ip:port
send method:
  • Takes data as parameters
  • Sends the serialized data to the client

 

receive method:
  • Returns the data sent by a client after processing
close method:
  • Send an appropriate end message to the server
  • Closes the socket

 

Additions to be done in existing classes:

 

The Main Game class initializes an instance of the Socket class which it will use to communicate with the server. A new method is implemented to receive the states of opponents and the orb coordinates from the server and update the information in the local variables. This method is called before the contents are rendered on the screen. Every time the player moves, the current player state and collision with orbs if any are sent to the server.

 

If a collision with another player is encountered, an appropriate end message is sent to the server. The end message is also sent if the player closes the game before dying to ensure that the player is removed.






 

METADATA

Report prepared on May 5, 2024, 11:31 p.m. by:

  • Sanjay S Bhat [CompSoc]
  • Tarun Dinesh Joshi [CompSoc]
  • Aditya V Bhagwat [CompSoc]

Report reviewed and approved by Aditya Pandia [CompSoc] on May 9, 2024, 10:49 p.m..

Check out more projects!