为什么 pygame 显示不正确?
Why is pygame display not bliting properly?
我正在尝试使用 Pygame 在 Python 中制作基于图块的 2d 平台游戏。我刚开始创建 window 和 tile 系统。它引用一个文本文件,并根据在文件中找到的每个数字,将图像 blits 到 Pygame 显示器 window(“2”表示草图像,“1”表示污垢图像)。当程序执行时,方块出现在屏幕上但快速闪烁并缓慢移动到一侧。瓷砖之间也有缝隙,我不确定为什么会有缝隙,但我想摆脱它们。
import pygame, sys
pygame.init()
dirt_img = pygame.image.load("dirt2.png") #loads dirt image
dirt_img = pygame.transform.scale(dirt_img, (80,80)) #scales dirt image up to 80*80
grass_img = pygame.image.load("grass2.png") #loads grass image
grass_img = pygame.transform.scale(grass_img, (80,80)) #scales grass image up to 80*80
clock = pygame.time.Clock()
window = pygame.display.set_mode((1200, 800))
#load map
def load_map(path):
f = open(path + '.txt','r') #open text file
data = f.read() #reads it
f.close() #closes
data = data.split('\n') #splits the data by the new line character
game_map = [] #creates game map data
for row in data:
game_map.append(list(row)) #ads each line in'map.txt'..
#..data to new game map list
return game_map
game_map = load_map('map')
grass_count = 0 #meant to be used to count each time a grass tile is blitted to..
#..move the position over 80 pixles for the next tile to be blited
dirt_count = 0 # I think this might be where my problem is but I am not sure.
# Main loop
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
window.fill((135, 178,255)) #sets light Blue background color
for layer in game_map:
for tile in layer:
if tile == '1': #finds '1' in file,
dirt_count += 1 #updates dirt count,
window.blit(dirt_img, (100 * dirt_count + 80, 500))#blits next dirt tile
if tile == '2': #finds '2' in file,
grass_count += 1 #updates grass count,
window.blit(grass_img, (100 * grass_count + 80, 500))#blits next tile
clock.tick(60)
pygame.display.update()
pygame.quit()
变量 dirt_count
和 grass_count
递增,但它们永远不会变回 0。在循环之前将变量设置为 0:grass_count = 0
grass_count = 0
.无论如何,我认为这不会让您满意,因为图块的坐标似乎并不依赖于它的索引。
图块的位置很可能取决于 row
和 column
:
for row, layer in enumerate(game_map):
for column, tile in enumerate(layer):
x, y = 80 + column * 100, 80 + row * 100
if tile == '1':
window.blit(dirt_img, (x, y))
if tile == '2':
window.blit(grass_img, (x, y))
我正在尝试使用 Pygame 在 Python 中制作基于图块的 2d 平台游戏。我刚开始创建 window 和 tile 系统。它引用一个文本文件,并根据在文件中找到的每个数字,将图像 blits 到 Pygame 显示器 window(“2”表示草图像,“1”表示污垢图像)。当程序执行时,方块出现在屏幕上但快速闪烁并缓慢移动到一侧。瓷砖之间也有缝隙,我不确定为什么会有缝隙,但我想摆脱它们。
import pygame, sys
pygame.init()
dirt_img = pygame.image.load("dirt2.png") #loads dirt image
dirt_img = pygame.transform.scale(dirt_img, (80,80)) #scales dirt image up to 80*80
grass_img = pygame.image.load("grass2.png") #loads grass image
grass_img = pygame.transform.scale(grass_img, (80,80)) #scales grass image up to 80*80
clock = pygame.time.Clock()
window = pygame.display.set_mode((1200, 800))
#load map
def load_map(path):
f = open(path + '.txt','r') #open text file
data = f.read() #reads it
f.close() #closes
data = data.split('\n') #splits the data by the new line character
game_map = [] #creates game map data
for row in data:
game_map.append(list(row)) #ads each line in'map.txt'..
#..data to new game map list
return game_map
game_map = load_map('map')
grass_count = 0 #meant to be used to count each time a grass tile is blitted to..
#..move the position over 80 pixles for the next tile to be blited
dirt_count = 0 # I think this might be where my problem is but I am not sure.
# Main loop
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
window.fill((135, 178,255)) #sets light Blue background color
for layer in game_map:
for tile in layer:
if tile == '1': #finds '1' in file,
dirt_count += 1 #updates dirt count,
window.blit(dirt_img, (100 * dirt_count + 80, 500))#blits next dirt tile
if tile == '2': #finds '2' in file,
grass_count += 1 #updates grass count,
window.blit(grass_img, (100 * grass_count + 80, 500))#blits next tile
clock.tick(60)
pygame.display.update()
pygame.quit()
变量 dirt_count
和 grass_count
递增,但它们永远不会变回 0。在循环之前将变量设置为 0:grass_count = 0
grass_count = 0
.无论如何,我认为这不会让您满意,因为图块的坐标似乎并不依赖于它的索引。
图块的位置很可能取决于 row
和 column
:
for row, layer in enumerate(game_map):
for column, tile in enumerate(layer):
x, y = 80 + column * 100, 80 + row * 100
if tile == '1':
window.blit(dirt_img, (x, y))
if tile == '2':
window.blit(grass_img, (x, y))