rapidfire1661
rapidfire1661

Reputation: 25

Module 'pygame.locals' has no attribute 'SCREEN_WIDTH'

I'm not sure why this is happening, I understand the problem, but have double checked my code and cannot find out what is wrong with it. I'm quite new to python, especially pygame, so I could just be making a silly mistake. The game is a racing game where you have to avoid the cars are coming from above.

Here is the snippet of code with the error:

class Enemy(pygame.sprite.Sprite): 
  def __init__(self):
    super().__init__()
    self.image = pygame.image.load("car_image.png")
    self.rect = self.image.get_rect()
    self.rect.center=(random.randint(40,pygame.locals.SCREEN_WIDTH-40),0) 
  def move(self):
    self.rect.move_ip(0,10)
    if (self.rect.top > 600):
      self.rect.top = 0
      self.rect.center = (random.randint(30, 370), 0)
  def draw(self, surface):
    surface.blit(self.image, self.rect)

Here is the full code: https://pastebin.com/0XLucRVm

Any help will be appreciated.

Upvotes: 1

Views: 142

Answers (1)

Rabbid76
Rabbid76

Reputation: 211166

pygame.locals.SCREEN_WIDTH doesn't exist. You invented that. You can get the width of the window from the display surface. e.g.:

screen_width = pygame.display.get_surface().get_width()

(see pygame.display and pygame.Surface)

Upvotes: 2

Related Questions