Reputation: 25
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
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