Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- import time
- import sys
- from PygameNetworking import ClientManager
- Client = ClientManager()
- class Timer():
- def __init__(self):
- self.LastTime = time.time()
- def GetTimer(self):
- return time.time() - self.LastTime
- def ResetTimer(self):
- self.LastTime = time.time()
- class GameApp():
- def __init__(self):
- pygame.init()
- self.window_size = [640,480]
- self.screen = pygame.display.set_mode(self.window_size)
- pygame.display.set_caption("Multiplayer Game")
- self.instancieds = {}
- self.get_timer = Timer()
- self.update_timer = Timer()
- self.colors = {
- "white":[255,255,255],
- "green":[0,255,0],
- "gray":[195,195,195]
- }
- self.player = {
- "object":pygame.Rect(310,230,10,10),
- "color":self.colors["green"],
- "speed":10,
- "delay":Timer()
- }
- self.update()
- def update(self):
- while True:
- Client.ReceiveData()
- if Client.connected == False:
- Client.Connect()
- else:
- if self.get_timer.GetTimer() >= 0.05:
- Client.GetInfos()
- self.get_timer.ResetTimer()
- if self.update_timer.GetTimer() >= 0.033:
- self.UpdatePlayer()
- self.update_timer.ResetTimer()
- if Client.my_info != None:
- print(Client.my_info)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- self.screen.fill(self.colors["white"])
- self.DrawOthers()
- pygame.draw.rect(self.screen, self.player["color"], self.player["object"])
- self.PlayerMovement()
- pygame.display.update()
- def PlayerMovement(self):
- pressed = pygame.key.get_pressed()
- if self.player["delay"].GetTimer() > 0.04:
- if pressed[K_UP] and self.player["object"].y > 0 and not pressed[K_LEFT] and not pressed[K_RIGHT]:
- self.player["object"].y -= self.player["speed"]
- if pressed[K_RIGHT] and self.player["object"].x < (self.window_size[0] - 10):
- self.player["object"].x += self.player["speed"]
- if pressed[K_DOWN] and self.player["object"].y < (self.window_size[1] - 10) and not pressed[K_LEFT] and not pressed[K_RIGHT]:
- self.player["object"].y += self.player["speed"]
- if pressed[K_LEFT] and self.player["object"].x > 0:
- self.player["object"].x -= self.player["speed"]
- self.player["delay"].ResetTimer()
- def UpdatePlayer(self):
- Dict = {"Position":[self.player["object"].x, self.player["object"].y]}
- Client.UpdatePlayerDict(Dict)
- def DrawOthers(self):
- if Client.players_info != None:
- for id in Client.players_info:
- if id != Client.my_info["ID"]:
- try:
- if not id in self.instancieds:
- self.instancieds[id] = {"object":pygame.Rect(Client.players_info[id]["Position"][0], Client.players_info[id]["Position"][1],10,10)}
- else:
- self.instancieds[id]["object"].x = Client.players_info[id]["Position"][0]
- self.instancieds[id]["object"].y = Client.players_info[id]["Position"][1]
- pygame.draw.rect(self.screen, self.colors["gray"], self.instancieds[id]["object"])
- except:
- pass
- GameApp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement