2

I am trying to write a script in python that renders a pygame window and moves a

rectangular box within the window in response to the keys pressed by user. Below is the source code:

The below code is written in script "client.py"

import pygame

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

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):
    win.fill((255,255,255))
    player.draw(win)
    pygame.display.update()


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

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

        p.move()
        redrawWindow(win, p)

main()

On executing this python script through MAC OS terminal using the command:

(venv) janamzaveri@Janams-MacBook-Air side-projects % python3 client.py

I encounter the following:

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Unable to obtain graphics context for NSWindow (Mojave behavior)\

I am unsure for the reason for this anomalous behaviour. Is there any I can resolve this issue?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Related [pygame installation issue in mac os](https://stackoverflow.com/questions/22974339/pygame-installation-issue-in-mac-os) – Rabbid76 Jul 04 '21 at 07:23

1 Answers1

1

By installing the dev version of pygames with the below command enabled me to

run my script successfully.

pip install pygame==2.0.0.dev6