user1182742
user1182742

Reputation: 21

Python Pygame Newbie Code Showing Blank Screen

I execute the following code and just get a blank (black) window.

The window caption shows but I have not yet gotten the images to load (I tried using other images than the ones utilized too). The .py file and the images are in the same directory.

background_image_filename='checkmark.jpg'
mouse_image_filename='digestive_bw.png'
import pygame, sys
from pygame.locals import*
from sys import exit


pygame.init()

screen=pygame.display.set_mode((800,800),0,32)
#pygame.display.set_caption("Hello, Howdy, Mate, and Hi there world!")


background=pygame.image.load(background_image_filename).convert()
mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()

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

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

x,y=pygame.mouse.get_pos()
x-=mouse_cursor.get_width() /2
y=-mouse_cursor.get_height() /2
screen.blit(mouse_cursor,(x,y))

pygame.display.update()

I have installed python 3.2 with pygame 1.9.2. If I can't get this to work I will consider uninstalling those and installing 3.1 + 1.9.1.

Upvotes: 2

Views: 3554

Answers (4)

MrRoss
MrRoss

Reputation: 35

Did you try adding

pygame.display.flip(screen)?

also need to update

pygame.display.update(screen) 

Upvotes: 0

ninMonkey
ninMonkey

Reputation: 7511

tip: You can simplify width / 2 when using Rect. They have other useful dynamic properties too: http://www.pygame.org/docs/ref/rect.html ( width, center, centerx, topleft, etc... )

Code:

mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()
mouse_rect = mouse_cursor.get_rect()

mouse_rect.center = pygame.mouse.get_pos()
screen.blit(mouse_cursor, mouse_rect)
pygame.display.update()

Which was:

mouse_cursor=pygame.image.load(mouse_image_filename).convert_alpha()

x,y=pygame.mouse.get_pos()
x-=mouse_cursor.get_width() /2
y=-mouse_cursor.get_height() /2
screen.blit(mouse_cursor,(x,y))
pygame.display.update()

Upvotes: 0

ThisIsAQuestion
ThisIsAQuestion

Reputation: 1967

Something else you might want to do, you put y=- at one point and x-= right next to it. I dont' think you meant to do that.

Upvotes: 0

pmoleri
pmoleri

Reputation: 4449

You should put the code inside the loop, and use a clock to avoid using all the cpu:

clock = pygame.time.Clock()

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

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

    x,y=pygame.mouse.get_pos()
    x-=mouse_cursor.get_width() /2
    y=-mouse_cursor.get_height() /2
    screen.blit(mouse_cursor,(x,y))

    pygame.display.update()
    clock.tick(30)  # keep 30 fps

Upvotes: 2

Related Questions