save value in the variable during while loop

save value in the variable during while loop

Postby kostik » Thu Jul 18, 2013 11:32 am

So iam trying to make game in which space rocket is launching missile and trying to hit asteroids.I am fighting with one problem which i have in almost every game...iam trying to save value in variable during while loop....because its in the while loop the variable is still changing...i want to save it and if i want to i could again change it ....is this possible?? Thanks for any advice....Iam doing it different way but the my cods gets hard to read and long
Code: Select all
class Missile(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("strela0.xcf").convert()
        self.rect = self.image.get_rect()
    def movement(self,rocketx,rockety):
        self.rect.x = rocket_x
        self.rect.y = rocket_y +20 #position of missile is on the top down of space rocket
    def launch(self):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                self.rect.x = self.rect.x + 20# when I hit space the missile launchs
                # this is the problem when I move with my space rocket up also lauched missile moves up because everything is in the while loop
                # self.rect.y equals rocket_y + 20 so when rocket moves missile moves
        # i can do something like this
        if event.type == pygame.KEYDOWN:
            if self.missile_not_launch == True:
                self.variable_y = self.rect.y
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    self.missile_not_launch = False
                    self.rect.y = self.variable.y
                    self.rect.x = self.rect.x + 10
        # but its pretty complicated,This problem i have almost in every game, it makes my code long
                   
kostik
 
Posts: 13
Joined: Tue Apr 16, 2013 10:26 am

Re: save value in the variable during while loop

Postby Mekire » Thu Jul 18, 2013 3:21 pm

What you want is clearly possible. If you could post a working example demonstrating this behavior I will see what I can do. Everything is going to be inside your while loop of your game. This isn't the issue. I believe the issue is more organization of your classes. The Missile class and rocket Class should have a method telling it how to update every frame. Your rocket should have a method that creates (fires) Missile instances. Your game loop should call the update methods for both of these.

-Mek
User avatar
Mekire
 
Posts: 815
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: save value in the variable during while loop

Postby kostik » Thu Jul 18, 2013 4:36 pm

my code has also meny animation and drawings ....i made this code simple easy to read

Code: Select all
class Missile(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("strela0.xcf").convert()
        self.rect = self.image.get_rect()
    def movement(self,rocketx,rockety):
        self.rect.x = rocket_x
        self.rect.y = rocket_y +20
    def launch(self):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                self.rect.x = self.rect.x + 20
       
missile1 = Missile()
sprite_group = pygame.sprite.Group()
sprite_group.add(missile1)
class Space_rocket(pygame.sprite.Sprite):
    changey = 0
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("space_rocket").convert()
        self.rect = self.image.get_rect()
       
    def movement(self):
        #movement
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                self.changey = -5
            if event.key == pygame.K_DOWN:
                self.change = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                self.zmenay = 0
            if event.key == pygame.K_DOWN:
                self.zmenay = 0
            #real movement
        self.rect.y = self.rect.y + self.zmenay
space_rocket = Space_rocket()
sprite_group.add(space_rocket)
done = True
while done == True:
   
    space_rocket.movement()
    missile1.movement(space_rocket.x,space_rocket.y)
    missile1.launch()

    screen.fill(black)
    sprite_group.draw(screen)
    pygame.display.flip()
kostik
 
Posts: 13
Joined: Tue Apr 16, 2013 10:26 am

Re: save value in the variable during while loop

Postby Mekire » Fri Jul 19, 2013 1:36 am

Well, I could be mistaken, but I believe you want something like this:
Code: Select all
import sys,os
import pygame


DIRECT_DICT = {pygame.K_UP    : ( 0,-1),
               pygame.K_DOWN  : ( 0, 1),
               pygame.K_LEFT  : (-1, 0),
               pygame.K_RIGHT : ( 1, 0)}


class Missile(pygame.sprite.Sprite):
    def __init__(self,start):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10,10)).convert()
        self.image.fill((0,255,0))
        self.rect = self.image.get_rect(center=start)
        self.speed = 7

    def update(self,surface):
        self.rect.x += self.speed
        if not self.rect.colliderect(surface.get_rect()):
            self.kill()

    def draw(self,surface):
        surface.blit(self.image,self.rect)


class SpaceRocket(pygame.sprite.Sprite):
    def __init__(self,start):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50)).convert()
        self.image.fill((255,0,0))
        self.rect = self.image.get_rect(center=start)
        self.speed = 5

    def update(self,surface):
        self.movement()
        self.rect.clamp_ip(surface.get_rect())

    def draw(self,surface):
        surface.blit(self.image,self.rect)

    def movement(self):
        keys = pygame.key.get_pressed()
        for key in DIRECT_DICT:
            if keys[key]:
                self.rect.x += DIRECT_DICT[key][0]*self.speed
                self.rect.y += DIRECT_DICT[key][1]*self.speed

    def fire_missile(self,objects):
        objects.add(Missile((self.rect.centerx+25,self.rect.centery)))


class Control(object):
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((500,500))
        self.clock = pygame.time.Clock()
        self.fps = 60
        self.done = False
        self.objects = pygame.sprite.Group()
        self.rocket = SpaceRocket((25,250))
        self.objects.add(self.rocket)

    def event_loop(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.done = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    self.rocket.fire_missile(self.objects)

    def update(self):
        self.objects.update(self.screen)
        self.screen.fill(0)
        self.objects.draw(self.screen)

    def game_loop(self):
        while not self.done:
            self.event_loop()
            self.update()
            pygame.display.update()
            self.clock.tick(self.fps)


if __name__ == "__main__":
    os.environ["SDL_VIDEO_CENTERED"] = '1'
    run_it = Control()
    run_it.game_loop()
    pygame.quit();sys.exit()

Note how both sprites have their own update and draw functions. Also note that we don't actually create instances of Missile until they are fired (and the function to fire them is in the SpaceRocket class where it belongs.

-Mek

Edit:
Slight simplification after confirming that you can safely call kill without changing the group size mid-iteration with sprite.Group.update().
User avatar
Mekire
 
Posts: 815
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest