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