Running a if statement only once in pygame

前端 未结 2 2040
孤街浪徒
孤街浪徒 2020-12-06 23:53

I have a running game where you can move and collect coins. For each coin I have a if statement:

if cn1+25 < x and cn1 + 50 > x:
        cn1 = -1000
           


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 00:28

    It is also possible to use a tuple to run an if statement only once. by creating the tuple switch = [0] out side of the while loop then this peace of code can suffice.

    if Your conditions:
        if switch[0] == 0:
            switch[0] += 1
    

    all that you need from here is something that runs (or doesn't) because switch[0] = 1. By having it only add up to 1 you could add a similar peace of code to undo.

提交回复
热议问题