【问题标题】:While loop executing class, then stopping rather than starting over?循环执行类,然后停止而不是重新开始?
【发布时间】:2015-05-20 06:37:06
【问题描述】:

我正在尝试使用 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 冻结(开始一个“无响应”状态),但是脚本被“成功”终止。

【问题讨论】:

    标签: python class python-2.7 loops pygame


    【解决方案1】:

    只需逐行检查您的代码(实际上,您应该简单地使用调试器),它就会变得很明显。

    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 &gt;=640True,但 mapy &lt;= 128False,因此会执行 else 分支。在这个else 分支中,你执行break,因此,打破你的while 循环。


    另外,你的课程没有多大意义,但这是另一个话题......

    【讨论】:

    • 谢谢!该回复修复了 While 循环问题,这很有趣,因为我正在从头开始重建此脚本,但 break 在上次构建中工作得非常好..(现在,要弄清楚为什么它不能正确地进行 blitting.. ..)
    • 另外,关于课程......我的问题的根源。我应该问另一个关于课程的问题还是你能帮助我吗?谢谢!
    • 通常最好问一个新问题。在您的情况下,您创建了很多类,它们仅在您加载的图像上有所不同。例如,您可以轻松地创建一个类并将图像作为参数传递给它的初始化程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多