【问题标题】:Pygame: game displays nothing, despite 'while' loopPygame:游戏不显示任何内容,尽管有'while'循环
【发布时间】:2015-09-15 16:19:15
【问题描述】:

我正在开发一个简单的游戏(“Dodger”游戏的半副本),游戏运行,但没有显示任何内容。我有一个while循环正在运行,为什么什么都没有出现?是间距、图像本身的问题,还是我只是忽略了某些东西?

import pygame,sys,random, os
from pygame.locals import *
pygame.init()
#This One Works!!!!!!!


WINDOWHEIGHT = 1136
WINDOWWIDTH = 640
FPS = 40
TEXTCOLOR = (255,255,255)
BACKGROUNDCOLOR = (0,0,0)
PLAYERMOVEMENT = 6
HARVEYMOVEMENT = 5
TJMOVEMENT = 7
LASERMOVEMENT = 10
ADDNEWBADDIERATE = 8
COLOR = 0
TJSIZE = 65
HARVEYSIZE = 65
#Check the sizes for these
def terminate():
    if pygame.event() == QUIT:
        pygame.quit()

def startGame():
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    terminate()
                return
def playerHasHitBaddies(playerRect,TjVirus,HarVirus):
    for b in TjVirus and HarVirus:
        if playerRect.colliderect(b['rect']):
            return True
        return False
def drawText(text,font,surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

mainClock = pygame.time.Clock()
WindowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.mouse.set_visible(False)
pygame.display.set_caption('Virus')




#Player Images

# Check the name of the .png file
TjImage = pygame.image.load('Virus_TJ_edited-1.png')
TjRect = TjImage.get_rect()
#chanhe this part from the baddies variable in the 'baddies' area
playerImage = pygame.image.load('Tank_RED.png')
playerRect = playerImage.get_rect()
LaserImage = pygame.image.load('laser.png')
LaserRect = LaserImage.get_rect()

pygame.display.update()
startGame()



while True:

    TjVirus = []#the red one / make a new one for the blue one
    HarVirus = []#The BLue one / Need to create a new dictionary for this one
    playerRect.topleft = (WINDOWWIDTH / 2, WINDOWHEIGHT - 50)
    moveLeft = moveRight = moveUp = moveDown = laser = False
    baddieAddCounter = 0




    while True:


        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()


            if event.type == KEYDOWN:
                if event.key == ord('a'):
                    moveRight = False
                    moveLeft = True
                if event.key == ord('d'):
                    moveLeft = False
                    moveRight = True
                if event.key == ord('w'):
                    moveDown = False
                    moveUp = True
                if event.key == ord('s'):
                    moveUp = False
                    moveDown = True
                if event.key == K_SPACE:
                    lasers = True


                if event.type == KEYUP:
                    if evnet.type == K_ESCAPE:
                        terminate()

                if event.key == K_LEFT or event.key == ord('a'):
                    moveLeft = False
                if event.key == K_RIGHT or event.key == ord('d'):
                    moveRight = False
                if event.key == K_UP or event.key == ord('w'):
                    moveUp = False
                if event.key == K_DOWN or event.key == ord('s'):
                    moveDown = False
                if event.key == K_SPACE:
                    LaserImage.add(LaserRect)
                if event.key == ord('j'):
                    COLOR = 2
                if event.key == ord('k'):
                    if COLOR == 2:
                        COLOR = 1
                        playerImage = pygame.image.load('Tank_RED.png')
                    if COLOR == 1:
                        COLOR = 2
                        playerImage = pygame.image.load('Tank_BLUE.png')



            if event.type == MOUSEMOTION:
                playerRect.move_ip(event.pos[0] - playerRect.centerx, event.pos[1] - playerRect.centery)

        if baddieAddCounter == ADDNEWBADDIERATE:
            baddieAddCounter = 0
#Dict for TJ(RED) VIRUS
            baddieSize = (TJSIZE)
            NewTjVirus = {'rect':pygame.Rect(random.rantint(0,WINDOWWIDTH - TJSIZE),0 - TJSIZE,TJSIZE,TJSIZE),
                         'speed':(TJMOVEMENT),
                         'surface':pygame.transform.scale(TJImage,(TJSIZE,TJSIZE)),
                         }
            TjVirus.append(NewTjVirus)

        #Dict for Harvey(BLUE) virus
            baddieSize = (HARVEYSIZE)
            NewHarveyVirus = {'rect':pygame.Rect(random.randint(0,WINDOWWIDTH - HARVEYSIZE),0 - HARVEYSIZE,HARVEYSIZE,HARVEYSIZE),
                              'speed':(HARVEYMOVEMENT),
                              'surface':pygame.transform.scale(HARVEYSIZE,(HARVEYSIZE,HARVEYSIZE))
                              }
            HarVirus.append(NewHarveyVirus)
#Player Movement
        if moveLeft and playerRect.left >0:
            playerRect.move_ip(-1*PLAYERMOVEMENT,0)
        if moveRight and playerRect.right < WINDOWWIDTH:
            playerRect.move_ip(PLAYERMOVEMENT,0)
        if moveUp and playerRect.top >0:
            playerRect.move_ip(0,-1*PLAYERMOVEMENT)
        if moveDown and playerRect.bottom < WINDOWHEIGHT:
            playerRect.move_ip(0,PLAYERMOVEMENT)

            pygame,mouse.set_pos(playerRect.centerx,playerRect.centery)

            #Need to change for each individual virus
            for b in HarVirus and TjVirus:
                b['rect'].move_ip(0,b['speed'])

            for b in HarVirus and TjVirus:
                if b['rect'].top > WINDOWHEIGHT:
                    baddies.remove(b)


            windowSurface.fill(pygame.image.load('Background_Proto copy.png'))

        for b in HarVirus and TjVirus:
            windowSurface.blit(b['surface'],b['rect'])

        pygame.display.update()

        if playerHasHitBaddies(playerRect,HarVirus,TjVirus):
                break
        for b in TjVirus and HarVirus[:]:
            if b['rect'].top < WINDOWHEIGHT:
                HarVirus.remove(b)
                TjVirus.remove(b)


        mainClock.tick(FPS)

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    通常我使用pygame.display.flip() 而不是pygame.display.update()

    【讨论】:

    • 感谢您发布此问题的答案!这个答案很短,但没有提供太多背景信息。请解释其背后的一些原因,它将对提问者和未来的读者变得更加有用。谢谢!
    • 感谢您提供可能的解决方案,但它似乎不起作用。还有其他建议吗?
    【解决方案2】:

    您在脚本中调用startGame(),如下所示:

    def startGame():
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    terminate()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        terminate()
                    return
    

    游戏一直停留在while 循环中,直到您按下一个键。


    这是您当前的问题,但也有其他错误,例如

    def terminate():
        if pygame.event() == QUIT:
            pygame.quit()
    

    pygame.event() 不可调用。也许你想在这里传递一个事件作为参数?

    还有

    pygame,mouse.set_pos(playerRect.centerx,playerRect.centery)
    

    , 而不是.

    【讨论】:

    • 感谢您的建议,quit 模块现在可以工作了。但是,游戏仍然很顽固,无法运行。我可以成为我没有看到的东西吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    相关资源
    最近更新 更多