Creating a Client

Subscribe to Tech With Tim!

In this tutorial I talk about how an online game works and show you how to code a very basic client using pygame. In the next videos we will code the server and connect our client to it.

Install Pygame

The first thing that must be done before creating the client is to install pygame.

You can watch the following video(s) to see how to do so:
How to Install Pygame on Windows
How to Install Pygame on Mac OS

Client Code

import pygame

width = 500
height = 500
win = pygame.display.set_mode((width, height))

clientNumber = 0

class Player():
    def __init__(self, x, y, width, height, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.rect = (x,y,width,height)
        self.vel = 3

    def draw(self, win):
        pygame.draw.rect(win, self.color, self.rect)

    def move(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT]:
            self.x -= self.vel

        if keys[pygame.K_RIGHT]:
            self.x += self.vel

        if keys[pygame.K_UP]:
            self.y -= self.vel

        if keys[pygame.K_DOWN]:
            self.y += self.vel

        self.rect = (self.x, self.y, self.width, self.height)

def redrawWindow(win,player):

def main():
    run = True
    p = Player(50,50,100,100,(0,255,0))
    clock = pygame.time.Clock()

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        redrawWindow(win, p)