Mekire's Profile User Rating: -----

Reputation: 116 Stalwart
Group:
Active Members
Active Posts:
214 (0.31 per day)
Joined:
11-January 13
Profile Views:
15,182
Last Active:
User is offline Jul 02 2014 11:33 PM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
0
Icon   Mekire has not set their status

Posts I've Made

  1. In Topic: Structure of a game - pygame

    Posted 2 Jul 2014

    To cleanly implement a game with multiple scenes you will need some sort of scene manager. This can be implemented as a FSM or PDA, but knowing about that isn't really critical at this point.

    Here is a fairly bare bones scene manager example:
    https://github.com/M...late-with-movie
    Be aware that this is probably quite a jump from your current level.

    As for your current code, I still think you need to really get yourself used to how rects work. Play around with them in the interactive interpreter until you are really comfortable with them. Understanding them really is key to working with pygame. Also I'm not sure why you are defining your classes inside another function. Typically the only time you would do this would be if the function was a class factory, and in your case it is not. Also, you should work on getting your main loop out of the global namespace.

    -Mek
  2. In Topic: Sprites collision

    Posted 30 Jun 2014

    Quote

    Without x and y how can I specify position of the sprite?

    The rect is the position of your sprite.
    >>> import pygame as pg
    >>> a = pg.Rect(0, 0, 50, 50)
    >>> a
    <rect(0, 0, 50, 50)>
    >>> a.topleft
    (0, 0)
    >>> a.x += 10
    >>> a
    <rect(10, 0, 50, 50)>
    >>> a.y += 15
    >>> a
    <rect(10, 15, 50, 50)>
    >>> a.topleft = (25, 25)
    >>> a
    <rect(25, 25, 50, 50)>
    

    The reason it is only working in the top corner is because your rects have the coordinates (0,0), because as I said, you never move it.

    -Mek
  3. In Topic: Sprites collision

    Posted 29 Jun 2014

    Forget about self.x and self.y for now; get rid of them; remove them from your code.

    Use the rect. The sprite collision methods use the self.rect attribute of your sprite.

    But. You. Never. Move. The. Rect.


    Please post your full program (or at least a testable sample), and I will try to help you with the specifics of the collision.

    -Mek
  4. In Topic: Moving Sprites

    Posted 28 Jun 2014

    I think you are missing what the rect is for here.

    The rect is where your sprite is.

    This line:
    screen.blit(self.image, self.rect)
    
    Draws the sprite to the screen at the position self.rect.topleft.
    You are changing the variable self.x and there is no relationship between self.x and self.rect.

    Try changing self.rect.x instead.

    -Mek
  5. In Topic: Move_ip of Sprites - Pygame

    Posted 25 Jun 2014

    The main thing that can be simplified here is your user input. For continuos key presses it is generally better to get the keyboard state using pygame.key.get_pressed() than to detect KEYUP and KEYDOWN events.


    The following shows this method of movement, as well as restricting the player to the screen with pygame.Rect.clamp_ip():
    import sys
    import pygame as pg
    
    
    SCREEN_SIZE = (600, 400)
    BACKGROUND_COLOR = pg.Color("darkgreen")
    
    DIRECT_DICT = {pg.K_LEFT  : (-1, 0),
                   pg.K_RIGHT : ( 1, 0),
                   pg.K_UP    : ( 0,-1),
                   pg.K_DOWN  : ( 0, 1)}
    
    
    class BlockSprite(pg.sprite.Sprite):
        def __init__(self, pos, color, *groups):
            super(BlockSprite, self).__init__(*groups)
            self.image = pg.Surface((50,50)).convert()
            self.image.fill(color)
            self.rect = self.image.get_rect(topleft=pos)
    
        def draw(self, surface):
            surface.blit(self.image, self.rect)
    
        def update(self, *args, **kwargs):
            pass
    
    
    class Player(BlockSprite):
        def __init__(self, pos, *groups):
            super(Player, self).__init__(pos, pg.Color("red"), *groups)
            self.speed = 5
    
        def update(self, keys, screen_rect):
            for key in DIRECT_DICT:
                if keys[key]:
                    self.rect.x += self.speed*DIRECT_DICT[key][0]
                    self.rect.y += self.speed*DIRECT_DICT[key][1]
            self.rect.clamp_ip(screen_rect)
    
    
    class Control(object):
        def __init__(self):
            pg.init()
            pg.display.set_caption("Game") 
            self.screen = pg.display.set_mode(SCREEN_SIZE)
            self.screen_rect = self.screen.get_rect()
            self.clock = pg.time.Clock()
            self.fps = 60
            self.done = False
            self.keys = pg.key.get_pressed()
            self.all_sprites = pg.sprite.Group()
            self.player = Player((250,250), self.all_sprites)
            self.bone = BlockSprite((25,25), pg.Color("white"), self.all_sprites)
    
        def event_loop(self):
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    self.done = True
    
        def update(self):
            keys = pg.key.get_pressed()
            self.all_sprites.update(keys, self.screen_rect)
            
        def draw(self):
            self.screen.fill(BACKGROUND_COLOR)
            self.all_sprites.draw(self.screen)
    
        def main_loop(self):
            while not self.done:
                self.event_loop()
                self.update()
                self.draw()
                pg.display.update()
                self.clock.tick(self.fps)
            pg.quit()
            sys.exit()
                    
    
    if __name__ == "__main__":
        Control().main_loop()
    
    


    I have a sample repo here:
    https://github.com/M...-pygame-samples
    Where you will find some similar examples (with a little documentation thrown in). Specifically appropriate for you are the 8-direction movement and 4-direction movement examples.

    Let me know if you have any specific questions.
    -Mek

My Information

Member Title:
D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:
Location:
Amakusa, Japan

Contact Information

E-mail:
Private

Comments

Page 1 of 1
  1. Photo

    RetroGamer Icon

    13 Dec 2013 - 10:48
    Thanks man for the help awesome work!
Page 1 of 1