3.4 KiB
This project will be a network mutiplayer Snake game.
python 3 server and a web client that uses webtransport datagrams.
the logic of the game: When the snake hits something, it doesn't die, the head stays in place, but the tail shortens by 1 every tick the head is turned in the direction of the obstacle. The player can change the direction the head is turned and the snake continues to move in new direction. The snake can not be shorter than its head, so the head always lives, even when the tail is all gone. consider the possibility of stucking on anothers player snake (head or tail)? when the other player's snake have passed by and no more considered the obstacle, the current player's snake should continue moving. self-collision is not permanent, because the tail keeps shrinking, there will be a moment, when the segment of a tail that was an obstacle will cease to exist other snake collision can be auto-cleared when the other snake is moved away or shrinks to the point when it is no longer an obstacle if a player is disconnected - his snake and score dissapears from the game, so there is no game-over state. the server runs constantly, without "rounds" of gameplay. new players connect directly into the game. There is no end game to detect winner or loser, there is continuous gameplay and in each moment there is a length of each snake. The longest snake is the "winner" at each moment (considering it can shorten or be outrunned by another player and lose its winning position). keep the snake color the same for the whole duration of client connection instead of a score based on eaten apples display the snakes current lengths when only the head is left - it can turn 180 degrees as well as 90
make a small input buffer to buffer 3 direction changes for next ticks. if the new direction the user is pressing is directly opposite to the last in the buffer - replace the last in the buffer (do not add it as a new step). instead of ignoring overflowinf inputs - replace the last one. do not add to the buffer repeating inputs. before calculating next position when consuming 1 direction from buffer check if it is 180 turn when snake length>1, if so - ignore this input and consume the next one.
when connected to server - show the current gameplay on the background with the text overlay "press space to join" when 0 players left - populate the field with 3 apples
field size 60 by 40 (by default). allow room in the protocol it to be changed between 3x3 up to 255x255
when using webtransport datagrams (UDP) for the game protocol, add packet number into the message to ignore late packets. make possible to wrap numbering from the beginnig in a case of integer overflow. use compression for the data in packets, so the full field update can be transmitted as one UDP datagram (typically up to 1500 bytes) in UDP (webtransport datagrams) Protocol Design - allow room for lost packets before/after wrapping; make room for up to 32 simultaneous players with different colors; limit player name length to 16 characters
if UDP packet size exceeds 1280 bytes - split the update in several parts that do not exceed this size by updating different snakes info in different independant packets, so if one of them is lost - some of the information still reaches the recipient. If a snake is so long that its update doesn't fit into one packet by itself - then find a way to split it into several updates, preferably of the similar size (split the snake into the equal size parts)