Esempio n. 1
0
def fireShell(n: Network):
    data = n.ReadData()
    curX, curY = data[0], data[1]
    while data != "HIT":
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        # print(startingShell[0],startingShell[1])
        curX, curY = data[0], data[1]
        pygame.draw.circle(gameDisplay, red, (curX, curY), 5)
        pygame.display.update()
        data = n.ReadData()
        clock.tick(60)
    players = n.ReadData()
    player, enemy = players[0], players[1]
    explosion(curX, curY)
    return [player, enemy]
Esempio n. 2
0
def gameLoop(n : Network):
    gameExit = False
    gameOver = False
    FPS = 15
    playersInfo = n.ReadData()
    mainInfo, enemyInfo, pNo = playersInfo[0], playersInfo[1], playersInfo[2]
    player_health, enemy_health = mainInfo.health, enemyInfo.health
    mainTankX, mainTankY, enemyTankX, enemyTankY = mainInfo.x, mainInfo.y, enemyInfo.x, enemyInfo.y
    currentTurPos, enemyTurPos = mainInfo.turPos, enemyInfo.turPos
    tankColor, enemyColor = mainInfo.color, enemyInfo.color

    # barrier_width = 50
    Y = display_height - ground_height
    radius = 60

    tankMove = 0
    changeTur = 0

    fire_power = 50
    power_change = 0

    xlocation = int(display_width / 2)

    while not gameExit:

        if gameOver == True:
            # gameDisplay.fill(white)
            message_to_screen("Game Over", red, -50, size="large")
            message_to_screen("Press C to play again or Q to exit", black, 50)
            pygame.display.update()
            while gameOver == True:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        gameExit = True
                        gameOver = False

                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_c:
                            gameLoop()
                        elif event.key == pygame.K_q:

                            gameExit = True
                            gameOver = False
        
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                gameExit = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    tankMove = -5

                elif event.key == pygame.K_RIGHT:
                    tankMove = 5

                elif event.key == pygame.K_UP:
                    changeTur = 1

                elif event.key == pygame.K_DOWN:
                    changeTur = -1

                elif event.key == pygame.K_p:
                    pause()

                elif event.key == pygame.K_SPACE:
                    n.SendData(["Firing", fire_power])
                elif event.key == pygame.K_a:
                    power_change = -1
                elif event.key == pygame.K_d:
                    power_change = 1

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    tankMove = 0

                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    changeTur = 0

                if event.key == pygame.K_a or event.key == pygame.K_d:
                    power_change = 0
        
        mainTankX += tankMove
        currentTurPos += changeTur

        if currentTurPos > 8:
            currentTurPos = 8
        elif currentTurPos < 0:
            currentTurPos = 0
        n.SendData([tankMove, currentTurPos])
        gameDisplay.fill(black)
        health_bars(player_health, enemy_health, pNo)
        tank(mainTankX, mainTankY, currentTurPos,tankColor)
        enemy_tank(enemyTankX, enemyTankY, enemyTurPos,enemyColor)

        fire_power += power_change

        if fire_power > 100:
            fire_power = 100
        elif fire_power < 1:
            fire_power = 1

        power(fire_power)

        barrier(xlocation, Y, radius)
        gameDisplay.fill(green, rect=[0, display_height - ground_height, display_width, ground_height])
        pygame.display.update()

        if player_health < 1:
            game_over()
        elif enemy_health < 1:
            you_win()
        clock.tick(FPS)
        info = n.ReadData()
        if type(info) == type(" ") and info == "FIRING":
            players = fireShell(n)
            player, enemy = players[0], players[1]
            player_health, enemy_health = player.health, enemy.health
            continue
        players = info
        try:
            enemyData = players[1]
            mainData = players[0]
            enemyTankX = enemyData.x
            mainTankX = mainData.x
            player_health = mainData.health
            enemy_health = enemyData.health
            enemyTurPos = enemyData.turPos
        except:
            pass
    pygame.quit()
    quit()