Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is in all of the lines that look like this:</p> <pre><code>Mushroom().score_required+=50 </code></pre> <p>There are a number of problems here, which all together add up to make this have no useful effect:</p> <ul> <li>Mushroom() creates a new <code>Mushroom</code> instance (which goes away as soon as this line is done).</li> <li>Assigning (including update-assigning) to an attribute through an instance always creates or updates an instance attribute, even if there was a class attribute of the same name.</li> <li>The += operator doesn't mutate immutable values like integers in-place (because that would be impossible); <code>a += b</code> is effectively the same as <code>a = a + b</code>.*</li> </ul> <p>So, when you put that together, what you're doing is creating a new value equal to <code>Mushroom.score_required + 50</code>, then assigning that value to a new instance attribute of a temporary instance (which immediately goes away). This has no effect on the class attribute, or on any of the other instances.</p> <p>You have a related, but different, problem in the lines like this:</p> <pre><code>x_position=random.randrange(640) </code></pre> <p>Unless you want all of the mushrooms to have the same <code>x_position</code>, this should not be a class attribute, but an instance attribute, and you're going to run into all kinds of strange problems.</p> <p>Storing game stats as class attributes of a random class is a strange thing to do. There are ways you could make that work, but there's no good reason to even try. Class attributes are useful for constants that all instances of the class might as well share, but they're not useful as a substitute for global variables.</p> <p>A better design would be something like this:</p> <pre><code>class Game(object): def __init__(self): self.score = 0 self.start = 200 self.score_required = 100 self.level = 1 self.total_score = 0 def next_level(self): indicate = 'Level ', + Mushroom.level, ' cleared' message = games.Message(value=indicate, size=50, color=color.red, x=games.screen.width/2, y=games.screen.height/2, lifetime=150) games.screen.add(message) self.score_required += 50 self.score -= self.score_required self.start -= 150 self.speed += 5 self.level += 1 if self.start == 20: self.start += 10 def update_score(self, n): game.score += n game.total_score += game.score if self.score == self.score_required: self.next_level() class Mushroom(games.Sprite): mushroom=games.load_image("mushroom.jpg") def __init__(self, game): self.x_position=random.randrange(640) self.game = game super(Mushroom,self).__init__(image=Mushroom.mushroom,x=games.mouse.x,y=0) def update(self): self.dy=Mushroom.speed self.check() self.check2() def check(self): if self.bottom == games.screen.height: self.destroy() game.update_score(50) def check2(self): if self.top == Mushroom.start: self.duplicate() def duplicate(self): games.screen.add(Mushroom(self.game)) game = Game() games.screen.add(Mushroom(game)) games.screen.add(Mushroom(game)) games.screen.add(Mushroom(game)) games.screen.event_brab=True </code></pre> <hr> <p>* That's not completely true. In fact, <code>a = a + b</code> is equivalent to <code>a = a.__add__(b)</code>, while <code>a += b</code> is equivalent to <code>a = a.__iadd__(b)</code> if such a method exists, falling back to <code>__add__</code> only if it doesn't. For mutable objects like lists, this makes a big difference, because <code>__iadd__</code> can change <code>self</code> in-place and then return it, meaning you end up assigning the same object back to <code>a</code> that was already there. But for immutable objects, there's no difference.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload