Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
939 views
in Technique[技术] by (71.8m points)

pygame - Python Arguments TypeError using my own newly made game framework

I am making a game in Python using my own newly made framework, but i get this error:

self.collisions2D(self.rect, collisionList)
TypeError: collisions2D() takes 2 positional arguments but 3 were given
   
Process finished with exit code 1

and I do not understand what I am doing wrong

Game File:

import pygame
import os

from dev.nils.pythonGame.framework.objectHandler import *


class Game:
    def __init__(self):
        self.screen_size = (1280, 720)
        self.window = pygame.display.set_mode(self.screen_size)
        self.screen = pygame.Surface((int(self.screen_size[0]/3), int(self.screen_size[1]/3)))
        self.fps = 60
        self.running = True
        self.bgcolor = (0,0,0)
        self.clock = pygame.time.Clock()
        self.handler = Handler("D:/PyCharm/pythonGame/dev/nils/pythonGame/game/levelDesign/map.txt", self.screen)

    def events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.running = False

    def draw(self):
        self.screen.fill(self.bgcolor)

        drawTiles(self.screen, self.handler.getTileList())
        self.handler.draw()
    
        self.window.blit(pygame.transform.scale(self.screen, self.screen_size), (0,0))
        pygame.display.update()

    def update(self):
        self.handler.update()

    def run(self):
        while self.running:
            self.events()
            self.update()
            self.draw()
            self.clock.tick(self.fps)
        pygame.quit()
        exit()

def main():
    os.environ["SDL_VIDEO_CENTERED"] = "1"
    pygame.init()
    Game().run()

if __name__ == "__main__":
    main()

Player File:

import pygame
from dev.nils.pythonGame.framework.physicsObject import *


class Player(PhysicsObject):
    def __init__(self, x:int, y:int, xSize:int, ySize:int):
        PhysicsObject.__init__(self, x, y, xSize, ySize)

    def update(self, collisionList:[]):
        self.collisions2D(self.rect, collisionList)
    
    def draw(self, screen:pygame.Surface):
        pygame.draw.rect(screen, (255,255,255), self.rect)

Tile File:

import pygame
from dev.nils.pythonGame.framework.physicsObject import PhysicsObject

class Tile(PhysicsObject):
    def __init__(self, x:int, y:int, xSize:int, ySize:int):
        PhysicsObject.__init__(self, x, y, xSize, ySize)

def getCollisionList(objectRect:pygame.Rect, tileList:[]):
    collisions = []
    for tile in tileList:
        print(tile)
        if objectRect.colliderect(tile[1].rect):
            collisions.append(tile)
    return collisions

def drawTiles(screen:pygame.Surface, tileList:[]):
    for tile in tileList:
        if tile[0] == "1":
            pygame.draw.rect(screen, (255,255,255), (tile[1].rect.x, tile[1].rect.y,tile[1].rect.width, tile[1].rect.height))

Object Handler:

import pygame
from dev.nils.pythonGame.game.objects.tile import *
from dev.nils.pythonGame.game.objects.player import *
from dev.nils.pythonGame.framework.physicsObject import *


class Handler:
    def __init__(self, path: str, screen: pygame.Surface):
        self.path = path
        self.objects = {
            "player":Player(0, 0, 32, 32)
        }
        self.screen = screen
        self.tileList = self.getTileList()

    def update(self):
        for object in self.objects.values():
            object.update(getCollisionList(object.rect, self.tileList))

    def draw(self):
        for object in self.objects.values():
            object.draw(self.screen)

    def getTileList(self):
        tiles = []
        x = 0
        y = 0
        with open(self.path) as map:
            map = list(map)
            for row in map:
                x = 0
                for tile in row:
                    tiles.append([tile, PhysicsObject(x, y, 16, 16)])
                    x += 1
                y += 1
        return tiles

And physicsObject file:

import pygame


class PhysicsObject:
    def __init__(self, x:int, y:int, xSize:int, ySize:int):
        self.xVel = 0.0
        self.yVel = 0.0
        self.xSize = xSize
        self.ySize = ySize
        self.rect = pygame.Rect(x*self.xSize, y*self.ySize, xSize, ySize)
        self.collisionTypes = {}

    def collisions2D(self, collisionList:[]):
        self.collisionTypes = {"left":False, "right":False, "top":False, "bottom":False}
        self.rect.x += self.xVel
        for object in collisionList:
            if self.xVel > 0:
                self.rect.right = object.left
                self.collisionTypes["left"] = True
            if self.xVel < 0:
                self.rect.left = object.right
                self.collisionTypes["right"] = True

        self.rect.y += self.yVel
        for object in collisionList:
            if self.yVel > 0:
                self.rect.bottom = object.top
                self.collisionTypes["top"] = True
            if self.yVel < 0:
                self.rect.top = object.bottom
                self.collisionTypes["bottom"] = True


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

collisions2D is an Method with the signature:

def collisions2D(self, collisionList:[]):

The method has to be called as follows

self.collisions2D(self.rect, collisionList)

self.collisions2D(collisionList)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...