#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    West Yorkshire, United Kingdom
    Posts
    1
    Rep Power
    0

    Basic Pygame/Python (3.2) problem.


    Hello,

    I'm relatively new to python and I am currently in the process of experimenting with pygame. Here is the code I am having problems with:

    Code:
    import pygame, pygame.display, sys, os
    from pygame.locals import *
    
    pygame.init()
    
    cif = ('''C:\Python_Game\Graphics\Game_Cursor.png''')
    pygame.display.set_caption("Apple")
    
    SWidth = 1280
    SHeight = 1024
    
    screen = pygame.display.set_mode((SWidth, SHeight),0,32)
    
    mouse_c = pygame.image.load(cif).convert_alpha()
    
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
                
    x,y = pygame.mouse.get_pos()
    x -= mouse_c.get_width()/2
    y -= mouse_c.get_height()/2
    
    screen.blit(mouse_c(x,y))
    
    pygame.display.update()
    The program should be self explanatory, it is meant to open a new window (Which is does correctly), change the 'name', if you will, to Apple (Which is also does fine). The problem arises as my image cursor will not replace/appear over pygame's pre-set cursor. There are no errors in the log, and I appreciate it's probably a trivial mistake but any help someone can give would be very nice.

    Thanks in advance.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    You have two options:

    1) Replace the cursor itself
    2) Hide the real cursor and draw an image wherever the mouse pointer is (which is what you have done)

    So if choosing alternative 2, you also have to hide the real mouse cursor:

    Code:
    pygame.mouse.set_visible(False)
    Not sure, but you could try changing
    Code:
    pygame.display.update()
    to

    Code:
    pygame.display.flip()
    http://www.catb.org/esr/faqs/smart-questions.html#code
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    191
    Rep Power
    2
    This isn't related to using pygame.display.flip() or pygame.display.update(). It is simply that some of your code is not in your while loop.

    I replaced your image with a filled surface, but this will at least get you up and running:

    python Code:
    import pygame, sys, os
     
    pygame.init()
    pygame.display.set_caption("Apple")
    SWidth = 1280
    SHeight = 1024
    screen = pygame.display.set_mode((SWidth, SHeight),0,32)
    cif = pygame.Surface((20,20)).convert()
    cif.fill((255,0,255))
     
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
     
        x,y = pygame.mouse.get_pos()
        x -= cif.get_width()/2
        y -= cif.get_height()/2
     
        screen.blit(cif,(x,y))
        pygame.display.update()
    Note you are doing nothing to clear the screen so everything stays once drawn.

    Other than that I STRONGLY suggest you get a strong grasp of functions and classes before continuing. Also do not flood your namespace with that * import in the beginning. That is a horrible habit that is perpetuated in endless amounts of code samples but it is still terrible.

    -Mek
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by Mekire
    This isn't related to using pygame.display.flip() or pygame.display.update(). It is simply that some of your code is not in your while loop.
    Oh wow, I really didn't look at the code long enough. Good catch.
    http://www.catb.org/esr/faqs/smart-questions.html#code
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0

    Thanks guys.


    I'm using a new account because I'm running in to trouble with the other, but anyway. Thanks Mekire and partoj for the help. On a side note I am familiar with functions from Lua and have a foundation understanding of them and classes in python, however I cannot find a sufficent tutorial on applying them on a large scale through multiple scripts.

    I didn't use them here because I didn't see the point calling a function when I just need the mouse cursor to be constantly refreshed on my screen, so I figured a while loop would be more appropriate. (And then I went on to apply it poorly ).

    How would you have applied a function or class in this instance?

    + Can I ask is the way I tried to navigate through my computer the right way to access files? (I'm using windows ofc so I used the \)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    191
    Rep Power
    2
    In terms of paths, regardless of what operating system you use you should always use os.path.join. This promises the greatest hope of cross-compatibility.

    As for using functions and classes, yes it is slightly overkill for this, but at the same time you build a framework you can use again and again.

    Here is a quick example:
    python Code:
    import pygame as pg
    import sys,os
     
    SCREENSIZE = 1000,650
     
    class Control:
        def __init__(self):
            os.environ['SDL_VIDEO_CENTERED'] = '1' #Center window.
            pg.init()
            pg.display.set_caption("Apple")
            self.screen = pg.display.set_mode(SCREENSIZE)
            self.clock = pg.time.Clock()
            self.done = False
            self.cursor = pg.Surface((20,20)).convert()
            self.cursor.fill((255,0,0))
            self.curs_rect = self.cursor.get_rect(center=pg.mouse.get_pos())
        def event_loop(self):
            for event in pg.event.get():
                if event.type==pg.QUIT or (event.type==pg.KEYDOWN and event.key==pg.K_ESCAPE):
                    self.done = True
        def update(self):
            self.screen.fill(-1)
            self.curs_rect.center = pg.mouse.get_pos()
            self.screen.blit(self.cursor,self.curs_rect)
        def main(self):
            while not self.done:
                self.event_loop()
                self.update()
                self.clock.tick(60)
                pg.display.update()
     
    if __name__ == "__main__":
        RunIt = Control()
        RunIt.main()
        pg.quit();sys.exit()

    This is obviously overkill for the given project but it builds a framework that you can modify easily and builds good habits for the future.

    -Mek
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0
    I have another, but similar, problem now .

    The code works fine and loads my image, but unlike the previous code I have it constantly draws the image on the screen like it did with your pink box in the code you provided (Thanks for that, it helped a lot ).

    I have a working version of this on my system that is relatively similar but I can't pick out any details that would make it behave differently.

    Code:
    import pygame, sys
    
    pygame.init()
    pygame.display.set_caption("Apple")
    SWidth = 1280
    SHeight = 1024
    S = pygame.display.set_mode((SWidth, SHeight), 0, 32)
    
    pygame.mouse.set_visible(False)
    cif = ("C:\\Python_Game\\Graphics\\Game_Cursor.png")
    cif = pygame.image.load(cif).convert_alpha()
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
                
        x,y = pygame.mouse.get_pos()
        x -= cif.get_width()/2
        y -= cif.get_height()/2
        
        S.blit(cif,(x,y))
        pygame.display.update()
    Again, any help would be much appreciated. Also any good books/resources for learning python?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    191
    Rep Power
    2
    You need to clear the screen (or at least the pertinent parts) every frame.

    Add this:
    Code:
    S.fill(0)
    before this:
    Code:
    S.blit(cif,(x,y))
    pygame.display.update()
    -Mek
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0
    Again, thanks a lot. I'll pick up on these things as I go along I hope .

IMN logo majestic logo threadwatch logo seochat tools logo