Pygame troubleshoot

Pygame troubleshoot

Postby python_lover » Mon Aug 19, 2013 9:31 pm

Hello. I am new to Python and Pygame and I have run into a problem with Pygame.

Currently, I have downloaded python 2.7 and Pygame version 1.9.1 that is associated with 2.7. I ran the 'print pygame.ver' and I know that pygame has downloaded properly, however, when I wrote two different codes, I noticed two problems. One, when I close the Pygame window, it freezes and crashes. This isn't a smooth closing of the program. Second, There is one code that I typed, and when I executed it, a pygame window appears and is only black. Then it freezes, stops responding, and crashes without executing what the code says it is supposed to do.

CAN SOMEONE PLEASE HELP ME. Is there a problem with my code or the pygame program or compatibility or what? Please help.

Here is the code for the one that appears with a black window and crashes:


Code: Select all
bif = 'bg.jpg'
mif = 'ball.png'

import pygame, sys
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((640, 360), 0, 32)

background = pygame.image.load(bif) .convert()
mouse_c = pygame.image.load(mif),convert_alpha()

while True:
    for event in pygame.even.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        screen.blit (background, (0,0))

        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()
Last edited by micseydel on Mon Aug 19, 2013 9:52 pm, edited 1 time in total.
Reason: Added code tags, locked OP.
python_lover
 
Posts: 2
Joined: Mon Aug 19, 2013 9:22 pm

Re: Pygame troubleshoot

Postby DrakeMagi » Mon Aug 19, 2013 11:23 pm

stop/freezing it probably because your cpu is maxing out.
here a sleep option.
Code: Select all
# set before game loop
clock = pygame.time.Clock()

Code: Select all
# put in game loop. I always put it after pygame.display.flip() or pygame.display.update()
clock.tick(60) # limit frames to 60


If you don't see image. You probably set depth to high for video card.
depth set to 0 an pygame will pick the best depth for video card.
Try.
Code: Select all
screen = pygame.display.set_mode((640, 360))


also you need to fix this. change comma to dot.
Code: Select all
mouse_c = pygame.image.load(mif),convert_alpha()

and fix this. remove the space before .convert
Code: Select all
background = pygame.image.load(bif) .convert()
DrakeMagi
 
Posts: 72
Joined: Sun May 12, 2013 8:36 pm

Re: Pygame troubleshoot

Postby python_lover » Tue Aug 20, 2013 2:54 pm

Thanks. It has helped me a lot. But I am curious, why do we have to use the two code for a sleep option:

Code: Select all
clock = pygame.time.Clock()

Code: Select all
clock.tick(60)


Can you tell me what is the function of them and why they must be used? My program can't seem to work without them.
python_lover
 
Posts: 2
Joined: Mon Aug 19, 2013 9:22 pm

Re: Pygame troubleshoot

Postby Mekire » Tue Aug 20, 2013 3:44 pm

The first one creates an instance of pygame.time.Clock(). The most common use for this class (and the use it is providing here) is to make the game run no faster than a given framerate.

In the line:
Code: Select all
clock.tick(60)
the clock is telling the program to wait, so that it never runs faster than 60 frames per second (which is fairly industry standard). With out this line your program will just run as fast as your computers processor can make it. You can still run your program without a clock but it isn't generally a good idea and is almost never done.

-Mek
User avatar
Mekire
 
Posts: 828
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