gpt4 book ai didi

python - while 循环执行类,然后停止而不是重新开始?

转载 作者:太空宇宙 更新时间:2023-11-03 17:42:16 26 4
gpt4 key购买 nike

我正在尝试 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()
现在,我的问题是,生成随机数后,代码转到引用的类,启动一个对象,但未放置,并且循环中断,即使它应该继续进行,并且脚本结束而没有放置对象。

无论如何,我的问题是,为什么它会转到引用的类,然后即使 isGen != 0 也退出?看起来它要去类,执行它,忘记放置图像,然后做一个肮脏的(如果这就是所谓的..)退出,因为 GUI 卡住(启动“无响应”状态),但是脚本已“成功”终止。

最佳答案

只需逐行单步执行代码(实际上,您应该简单地使用调试器),它就会变得显而易见。

randrange返回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

bsc4 ,但是mapy768mapy >=640True ,但是mapy <= 128False ,因此 else分支被执行。在此else您执行的分支 break因此,好吧,打破你的 while循环。

<小时/>

另外,你的类(class)没有多大意义,但那是另一个话题了......

关于python - while 循环执行类,然后停止而不是重新开始?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30342023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com