我做了一个小游戏,但问你可以猜,我有一个问题:p
我的第一堂课(它从游戏的第一部分获得原版画面)
import pygame
from pygame.locals import *
import pygbutton
class part():
def __init__(self,originalscreen):
self.screen = originalscreen
self.part()
def part(self):
""" Finally: the game!"""
# But first, some variables and imports
parton = True
import makemap
# Looping
while parton:
# The screen
self.screen.fill(0)
makemap.mapmaker(self.screen)
# Events
for event in pygame.event.get():
if event.type == QUIT:
exit()
# Updating the screen
pygame.display.flip()
正如您所读,我导入了makemap并将屏幕传递给它
import pygame
from pygame.locals import *
""" Add tiles here"""
grass = pygame.image.load("tiles/grass.png")
def mapmaker(screen):
height = screen.get_height()
width = screen.get_width()
# Set the size of each tile (25 pixels)
xscale = width / 40
yscale = height / 24
# Loading file
mapfile = open("part1map.txt", "r")
# Making the map
xco = 0
yco = 0
lines = mapfile.readlines()
for line in lines:
for typeoftile in range(0, len(line)):
if str(typeoftile) == "g":
screen.blit(grass, (xco*xscale, yco*yscale))
xco = xco + 1
yco = yco + 1
然后我的问题出现了:我的背景保持黑色(0),因此mapmaker功能不会将屏幕传回我的第一个代码文件。
如何编辑屏幕(就像我在mapmaker中那样)然后显示它?
我希望有人可以帮助我 卢卡斯
ps:如果您有任何疑问,请询问。对不起我的英语,我是荷兰人......
答案 0 :(得分:1)
您的问题尚未完全指定 - 例如,我们不知道地图文件的外观。但这部分肯定是错误的:
for line in lines:
for typeoftile in range(0, len(line)):
if str(typeoftile) == "g":
screen.blit(grass, (xco*xscale, yco*yscale))
xco = xco + 1
“typeoftile”将绑定到0和len(line)之间的数字 - 1.这显然永远不会是字符“g”,因此条件永远不会触发。< / p>
看起来你想要的是相应的字符,而不是索引。最简单的解决方法是插入索引操作符,如下所示:
for line in lines:
for index in range(0, len(line)):
typeoftile = line[index]
if str(typeoftile) == "g":
screen.blit(grass, (xco*xscale, yco*yscale))
xco = xco + 1
然而,它更优雅,“Pythonic”迭代字符串中的字符,如下所示:
for line in lines:
for typeoftile in line:
if str(typeoftile) == "g":
screen.blit(grass, (xco*xscale, yco*yscale))
xco = xco + 1
假设“line”是一个字符串,那么“typeoftile”将依次绑定到字符串中的每个字符。 (“abc”在迭代这样的字符时与[“a”,“b”,“c”]的作用相同。)
答案 1 :(得分:0)
但是我仍然有一个问题:只显示了一条水平线的“草地”,所以它看起来像“yco = yco + 1”不起作用,但确实如此(我通过让我的程序测试了这一点)在for-loop中打印yco。
那么原因是什么原因不能显示第二(和第三......)水平草线?
代码现在看起来像这样:
xco = 0
yco = 0
lines = mapfile.readlines()
for line in lines:
for typeoftile in line:
if str(typeoftile) == "g":
screen.blit(grass, (xco*xscale, yco*yscale))
xco = xco + 1
yco = yco + 1
return screen
顺便说一句,输入文件如下所示:
gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg