Pygame:游戏什么都不显示,尽管'而#39;环

时间:2015-09-15 16:19:16

标签: python python-2.7 pygame

我正在制作一个简单的游戏(“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)

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您在脚本中调用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)

,代替.