Answers for "how to make a multiplayer game in python"

0

how to make a game in python

# Snake Game

import turtle
import random
import time

score = 0

# screen
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Snake Game By Atharva")
screen.tracer(0)

# Snake
snake = turtle.Turtle()
snake.shape("square")
snake.color("green")
snake.penup()
snake_pos = "None"

# apple
apple = turtle.Turtle()
apple.shape("circle")
apple.color("red")
apple.penup()
apple.goto(0, 100)

# classes


class SnakeMovements:
  y = 0
  x = 0
  snpos = snake_pos
  
  snpos = None
	def main(self):
      if self.snpos == "up":
        self.y = snake.ycor()
        snake.sety(self.y + 15)
        
      if self.snpos == "down":
        self.y = snake.ycor()
        snake.sety(self.y - 15)
        
      if self.snpos == "right":
        self.x = snake.xcor()
        snake.setx(self.x + 15)
        
      if self.snpos == "left":
        self.x = snake.xcor()
        snake.setx(self.x - 15)
        
    def SnakeUp(self):
      self.snpos = "up"
      
    def SnakeDown(self):
      self.snpos = "down"
      
    def SnakeRight(self):
      self.snpos = "right"
      
    def SnakeLeft(self):
      self.snpos = "left"
      
instanceVar = SnakeMovements()

# Keyboard Binding

screen.listen()
screen.onkeypress(instanceVar.SnakeUp, "Up")
screen.onkeypress(instanceVar.SnakeDown, "Down")
screen.onkeypress(instanceVar.SnakeRight, "Right")
screen.onkeypress(instanceVar.SnakeLeft, "Left")

# Mainloop of the game

while True:
  if snake.distance(apple) < 20:
    score += 1
    print(score)
    applex = random.randint(-290, 290)
    appley = random.randint(-290, 290)
    apple.goto(applex, appley)
    
    time.sleep(0.1)
  
  instanceVar.main()
  screen.update()
Posted by: Guest on November-30-2020
2

how to make a game on python

#Easy game in python

import random
import time

Passwordlist = ['abc123', '1234', '123456', 'QWERT', 'ASFDJKL;', '0102', '0', '100', '1000', '10000', '123', '2048', '1024', 'JSON', '1234567890']

ans = input('Enter an account name: ')
time.sleep(1)

print('Finding the password for it...')
time.sleep(1)
print(random.choice(Passwordlist)+ ' is the password')
Posted by: Guest on October-06-2020
-1

how to make a game in python

# Snake Game

import turtle
import random
import time

score = 0

# screen
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Snake Game By Atharva")
screen.tracer(0)

# Snake
snake = turtle.Turtle()
snake.shape("square")
snake.color("green")
snake.penup()
snake_pos = "None"

# apple
apple = turtle.Turtle()
apple.shape("circle")
apple.color("red")
apple.penup()
apple.goto(0, 100)

# classes


class SnakeMovements:
  y = 0
  x = 0
  snpos = snake_pos
  
  snpos = None
	def main(self):
      if self.snpos == "up":
        self.y = snake.ycor()
        snake.sety(self.y + 15)
        
      if self.snpos == "down":
        self.y = snake.ycor()
        snake.sety(self.y - 15)
        
      if self.snpos == "right":
        self.x = snake.xcor()
        snake.setx(self.x + 15)
        
      if self.snpos == "left":
        self.x = snake.xcor()
        snake.setx(self.x - 15)
        
    def SnakeUp(self):
      self.snpos = "up"
      
    def SnakeDown(self):
      self.snpos = "down"
      
    def SnakeRight(self):
      self.snpos = "right"
      
    def SnakeLeft(self):
      self.snpos = "left"
      
instanceVar = SnakeMovements()

# Keyboard Binding

screen.listen()
screen.onkeypress(instanceVar.SnakeUp, "Up")
screen.onkeypress(instanceVar.SnakeDown, "Down")
screen.onkeypress(instanceVar.SnakeRight, "Right")
screen.onkeypress(instanceVar.SnakeLeft, "Left")

# Mainloop of the game

while True:
  if snake.distance(apple) < 20:
    score += 1
    print(score)
    applex = random.randint(-290, 290)
    appley = random.randint(-290, 290)
    apple.goto(applex, appley)
    
    time.sleep(0.1)
  
  instanceVar.main()
  screen.update()
 ++
Posted by: Guest on November-30-2020
-2

python multiplayer

#some server thingy i found online.



server = ""
port = 5555

games = {}
idCount = 0
currentPlayer = 0

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((server, port))

s.listen()
print("Waiting for a connection, Server Started")

class Game:
    def __init__(self, id):
        self.connected = False

players = [Player(0), Player(1)]

def threaded_client(conn, player, gameId):
    global idCount
    conn.send(pickle.dumps(players[player]))
    reply = ""
    while True:
        try:
            data = pickle.loads(conn.recv(2048))
            players[player] = data
            if gameId in games:
                game = games[gameId]
                if not data:
                    print("Disconnected")
                    break
                else:
                    if player == 1:
                        reply = players[0]
                    else:
                        reply = players[1]

                conn.sendall(pickle.dumps(reply))
        except:
            break

    print("Lost connection")
    try:
        del games[gameId]
        print("Closing game", gameId)
    except:
        pass
    idCount -= 1
    conn.close()

while True:
    conn, addr = s.accept()
    print("Connected to:", addr)

    idCount += 1
    p = 0

    gameId = (idCount - 1) // 2

    if idCount % 2 == 1:
        games[gameId] = Game(gameId)
        players[0].connected = True
        print("Creating a new game")
        print("Waiting for another player")
    else:
        games[gameId].connected = True
        p = 1
        players[1].connected = True
        print("Game is available")



    start_new_thread(threaded_client, (conn, currentPlayer, gameId))
    currentPlayer += 1
Posted by: Guest on July-21-2021

Code answers related to "how to make a multiplayer game in python"

Python Answers by Framework

Browse Popular Code Answers by Language