为什么我的代码不会在列表之间来回切换?

时间:2019-09-11 02:55:42

标签: python list if-statement methods pygame

我的游戏中有两个列表,我想在我的方法之间切换。

我正在开发一个游戏,该游戏在某个点上如果某个盒子坏了,则会在屏幕上打印一种颜色-这些颜色以特定的顺序显示在列表中。共有3个级别:A,B和C。我已经设置好了,因此级别可以切换到玩家的健康状况达到某一水平。我希望每次新级别开始时切换列表。因此,如果级别A从列表A开始,我希望级别B切换到列表B,级别C切换回列表A。

我不知道为什么我的代码无法正常工作。我有“ if”语句,这取决于播放器的运行状况,但是切换没有发生。

在此代码框中被“碎片”打断。 BoxA和BoxB是列表。我认为将会发生的情况是,当玩家的健康状况低于20时,将使用BoxA。当玩家的健康值超过20时,应使用BoxB。当健康状况超过40岁时,应再次使用BoxA。

import {withRouter} from 'react-router';
.
.
.
updateUser = () =>{
    console.log(this.state.objEdit)
    // this.state.objEdit.id=null
    const objEdit = {...this.state.objEdit, id: null};
    axios.post("http://localhost:3000/data/", objEdit).then(res=>{
        console.log(res)
         this.props.history.push('/Form')
         }).catch((error)=>{
        console.log("Error Occured")
    })
}  
.
.
.
// remove the Link. 
<button 
 type="button" 
 className="btn btn-primary" 
 onClick={this.updateUser}
>
  Submit
</button>
.
.
.
export default withRouter(App)

列表在一个单独的文件中定义,我将其导入到主代码中。这是一个示例列表:

    hits = pg.sprite.groupcollide(self.boxs, self.shards, False, True)
    hit_count = 0

    for hit in hits:
        font1 = pg.font.SysFont('comicsans', 100)
        if BOXA[hit_count] == GRE:
            text = font1.render('green', 1, (GREEN))
            #self.effects_sounds['lose_sound'].play()
        elif BOXA[hit_count] == BLU:
            text = font1.render('blue', 1, (CYAN))
        elif BOXB[hit_count] == YEL:
            text = font1.render('yellow', 1, (YELLOW))
        elif BOXB[hit_count] == RE:
            text = font1.render('red', 1, (RED))
            #self.effects_sounds['jackpot_sound'].play()

        self.screen.blit(text, (250 -(text.get_width()/2),200))
        pg.display.update()
        i = 0
        while i < 100:
            pg.time.delay(10)
            i += 1
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    i = 301
                    pg.quit()
        hit.health -= SHARD_DAMAGE
        if self.player.health < 20:
            self.player.health += 0
            hit.health += BOXA.popleft()
            hit_count += 1
            print('lvl_A')

        if self.player.health > 20 and self.player.health < 40:
            hit.health += BOXB.popleft()
            self.player.health += 0
            print('lvl_B')

        elif self.player.health > 40:
            self.player.health += 0
            hit.health += BOXA.popleft()
            print('lvl_C')

我真的不确定为什么不切换,所以任何见识都会受到赞赏!

1 个答案:

答案 0 :(得分:1)

我不知道我是否理解您的问题,但是我在代码中看到一个问题

您应该设置变量

level = "A"

并进行更改

    if self.player.health < 20:
        level = "A"

    if self.player.health > 20 and self.player.health < 40:
        level = "B"

    elif self.player.health > 40:
        level = "C"

并在显示颜色时使用它

if level in ("A", "C"):
    if BOXA[hit_count] == GRE:
        text = font1.render('green', 1, (GREEN))
    elif BOXA[hit_count] == BLU:
        text = font1.render('blue', 1, (CYAN))
else:
    elif BOXB[hit_count] == YEL:
        text = font1.render('yellow', 1, (YELLOW))
    elif BOXB[hit_count] == RE:
        text = font1.render('red', 1, (RED))

否则,它将始终检查第一个列表BOXA,并且总是查找用于显示文本的颜色,并且从不检查BOXB


或者您应该使用

box = BOXA

并进行更改

if self.player.health < 20:
    box = BOXA

if self.player.health > 20 and self.player.health < 40:
    box = BOXB

elif self.player.health > 40:
    box = BOXA

并使用它

    if box[hit_count] == GRE:
        text = font1.render('green', 1, (GREEN))
    elif box[hit_count] == BLU:
        text = font1.render('blue', 1, (CYAN))
    elif box[hit_count] == YEL:
        text = font1.render('yellow', 1, (YELLOW))
    elif box[hit_count] == RE:
        text = font1.render('red', 1, (RED))

顺便说一句:您可以用颜色创建列表或字典

all_colors = {
     GRE: (GREEN, "green")
     BLU: (CYAN, "blue")
     ...
}

,然后使用此词典而不是if/elif

选择颜色
color, name = all_colors[ box[hit_count] ]
text = font1.render(name, 1, color)