python - How to avoid a flickering game display? -
for reason weird squiggly wave keeps coming on minecraft game. cannot calculate problem need because have worked hard come game scratch. if see problems cause strange wave please respond possible.
here's code:
import pygame, sys, random 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 w in range(mapwidth)] h in range(mapheight) ] pygame.init() displaysurf = pygame.display.set_mode((mapwidth*tilesize, mapheight*tilesize + 50)) invfont = pygame.font.font(none, 18) rw in range(mapheight): 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 , randomnumber <= 7: tile = grass else: tile = dirt tilemap[rw][cl] = tile while true: event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() elif event.type == keydown: if event.key == k_right , playerpos[0] < mapwidth - 1: playerpos[0] += 1 if event.key == k_left , playerpos[0] > 0: playerpos[0] -= 1 if event.key == k_up , playerpos[1] > 0: playerpos[1] -= 1 if event.key == k_down , 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 row in range(mapheight): 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 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()
the problem you call pygames pygame.display.update()
function each iteration in mapheight
loop. try call pygame.display.update()
or pygame.display.flip()
afterwards:
#import , game set ... #main game loop while true: #event loop event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() ... row in range(mapheight): 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 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 #update (draw) screen pygame.display.update()
hope helps :)
Comments
Post a Comment