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

时间:2015-05-20 06:37:06

标签: python class python-2.7 loops pygame

我正在试验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冻结(启动一个&# 39;没有回复#状态),但脚本已成功终止&#39;。

1 个答案:

答案 0 :(得分: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 >=640True,但mapy <= 128False,因此else分支已执行。在此else分支中,您执行break,因此,请中断while循环。

此外,你的课程没有多大意义,但那是另一个主题......