How can I prevent my python game from reiterating and instead carry on? -


i've made simple game using pygame , livewires, sprite has avoid falling mushrooms. number of mushrooms falling @ time meant increase score increases. here mean:

from livewires import games,color import random  games.init(screen_width=633,screen_height=479,fps=50)  class stick_man(games.sprite):  def update(self):   self.x=games.mouse.x   if self.left<0:       self.left=0    if self.right>games.screen.width:       self.right=games.screen.width   self.check_collision()   def check_collision(self):   if self.overlapping_sprites:    self.over_message()   def over_message(self):   b=games.message(value="game over", size=100, color=color.red,x=games.screen.width/2,y=games.screen.height/2,lifetime=250,after_death=games.screen.quit)   games.screen.add(b)   class mushroom(games.sprite):  score=0  start=200  score_required=100  level=1  total_score=0  speed=1  mushroom=games.load_image("mushroom.jpg")  x_position=random.randrange(640)   @staticmethod  def next_level():     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)     mushroom().score_required+=50      mushroom().score-=mushroom.score_required     mushroom().start-=150     mushroom().speed+=5     mushroom().level+=1      if mushroom().start==20:      mushroom().start+=10    def __init__(self):    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()      mushroom.score+=50      mushroom.total_score+=mushroom.score   if mushroom().score==mushroom.score_required:      self.next_level()   def check2(self):      if self.top==mushroom.start:    self.duplicate()   def duplicate(self):       new_mush=mushroom()       games.screen.add(new_mush)  background_image=games.load_image("background.jpg",  transparent=false) games.screen.background=background_image  stickman_image=games.load_image("stickman.png", transparent=true)  stickman=stick_man(image=stickman_image,left=1,bottom=480)  games.screen.add(stickman)  games.mouse.is_visible=false  b=mushroom() c=mushroom() a=mushroom() games.screen.add(b) games.screen.add(a) games.screen.add(c) games.screen.event_brab=true  games.screen.mainloop() 

the code pretty self explanatory , whenever 1 of mushrooms equal start, new object created meaning new mushroom comes in. however, happens code doesn't function second time , mushrooms don't faster spawn faster either. also, when game first starts, minute first mushroom hits bottom says level 1 cleared, when should after 2 mushrooms. sprite red mushroom , stickman can found on g images if want simulate.

so question how make object's stats carry on left off whenever mushroom appears , display message @ right time

your problem in of lines this:

mushroom().score_required+=50 

there number of problems here, add make have no useful effect:

  • mushroom() creates new mushroom instance (which goes away line done).
  • assigning (including update-assigning) attribute through instance creates or updates instance attribute, if there class attribute of same name.
  • the += operator doesn't mutate immutable values integers in-place (because impossible); a += b same a = + b.*

so, when put together, you're doing creating new value equal mushroom.score_required + 50, assigning value new instance attribute of temporary instance (which goes away). has no effect on class attribute, or on of other instances.

you have related, different, problem in lines this:

x_position=random.randrange(640) 

unless want of mushrooms have same x_position, should not class attribute, instance attribute, , you're going run kinds of strange problems.

storing game stats class attributes of random class strange thing do. there ways make work, there's no reason try. class attributes useful constants instances of class might share, they're not useful substitute global variables.

a better design this:

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 

* that's not true. in fact, a = + b equivalent a = a.__add__(b), while a += b equivalent a = a.__iadd__(b) if such method exists, falling __add__ if doesn't. mutable objects lists, makes big difference, because __iadd__ can change self in-place , return it, meaning end assigning same object a there. immutable objects, there's no difference.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -