Flappy Bird
I'm trying to program flappy bird by following a youtube video, but even after follow his code I get error where he doesn't get error. I notice in the error zone the color has vanished. At the point i'm at the bird should be falling.
https://www.youtube.com/watch?v=SGdAYi_qAVg&list=PLjcN1EyupaQkz5Olxzwvo1OzDNaNLGWoJ&index=2
error
Expected type '_SpriteSupportsGroup | AbstractGroup[_SpriteSupportsGroup | Any] | Iterable[_SpriteSupportsGroup | Any] | Any' (matched generic type '_TSprite ≤: _SpriteSupportsGroup | AbstractGroup[_TSprite ≤: _SpriteSupportsGroup] | Iterable[_TSprite ≤: _SpriteSupportsGroup]'), got 'Bird' instead
here the code:
import pygame
pygame.init()
SCREEN = pygame.display.set_mode((600,600))
pygame.display.set_caption('Flappy Bird')
class Bird(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("imgs/bird1.png")
self.rect = self.image.get_rect()
self.rect.center = (30,300)
self.vel = 0
def update(self):
self.vel += 0.5
if self.rect.bottom < 600:
self.rect.y += int(self.vel)
bird_group = pygame.sprite.Group()
flappy = Bird()#here where i'm making the bird
bird_group.add(flappy) #here the error
running = True
while running:
SCREEN.fill((173,216,230))
bird_group.draw(SCREEN)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
pygame.quit()
请先登录再写评论。
Hi there
Can you please post a screenshot of this error ? is this an error or warning ?
Also can you please try to run the python file from the terminal in the same as it is visible in pycharm terminal?
If you get the same result the issue is not IDE related.
Kind regards,
as you can see line 24 is the error, but line 21 has lost its color for some reason.
Do you mean run it on my computer terminal?
if so …
'pygame.quit' is not recognized as an internal or external command,
operable program or batch file.
is what popped up
Hi there
The warning you are pointing out is due to a type mismatch of what the
bird_groupis expecting and what is passed.The editor in the video (not PyCharm) possibly does not have such features to warn when there is a type mismatch and while in this case it can be safely avoided maybe in other cases it can be useful to get warned for a type mismatch.
You can suppress this warning by adding
# type: ignorein the end of the line as shown in the screenshot.Regarding the colour of line 21 this is as expected.
Kind regards,
I know it say mismatch but if look at class. I made a sprite class or that what the video said. and the picture is from pycharm community.
class Bird(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image = pygame.image.load("imgs/bird1.png")self.rect = self.image.get_rect()self.rect.center = (30,300)self.vel = 0ain't this class a sprite class? or did i do something wrong?
Hi there
You are not doing anything wrong here and the code is being executed successfully.
The warning happens also when using a Sprite class (not just Bird class), I believe the root cause is related to how PyCharm is treating the types from subclasses which is throwing this warning however this might need some more in depth analysis.
As for this case it can be safely ignored and if it is an eye sore please do add the
# type: ignoresnippetI did find an issue reported to improve typing in AbstractGroup from which pygame.sprite.Group() is inherited from because it is too strict, so this could be as well related to the pygame libraries.
https://github.com/pygame/pygame/issues/2642
Please let me know if this is OK with you or you would like me to escalate this case internally for further analysis.
Kind regards,