Reputation: 278
I'm trying to make a game in Python using pygame, but I accidentally had disabled the error checking in PyCharm. As a result, this is how I tried to initialize a Vector2
:
self.vel = pg.Vector2(0, 0)
self.acc = pg.Vector2(0, self.GRAVITY)
After I re-enabled the error checking, PyCharm gave me an error message, telling me to use pg.Vector2.__new__(0, 0)
instead. After I did, the error message disappeared and the code worked.
Now for the actual question:
While the error messages were disabled, I wrote a lot of bad code like the example above. Strange enough, the code actually ran fine. The game could init and run properly, even while half the code had errors.
Could someone explain to me why the example above works, yet why it is considered bad?
Full code:
import pygame as pg
from game.settings import *
class Player(pg.sprite.Sprite):
"""
I'm a docstring hurr durr
"""
ACCELERATION = 60 / FPS # dummy value
FRICTION = 1 # dummy value
GRAVITY = 1 # dummy value
LIFE = 5
def __init__(self, x, y, h, w):
super().__init__()
self.x = x # x position
self.y = y # y position
self.h = h # height
self.w = w # width
self.vel = pg.Vector2.__new__(0, 0)
self.acc = pg.Vector2.__new__(0, self.GRAVITY)
self.rect = pg.Rect(x, y, w, h)
self.image = pg.Surface(w, h)
self.image.fill(RED)
# self.image = pg.image.load("../resources/player.*")
def update(self):
# key updates
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.acc.x -= self.a
if keys[pg.K_RIGHT]:
self.acc.x += self.a
# test jumping function
if keys[pg.K_SPACE]:
self.vel.y = -20
# friction
self.acc.x -= self.vel.x * self.FRICTION
# update vel and pos
self.vel.x += self.acc.x
self.x += self.vel.x
self.vel.y += self.acc.y
self.y += self.vel.y
Upvotes: 4
Views: 387
Reputation: 4653
The problem here is that PyCharm doesn't know what the real signature of the __init__
method should be, because the file is distributed as a .pyd file (like a .dll). You can verify this by using the "Go to... Declaration" (Ctrl+B) function in pycharm on pg.Vector2
in your code, and then on pygame.math.Vector2
in pygame's __init__.py
until you reach the code of pygame.math
. Here you'll see a declaration like this:
def __init__(self): # real signature unknown; restored from __doc__
pass
Observe also the note at the top of this math.py
file:
# from C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\pygame\math.cp35-win_amd64.pyd
# by generator 1.145
PyCharm is doing its best to try and generate something that looks like the actual code, but it is just a guess.
In short, you are right and pycharm is wrong in this case.
The exact same type of problem is described here:
Why do some built-in Python functions only have pass?
Upvotes: 4