将字符保持在屏幕顶部

时间:2014-07-07 15:11:22

标签: python-3.x pygame

我有一个python游戏。当您按1然后单击某处时,您可以放置​​一名士兵。我已经在这一段时间了,无法弄清楚如何让士兵留下来。我有一份士兵名单,每次放置士兵时都会填满。当您达到最大金额时,您将无法再放置。我知道我在每个循环中调用pygame.display.set_mode(),这是故意的。我知道发生的事情是士兵被吸引了。我怎么知道哪些士兵要搞哪些士兵以及哪些士兵不要搞砸?

import pygame, random, sys
from pygame.locals import *

class Menu():
    def Main(self):
        pygame.init()
        MenuChoice = 1
        noMoreRand = 0
        RocketY = 750
        ExpNum = 0
        Soldier1Coords = pygame.mouse.get_pos()
        while 1:
            screen = pygame.display.set_mode((1366, 768))
            pygame.display.set_caption("Throne Paranoia")
            Title = pygame.image.load("TitleScreen.png").convert_alpha()
            screen.blit(Title, (0, 0))
            font = pygame.font.Font(None, 64)
            img1 = font.render("Throne Paranoia", 1, (0, 50, 255))
            screen.blit(img1, (5, 5))
            img2 = font.render("Classic", 1, (255, 50, 0))
            screen.blit(img2, (200, 65))
            img3 = font.render("Hardcore", 1, (255, 50, 0))
            screen.blit(img3, (200, 115))
            img4 = font.render("Survival", 1, (255, 50, 0))
            screen.blit(img4, (200, 165))
            img5 = font.render("Quit", 1, (255, 50, 0))
            screen.blit(img5, (200, 715))
            pointer = pygame.image.load("pointer.png").convert_alpha()
            if noMoreRand == 1:
                if RocketY > 50:
                    RocketY = RocketY - 5
                    screen.blit(rocket, (1000, RocketY))
                if RocketY <= 55:
                    Exp1 = pygame.image.load("EXP1.png")
                    Exp2 = pygame.image.load("EXP2.png")
                    screen.blit(Exp1, (700, 50))
                    pygame.display.flip()
                    pygame.time.wait(200)
                    screen = pygame.display.set_mode((1366, 768))
                    pygame.display.set_caption("Throne Paranoia")
                    Title = pygame.image.load("TitleScreen.png").convert_alpha()
                    screen.blit(Title, (0, 0))
                    font = pygame.font.Font(None, 64)
                    img1 = font.render("Throne Paranoia", 1, (0, 50, 255))
                    screen.blit(img1, (5, 5))
                    img2 = font.render("Classic", 1, (255, 50, 0))
                    screen.blit(img2, (200, 65))
                    img3 = font.render("Hardcore", 1, (255, 50, 0))
                    screen.blit(img3, (200, 115))
                    img4 = font.render("Survival", 1, (255, 50, 0))
                    screen.blit(img4, (200, 165))
                    img5 = font.render("Quit", 1, (255, 50, 0))
                    screen.blit(img5, (200, 715))
                    pointer = pygame.image.load("pointer.png").convert_alpha()
                    screen.blit(Exp2, (700, 50))
                    pygame.display.flip()
                    pygame.time.wait(200)
                    RocketY = 750
                    noMoreRand = 0

            if MenuChoice == 5:
                MenuChoice = 1
            elif MenuChoice == 0:
                MenuChoice = 4
            elif MenuChoice == 1:
                screen.blit(pointer, (20, 50))
            elif MenuChoice == 2:
                screen.blit(pointer, (20, 100))
            elif MenuChoice == 4:
                screen.blit(pointer, (20, 700))
            elif MenuChoice == 3:
                screen.blit(pointer, (20, 150))
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == K_UP:
                        MenuChoice = MenuChoice - 1
                    elif event.key == K_DOWN:
                        MenuChoice = MenuChoice + 1
                    elif event.key == K_RETURN:
                        if MenuChoice == 4:
                            pygame.quit()
                            sys.exit()
                        elif MenuChoice == 1:
                            Game().Classic()
                        elif MenuChoice == 2:
                            Game().Hardcore()
                        elif MenuChoice == 3:
                            Game().Survival()
            if noMoreRand == 0:
                MissileRand = random.randint(0, 30)
            if noMoreRand == 0 and MissileRand == 17:
                print("Rocket Fired!")
                noMoreRand = 1
                rocket = pygame.image.load("Rocket.png")
                rocket = pygame.transform.scale(rocket, (50, 150))

            pygame.display.update()

class Game():
    def Classic(self):
        screen = pygame.display.set_mode((1366, 768))
        base_type = "basic"
        soldiers = 2
        cash = 500
        font = pygame.font.Font(None, 32)
        slot1 = font.render("1", 1, (255, 255, 255))
        slot2 = font.render("2", 1, (255, 255, 255))
        slot3 = font.render("3", 1, (255, 255, 255))
        slot4 = font.render("4", 1, (255, 255, 255))
        stuff1 = pygame.image.load("Soldier.png")
        stuff1 = pygame.transform.scale(stuff1, (68, 68))
        stuff2 = pygame.image.load("Turret.png")
        stuff2 = pygame.transform.scale(stuff2, (68, 68,))
        slot1obj = "Spawn Soldier"
        slot2obj = "Place Turret"
        slot3obj = "Upgrade Menu"
        slot4obj = "Sell"
        Soldier = pygame.image.load("Soldier.png")
        Turret = pygame.image.load("Turret.png")
        SoldierCoords = []
        I=0
        while I<10:
            SoldierCoords.append(I)
            I=I+1
        I=0
        while 1:
            screen = pygame.display.set_mode((1366, 768))
            background = pygame.image.load("Grass.png")
            screen.blit(background, (0, 0))
            screen.blit(slot1, (30, 680))
            screen.blit(slot2, (98, 680))
            screen.blit(slot3, (166, 680))
            screen.blit(stuff1, (0, 700))
            screen.blit(stuff2, (69, 700))
            if base_type == "basic":
                base = pygame.image.load("BasicBase.png")
            elif base_type == "military":
                base = pygame.image.load("MilitaryBase.png")
            elif base_type == "castle":
                base = pygame.image.load("Castle.png")
            screen.blit(base, (498, 671))
            pygame.draw.line(screen, (255, 255, 255), (0, 700), (498, 700), 1)
            pygame.draw.line(screen, (255, 255, 255), (68, 700), (68, 769), 1)
            pygame.draw.line(screen, (255, 255, 255), (868, 700), (1367, 700), 1)
            pygame.draw.line(screen, (255, 255, 255), (136, 700), (136, 769), 1)

            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if event.key == K_1:
                        currentSelection = font.render(slot1obj, 1, (255, 255, 255))
                        screen.blit(currentSelection, (683, 500))
                        pygame.display.flip()
                        pygame.time.wait(200)
                        Selection = 1
                    elif event.key == K_2:
                        currentSelection = font.render(slot2obj, 1, (255, 255, 255))
                        screen.blit(currentSelection, (683, 500))
                        pygame.display.flip()
                        pygame.time.wait(200)
                        Selection = 2
                    elif event.key == K_ESCAPE:
                        Pause().Pause()
                    pressed = pygame.mouse.get_pressed()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if I>9:
                        tell1 = font.render("You can't spawn any more soldiers.", 1, (255, 255, 255))
                        screen.blit(tell1, (600, 864))
                        pygame.display.flip()
                        print("No more soldiers are allowed.")
                        pygame.time.wait(1000)
                        break
                    SoldierCoords[I] = pygame.mouse.get_pos()
                    soldiers = soldiers - 1
                    screen.blit(Soldier, SoldierCoords[I])
                    pygame.display.flip()
                    print(I)
                    print("Spawning soldier!")
                    I=I+1
                elif event.type == pygame.QUIT:
                    Pause().Pause()




            pygame.display.flip()


class Pause():
    def Pause(self):
        while 1:
            screen = pygame.display.set_mode((640, 480))
            font = pygame.font.Font(None, 64)
            Msg1 = font.render("Paused.", 1, (255, 255, 255))
            Msg2 = font.render("Press ESC to continue or", 1, (255, 255, 255))
            Msg3 = font.render("the space bar to exit to", 1, (255, 255, 255))
            Msg4 = font.render("the main menu.", 1, (255, 255, 255))
            screen.blit(Msg1, (20, 20))
            screen.blit(Msg2, (20, 100))
            screen.blit(Msg3, (20, 180))
            screen.blit(Msg4, (20, 260))
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        return 0
                    if event.key == K_SPACE:
                        Menu().Main()
                elif event.type == pygame.QUIT:
                    return 0
Menu().Main()

1 个答案:

答案 0 :(得分:1)

你必须在SoldierCoords

之前的每个循环中对来自pygame.display.flip()的所有士兵进行blit

<强>顺便说一句:

使用

screen = pygame.display.set_mode((1366, 768))
pygame.display.set_caption("Throne Paranoia")
Title = pygame.image.load("TitleScreen.png").convert_alpha()

之前while Truewhile 1

您不必在每个循环中创建屏幕并加载标题图像 最终你可以在每个循环中用黑色填充屏幕。

更糟糕的是 - 在每个循环中再次加载所有图像,然后再次渲染所有文本 只做一次 - 因为while 1

如果您在课程中有__init__,则可以在__init__中加载图像并渲染文本,但是您必须在开头时使用类实例。

在循环中只使用pygame.display.flip()一次 - 最后。


BTW: Python中有规则 - 在变量名中使用小写和'_' 例如SoldierCoords =&gt; soldier_coords