'Was following a tut on pygame sprites and got some errors, not sure whats wrong with my code it says line 462 has an error, there is no line 462
This is my code. If you respond please include full code, i am bad at replacing things.
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
pygame.init()
class Will(pygame.sprite.Sprite):
def __init__(self,width,height, pos_y,pos_x,color):
super().__init__()
self.image = pygame.Surface([width,height])
self.image.fill(black)
self.rect = self.image.get_rect()
clock = pygame.time.Clock()
display = pygame.display.set_mode((500, 500))
pygame.display.set_caption('Slap Chris Rock!')
will = Will(50,50,100,100,(black))
will_group = pygame.sprite.Group()
will_group.add(Will)
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
# print(event)
display.fill(white)
pygame.display.update()
pygame.quit()
quit()
will_group.draw(display)
please note these are the actual indentations. my last post had wrong indentations, i have fixed that.
Solution 1:[1]
You didn't include the full code so i can't be 100% sure, but from what i tested, the solution is pretty simple, you just have to replace the
will_group.add(Will) by will_group.add(will) with a non caped "will". What your code does is that it tries to add the class Will to a group, and not the instance of Will named will. Try avoiding similar names and you will have less errors like this one.
Edit: in fact the whole code was there, but the indentation is messed up. Here is the working code:
import pygame
white = (255,255,255)
black = (0,0,0)
orange = (255,165,0)
pygame.init()
class Will(pygame.sprite.Sprite):
def __init__(self,width,height, pos_y,pos_x,color):
super().__init__()
self.image = pygame.Surface([width,height])
self.image.fill(black)
self.rect = self.image.get_rect()
clock = pygame.time.Clock()
display = pygame.display.set_mode((500, 500))
pygame.display.set_caption('Slap Chris Rock!')
will = Will(50,50,100,100,(black))
will_group = pygame.sprite.Group()
will_group.add(will)
exit = False
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
# print(event)
display.fill(white)
pygame.display.update()
pygame.quit()
quit()
will_group.draw(display)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rémi couturier |
