当循环执行 class 时,然后停止而不是重新开始?
While loop executing class, then stopping rather than starting over?
我正在试验 Pygame,并想出了这个代码:
import os
import sys
import pygame
import time
from random import randrange
from pygame.locals import *
class bedrock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('Bedrock.png', -1)
self.rect.center = (mapx,mapy)
class coalore(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('CoalOre.png', -1)
self.rect.center = (mapx,mapy)
class diamondore(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('DiamondOre.png', -1)
self.rect.center = (mapx,mapy)
class dirtblock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('DirtBlock.png', -1)
self.rect.center = (mapx,mapy)
class goldore(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('GoldOre.png', -1)
self.rect.center = (mapx,mapy)
class grassblock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('GrassBlock.png', -1)
self.rect.center = (mapx,mapy)
class gravelblock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('GravelBlock.png', -1)
self.rect.center = (mapx,mapy)
class ironore(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('IronOre.png', -1)
self.rect.center = (mapx,mapy)
class lavaflow(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('LavaFlow.png', -1)
self.rect.center = (mapx,mapy)
class obsidian(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('ObsidianBlock.png', -1)
self.rect.center = (mapx,mapy)
class stoneblock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('StoneBlock.png', -1)
self.rect.center = (mapx,mapy)
def main():
pygame.init()
DISPLAYSURF = pygame.display.set_mode((1536, 800))
pygame.display.set_caption('Minecrap')
mapx = 1504
mapy = 768
def load_image(file_name, colorkey=None):
print "Loading Image: ",file_name
full_name = os.path.join('images/Tile', file_name)
image = pygame.image.load(full_name)
image = image.convert()
return image, image.get_rect()
minecrap_bedrock, mbrect = load_image("Bedrock.png")
minecrap_coal_ore, corect = load_image("CoalOre.png")
minecrap_diamond_ore, dorect = load_image("DiamondOre.png")
minecrap_dirt, drect = load_image("DirtBlock.png")
minecrap_gold_ore, gorect = load_image("GoldOre.png")
minecrap_grass_block, gbrect = load_image("GrassBlock.png")
minecrap_gravel, grect = load_image("GravelBlock.png")
minecrap_iron_ore, iorect = load_image("IronOre.png")
minecrap_lava, mlrect = load_image("LavaFlow.png")
minecrap_obsidian, orect = load_image("ObsidianBlock.png")
minecrap_stone, srect = load_image("StoneBlock.png")
##
##br_s = pygame.sprite.RenderClear()
##co_s = pygame.sprite.RenderClear()
##do_s = pygame.sprite.RenderClear()
##di_s = pygame.sprite.RenderClear()
##go_s = pygame.sprite.RenderClear()
##grvl_s = pygame.sprite.RenderClear()
##grss_s = pygame.sprite.RenderClear()
##io_s = pygame.sprite.RenderClear()
##lf_s = pygame.sprite.RenderClear()
##ob_s = pygame.sprite.RenderClear()
##sb_s = pygame.sprite.RenderClear()
isGen = 1
while isGen == 1:
mapx = 1504
mapy = 768
print "@loop"
if mapy == 64:
gbsc = randint.randrange(1,3)
if gbsc == 1:
DISPLAYSURF.blit(grassblock,(mapx,mapy))
mapx = mapx - 32
if gbsc == 2:
DISPLAYSURF.blit(grassblock,(mapx,mapy))
mapx = mapx - 33
break
if mapy == 96:
DISPLAYSURF.blit(dirtblock,(mapx,mapy))
mapx = mapx - 32
break
if mapy == 32:
isGen = 0
#else:
bsc = randrange(1,11)
print bsc
if bsc == 1:
if mapy >= 768 and mapy <= 672:
DISPLAYSURF.blit(lavaflow,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 2:
if mapy >=768 and mapy <= 736:
DISPLAYSURF.blit(bedrock,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 3:
if mapy >=768 and mapy <= 762:
DISPLAYSURF.blit(obsidian,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 4:
if mapy >=640 and mapy <= 128:
DISPLAYSURF.blit(stoneblock,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 5:
if mapy >= 416 and mapy <= 96:
DISPLAYSURF.blit(dirtblock,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 6:
if mapy >=352 and mapy <= 128:
DISPLAYSURF.blit(coalore,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 7:
if mapy >=672 and mapy <= 640:
DISPLAYSURF.blit(diamondore,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 8:
if mapy >=224 and mapy <= 512:
DISPLAYSURF.blit(ironore,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 9:
if mapy >=544 and mapy <= 224:
DISPLAYSURF.blit(gravelblock,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 10:
if mapy >=544 and mapy <= 384:
DISPLAYSURF.blit(goldore,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
else:
break
print "broken"
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if __name__ == '__main__': main()
现在,我的问题是,在生成随机数之后,代码转到引用的 class,一个对象被初始化,但没有被放置,即使它应该继续进行,循环也会中断,并且脚本结束时没有放置对象。
无论如何,我的问题是,为什么它会转到引用的 class,然后即使 isGen != 0
也退出?它似乎会进入 class,执行它,忘记放置图像,然后做一个肮脏的(如果这就是所谓的......)退出,因为 GUI 冻结(开始 'Not Responding' 状态), 但脚本已终止 'successfully'.
只需逐行检查您的代码(真的,您应该简单地使用调试器),它变得显而易见。
说 randrange
returns 4
因此 bsc
现在是 4
。现在达到以下代码:
if bsc == 4:
if mapy >=640 and mapy <= 128:
DISPLAYSURF.blit(stoneblock,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
bsc
是 4
,但 mapy
是 768
。 mapy >=640
是 True
,但 mapy <= 128
是 False
,因此执行 else
分支。在这个 else
分支中,您执行 break
,因此,好吧,打破您的 while
循环。
此外,您的 类 没有多大意义,但那是另一个话题...
我正在试验 Pygame,并想出了这个代码:
import os
import sys
import pygame
import time
from random import randrange
from pygame.locals import *
class bedrock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('Bedrock.png', -1)
self.rect.center = (mapx,mapy)
class coalore(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('CoalOre.png', -1)
self.rect.center = (mapx,mapy)
class diamondore(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('DiamondOre.png', -1)
self.rect.center = (mapx,mapy)
class dirtblock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('DirtBlock.png', -1)
self.rect.center = (mapx,mapy)
class goldore(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('GoldOre.png', -1)
self.rect.center = (mapx,mapy)
class grassblock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('GrassBlock.png', -1)
self.rect.center = (mapx,mapy)
class gravelblock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('GravelBlock.png', -1)
self.rect.center = (mapx,mapy)
class ironore(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('IronOre.png', -1)
self.rect.center = (mapx,mapy)
class lavaflow(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('LavaFlow.png', -1)
self.rect.center = (mapx,mapy)
class obsidian(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('ObsidianBlock.png', -1)
self.rect.center = (mapx,mapy)
class stoneblock(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('StoneBlock.png', -1)
self.rect.center = (mapx,mapy)
def main():
pygame.init()
DISPLAYSURF = pygame.display.set_mode((1536, 800))
pygame.display.set_caption('Minecrap')
mapx = 1504
mapy = 768
def load_image(file_name, colorkey=None):
print "Loading Image: ",file_name
full_name = os.path.join('images/Tile', file_name)
image = pygame.image.load(full_name)
image = image.convert()
return image, image.get_rect()
minecrap_bedrock, mbrect = load_image("Bedrock.png")
minecrap_coal_ore, corect = load_image("CoalOre.png")
minecrap_diamond_ore, dorect = load_image("DiamondOre.png")
minecrap_dirt, drect = load_image("DirtBlock.png")
minecrap_gold_ore, gorect = load_image("GoldOre.png")
minecrap_grass_block, gbrect = load_image("GrassBlock.png")
minecrap_gravel, grect = load_image("GravelBlock.png")
minecrap_iron_ore, iorect = load_image("IronOre.png")
minecrap_lava, mlrect = load_image("LavaFlow.png")
minecrap_obsidian, orect = load_image("ObsidianBlock.png")
minecrap_stone, srect = load_image("StoneBlock.png")
##
##br_s = pygame.sprite.RenderClear()
##co_s = pygame.sprite.RenderClear()
##do_s = pygame.sprite.RenderClear()
##di_s = pygame.sprite.RenderClear()
##go_s = pygame.sprite.RenderClear()
##grvl_s = pygame.sprite.RenderClear()
##grss_s = pygame.sprite.RenderClear()
##io_s = pygame.sprite.RenderClear()
##lf_s = pygame.sprite.RenderClear()
##ob_s = pygame.sprite.RenderClear()
##sb_s = pygame.sprite.RenderClear()
isGen = 1
while isGen == 1:
mapx = 1504
mapy = 768
print "@loop"
if mapy == 64:
gbsc = randint.randrange(1,3)
if gbsc == 1:
DISPLAYSURF.blit(grassblock,(mapx,mapy))
mapx = mapx - 32
if gbsc == 2:
DISPLAYSURF.blit(grassblock,(mapx,mapy))
mapx = mapx - 33
break
if mapy == 96:
DISPLAYSURF.blit(dirtblock,(mapx,mapy))
mapx = mapx - 32
break
if mapy == 32:
isGen = 0
#else:
bsc = randrange(1,11)
print bsc
if bsc == 1:
if mapy >= 768 and mapy <= 672:
DISPLAYSURF.blit(lavaflow,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 2:
if mapy >=768 and mapy <= 736:
DISPLAYSURF.blit(bedrock,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 3:
if mapy >=768 and mapy <= 762:
DISPLAYSURF.blit(obsidian,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 4:
if mapy >=640 and mapy <= 128:
DISPLAYSURF.blit(stoneblock,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 5:
if mapy >= 416 and mapy <= 96:
DISPLAYSURF.blit(dirtblock,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 6:
if mapy >=352 and mapy <= 128:
DISPLAYSURF.blit(coalore,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 7:
if mapy >=672 and mapy <= 640:
DISPLAYSURF.blit(diamondore,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 8:
if mapy >=224 and mapy <= 512:
DISPLAYSURF.blit(ironore,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 9:
if mapy >=544 and mapy <= 224:
DISPLAYSURF.blit(gravelblock,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
if bsc == 10:
if mapy >=544 and mapy <= 384:
DISPLAYSURF.blit(goldore,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
else:
break
print "broken"
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if __name__ == '__main__': main()
现在,我的问题是,在生成随机数之后,代码转到引用的 class,一个对象被初始化,但没有被放置,即使它应该继续进行,循环也会中断,并且脚本结束时没有放置对象。
无论如何,我的问题是,为什么它会转到引用的 class,然后即使 isGen != 0
也退出?它似乎会进入 class,执行它,忘记放置图像,然后做一个肮脏的(如果这就是所谓的......)退出,因为 GUI 冻结(开始 'Not Responding' 状态), 但脚本已终止 'successfully'.
只需逐行检查您的代码(真的,您应该简单地使用调试器),它变得显而易见。
说 randrange
returns 4
因此 bsc
现在是 4
。现在达到以下代码:
if bsc == 4:
if mapy >=640 and mapy <= 128:
DISPLAYSURF.blit(stoneblock,(mapx,mapy))
mapx = mapx - 32
if mapx <= -32:
mapx = 1504
mapy = mapy - 32
else:
break
bsc
是 4
,但 mapy
是 768
。 mapy >=640
是 True
,但 mapy <= 128
是 False
,因此执行 else
分支。在这个 else
分支中,您执行 break
,因此,好吧,打破您的 while
循环。
此外,您的 类 没有多大意义,但那是另一个话题...