1

我正在尝试将起始玩家指定为“第一个”CurrentPlayer,然后在玩家 1 完成回合后将玩家 2 指定为 CurrentPlayer。不知何故,我根本无法让它工作。

    """ This is the model of the game"""
    class Game:
        def __init__(self, cannonSize, ballSize):
            p1 = Player("blue", -90)
            p2 = Player("red", 90)
            self.players = [p1,p2]
    
            self.CurrentPlayer = self.players[0]
    
            if self.CurrentPlayer == self.players[0]:
                self.OtherPlayer = self.players[1]
            else:
                self.OtherPlayer = self.players[0]
    
            if self.getCurrentPlayer == 0:
                self.CurrentPlayerNumber = 0
            else:
                self.CurrentPlayerNumber = 1
    
    
        """ A list containing both players """
        def getPlayers(self):
            return self.players
    
        """ The current player, i.e. the player whose turn it is """
        def getCurrentPlayer(self):
            return self.CurrentPlayer
    
        """ The opponent of the current player """
        def getOtherPlayer(self):
            return self.OtherPlayer
    
        """ The number (0 or 1) of the current player. This should be the position of the current player in getPlayers(). """
        def getCurrentPlayerNumber(self):
            return self.CurrentPlayerNumber

       """ Switch active player """
        def nextPlayer(self):
            if self.CurrentPlayer == self.players[0]:
               return self.CurrentPlayer == self.players[1]
            else:
               return self.CurrentPlayer == self.players[0]

我使用(来自大学)的测试代码告诉我以下内容:

C:\Users\Phili\PycharmProjects\pythonLab2\venv\Scripts\python.exe C:/Users/Phili/PycharmProjects/pythonLab2/testgame.py
Traceback (most recent call last):
  File "C:\Users\Phili\PycharmProjects\pythonLab2\testgame.py", line 181, in <module>
    runTests(gamemodel.Game(10,3))
  File "C:\Users\Phili\PycharmProjects\pythonLab2\testgame.py", line 23, in runTests
    assert game.getCurrentPlayerNumber()==0, "player 0 should start!"
AssertionError: player 0 should start!

Process finished with exit code 1

下面是完整的测试代码:

# A simple testing procedure for the game model

import graphics
import gamemodel
import gamegraphics

def runTests(game):

    players = game.getPlayers()

    assert len(players) == 2, "there should be two players"

    # Testing game initialization
    assert players[0].getColor()=="blue", "player 0 should be blue!"
    assert players[1].getColor()=="red", "player 1 should be red!"

    assert players[0].getX()== -90, "player 0 should stand at x=-90!"
    assert players[1].getX()== 90, "player 1 should stand at x=90!"

    assert players[0].getColor()=="blue"
    assert players[1].getColor()=="red"
    
    assert game.getCurrentPlayerNumber()==0, "player 0 should start!"
    
    assert game.getCurrentPlayer() == players[0], "player 0 should start! (2)"
    assert game.getOtherPlayer()==players[1], "getOtherPlayer() doesn't work"
    assert -10 <= game.getCurrentWind() <= 10, "wind should be a random value in [-10,10]"

    # Testing manually swapping player
    game.nextPlayer()
    assert game.getCurrentPlayerNumber()==1, "player 1 should go second!"
    assert game.getCurrentPlayer() == players[1]
    assert game.getOtherPlayer()==players[0], "getOtherPlayer() doesn't work"

    # Switch back to player 0
    game.nextPlayer()
    assert game.getCurrentPlayer() == players[0], "player 0 should go after player 1!"
    assert game.getOtherPlayer()==players[1], "getOtherPlayer() doesn't work"

    # Turn off wind
    game.setCurrentWind(0)
    assert game.getCurrentWind() == 0, "wind should be 0"


    #Testing "Manual fire" for player 0
    proj = game.getCurrentPlayer().fire(30,31)
    assert (proj.getX() == game.getCurrentPlayer().getX()), "Fired projectile should start at player X-position"
    assert (proj.getY() == 10/2), "Fired projectile Y-position should start half the cannon size"
    assert (proj.isMoving()), "projectile should be moving"

    assert game.getCurrentPlayer().getAim() == (30,31), "After firing, getAim() should give the latest (angle,velocity)-values"

    proj.update(1.0)
    assert (abs(proj.getX() + 63.1532124826824) < 0.01), "Projectile X-Position is {0:f}, should be -63.153212".format(proj.getX())
    assert (abs(proj.getY() - 15.6) < 0.01), "Projectile X-Position is {0:f}, should be 15.6".format(proj.getY())
    assert (proj.isMoving()), "projectile should be moving"

    ticks = 0
    while proj.isMoving():
        proj.update(0.1)
        ticks += 1
        assert ticks <= 25, "projectile should have stopped now..."
    assert ticks == 25, "Incorrect tick-count"

    assert proj.getY()==0.0, "projectile should stop at y=0"
    assert abs(proj.getX() - 3.9637563106115907) < 0.01, "Projectile X-Position is {0:f}, should be -3.9637563106115907".format(proj.getX())

    assert abs(players[1].projectileDistance(proj) - -78.03624368938841) < 0.01, "Projectile X-distance to player is {0:f}, should be -78.03624368938841".format(players[1].projectileDistance(proj))
    assert abs(players[0].projectileDistance(proj) - 85.96375631061159) < 0.01, "Projectile X-distance to player is {0:f}, should be 85.96375631061159".format(players[0].projectileDistance(proj))

    #Switching to player 1
    game.nextPlayer()
    assert game.getCurrentPlayerNumber()==1, "player 1 should go after player 0"
    assert game.getCurrentPlayer() == players[1], "player 1 should go after player 0"
    assert game.getOtherPlayer()==players[0], "getOtherPlayer() doesn't work"


    #Testing "Manual fire" for player 1
    proj = game.getCurrentPlayer().fire(45,41)
    assert(proj.getX() == game.getCurrentPlayer().getX()), "Fired projectile should start at player X-position"
    assert(proj.getY() == 10/2), "Fired projectile Y-position should start half the cannon size"
    assert(proj.isMoving()), "projectile should be moving"

    ticks = 0
    while proj.isMoving():
        proj.update(0.1)
        ticks += 1
        assert ticks <= 61, "projectile should have stopped now..."
    assert ticks == 61, "Incorrect tick-count"
    assert proj.getY()==0.0, "projectile should always stop at y=0"
    assert abs(proj.getX() - -86.84740597475547) < 0.01, "Projectile X-Position is {0:f}, should be -86.84740597475547".format(proj.getX())
    assert abs(players[1].projectileDistance(proj) - -168.84740597475547) < 0.01, "Projectile X-distance to player is {0:f}, should be 168.84740597475547".format(players[1].projectileDistance(proj))
    assert players[0].projectileDistance(proj) == 0, "Projectile X-distance to player is {0:f}, should be 0".format(players[1].projectileDistance(proj))

    # Test scoring
    assert players[0].getScore()==0, "Initial score should be 0"
    players[0].increaseScore()
    assert players[1].getScore()==0, "Score should be 0"
    assert players[0].getScore()==1, "Score should be 1"

    # Test new round
    game.setCurrentWind(1000)
    assert game.getCurrentWind()==1000, "Failed to set wind speed"
    game.newRound()
    assert game.getCurrentWind()!=1000, "Wind should be randomized each round"

    # Test firing with wind
    game.setCurrentWind(-1)
    proj = players[0].fire(45,41)
    assert(proj.getX() == players[0].getX()), "Fired projectile should start at player X-position"
    assert(proj.getY() == 10/2), "Fired projectile Y-position should start half the cannon size"
    assert(proj.isMoving()), "projectile should be moving"

    ticks = 0
    while proj.isMoving():
        proj.update(0.1)
        ticks += 1
        assert ticks <= 61, "projectile should have stopped now..."
        
    assert ticks == 61, "Incorrect tick-count"
    assert abs(proj.getX() - 68.2424059747553) < 0.01, "Projectile X-Position is {0:f}, should be 68.2424059747553".format(proj.getX())
    
    
    # A few additional hints
    gameAtts = len(game.__dict__.items())
    if (gameAtts > 5):
        print("Your Game object has {} attributes. This isn't necessarily wrong, but 5 seems like a nice number.".format(gameAtts))
        print("Make sure you are not representing the same information in multiple attributes.")
    playerAtts = len(game.getCurrentPlayer().__dict__.items())
    if (playerAtts > 8):
        print("Your Player object has {} attributes. This isn't necessarily wrong, but it seems a bit high.".format(playerAtts))



def testGraphics():
    game = gamemodel.Game(10,3)

    ggame = gamegraphics.GameGraphics(game)
    
    # For these tests to work, you need to have a getWindow-method in GraphicGame
    w = ggame.getWindow()

    if ggame.getWindow() is None:
        print("No game window yet, aborting graphics tests")
        return
    
    circ_type = type(graphics.Circle(graphics.Point(0,0), 10))
    rect_type = type(graphics.Rectangle(graphics.Point(0,0), graphics.Point(1,1)))

    if len(w.items) != 5:
        print("WARNING: your graphics does not start with five graphical components. This is likely an error.")

    # Run the standard tests for the model, then sync graphics
    runTests(game)
    ggame.sync()

    if len(w.items) != 7:
        print("WARNING: your graphics does not have seven graphical components after syncing. This is likely an error.")
    

    everything = w.items
    circles = [x for x in w.items if type(x) == circ_type]
    rectangles = [x for x in w.items if type(x) == rect_type]

    assert len(rectangles) == 2, "there should be two rectangles in the graphics"
    assert len(circles) == 2, "there should be two circles drawn after running tests"

    rect_pos = [x.getCenter().getX() for x in rectangles]
    assert -90 in rect_pos and 90 in rect_pos, "rectangles should be at x=-90 and x=90"

    # Fire a red projectile and move it a bit
    game.setCurrentWind(0)
    game.getCurrentPlayer().fire(30, 30).update(2)

    ggame.sync()

    circles = [x for x in w.items if type(x) == circ_type]
    assert len(circles) <= 2, "there should never be more than two circles! You need to undraw old cannonballs"


runTests(gamemodel.Game(10,3))

testGraphics()

# If your graphical window or close immediately after running test, try uncommenting this
#wait = input("Press Enter to terminate.")

有人知道为什么会出现此错误消息吗?我觉得它应该根据 self.CurrentPlayer 分配 self.CurrentPlayerNumber。

任何帮助将不胜感激。

4

0 回答 0