如何避免闪烁的游戏显示?

时间:2015-05-31 09:18:38

标签: python pygame

出于某种原因,一个奇怪的波浪形波浪不断超越我的我的游戏。我无法计算问题,所以我真的需要帮助,因为我已经努力从头开始制作游戏。如果您发现任何问题导致奇怪的波浪,请尽快回复。

以下是代码:

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

BLACK = (0, 0, 0      )
BROWN = (153, 76, 0   )
GREEN = (0, 255, 0    )
BLUE  = (0, 0, 255    )
WHITE = (255, 255, 255)

DIRT  = 0
GRASS = 1
WATER = 2
COAL  = 3

textures = {
                DIRT  : pygame.image.load("dirt.png"),
                GRASS : pygame.image.load("grass.png"),
                WATER : pygame.image.load("water.png"),
                COAL  : pygame.image.load("coal.png")
        }

inventory = {
               DIRT  : 0,
               GRASS : 0,
               WATER : 0,
               COAL  : 0
        }

TILESIZE  = 20
MAPWIDTH  = 30
MAPHEIGHT = 20

PLAYER = pygame.image.load("player.png")
playerPos = [0, 0]

resources = [DIRT,GRASS,WATER,COAL]
tilemap = [ [DIRT for w in range(MAPWIDTH)] for h in range(MAPHEIGHT) ]

pygame.init()
DISPLAYSURF = pygame.display.set_mode((MAPWIDTH*TILESIZE, MAPHEIGHT*TILESIZE + 50))

INVFONT = pygame.font.Font(None, 18)

for rw in range(MAPHEIGHT):
    for cl in range(MAPWIDTH):
        randomNumber = random.randint(0, 15)
        if randomNumber == 0:
            tile = COAL
        elif randomNumber == 1 or randomNumber == 2:
            tile = WATER
        elif randomNumber >3 and randomNumber <= 7:
            tile = GRASS
        else:
            tile = DIRT
        tilemap[rw][cl] = tile

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_RIGHT and playerPos[0] < MAPWIDTH - 1:
                playerPos[0] += 1
            if event.key == K_LEFT and playerPos[0] > 0:
                playerPos[0] -= 1
            if event.key == K_UP and playerPos[1] > 0:
                playerPos[1] -= 1
            if event.key == K_DOWN and playerPos[1] < MAPHEIGHT -1:
                playerPos[1] += 1
            if event.key == K_SPACE:
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                inventory[currentTile] += 1
                tilemap[playerPos[1]][playerPos[0]] = DIRT
            if (event.key == K_1):
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                if inventory[DIRT] > 0:
                    inventory[DIRT] -= 1
                    tilemap[playerPos[1]][playerPos[0]] = DIRT
                    inventory[currentTile] += 1

    for row in range(MAPHEIGHT):
        for column in range(MAPWIDTH):
            DISPLAYSURF.blit(textures[tilemap[row][column]], (column*TILESIZE,row*TILESIZE))

        DISPLAYSURF.blit(PLAYER,(playerPos[0]*TILESIZE,playerPos[1]*TILESIZE))

        placePosition = 10
        for item in resources:
            DISPLAYSURF.blit(textures[item],(placePosition,MAPHEIGHT*TILESIZE+20))
            placePosition += 30
            textObj = INVFONT.render(str(inventory[item]), True, WHITE, BLACK)
            DISPLAYSURF.blit(textObj,(placePosition,MAPHEIGHT*TILESIZE+20))
            placePosition += 50

        pygame.display.update()

1 个答案:

答案 0 :(得分:0)

问题是你在 new { token: "somestring"} angular.callbacks._a({"token":"somestring"}) 循环中调用 Pygames pygame.display.update() 函数每次迭代。之后尝试致电MAPHEIGHTpygame.display.update()

pygame.display.flip()

希望这会有所帮助:)