user3256142
user3256142

Reputation: 19

Python Blitting not working?

I'm writing a game in python and pygame and i trying to create a main menu, but my label wont show up????, also im getting this error: line 22, in x,y = pygame.mouse.get_pos() error: video system not initialized???? i don't understand what is happening here because i am very new to python heres my code:

bif="menubg.jpg"
load_check_x = "null"
load_check_y = "null"
import pygame, sys
from pygame.locals import *
x = 0
y = 0


pygame.init()
screen=pygame.display.set_mode((640,360),0,32)
background=pygame.image.load(bif).convert()
pygame.display.set_caption("Some random Title")
pygame.display.flip()
#screen.fill((0,0,0))

while True:
    evc = pygame.event.get()
    for event in evc:
        if event.type == pygame.QUIT:
            pygame.quit()       
x,y = pygame.mouse.get_pos()
#Setup mouse pos!!!
if x >= 340 and x <= 465:
    load_check_x = "True"
if x < 340 or x > 465:
    load_check_x = "False"

if y >= 425 and y <= 445:
    load_check_y = "True"
if y < 425 or y > 445:
    load_check_y = "False"

if load_check_x == "True" and load_check_y == "True":
   for event in evc:
       if event.type ==pygame.MOUSEBUTTONUP:
           clear()
labelfont = pygame.font.SysFont("Comic Sans MS", 12)
new_text = labelfont.render('Play!!!!', 1, (255, 255, 255))
screen.blit(new_text, (340, 425))

Someone help me!!!

Upvotes: 0

Views: 644

Answers (1)

Bartlomiej Lewandowski
Bartlomiej Lewandowski

Reputation: 11190

It seems that your indentation is wrong. How does the python interpreter know when an if statement body ends? It looks at the amount of tabs or spaces to see if the code after belongs to the body, or not.

If you look at your code, you will see that you have some code that does the initialization, then a while loop with another loop that processes your events. After that you have some code that checks the mouse etc.

As you can see, all of the code after the event loop, does not belong to the while loop, and it should. Apart from this, you can replace all your if conditions by creating a new rect and checking if the point you have clicked lies inside the rectangle.

my_rect = pygame.Rect((340,425),(125,20))
while True:
    evc = pygame.event.get()
    for event in evc:
        if event.type == pygame.QUIT:
            pygame.quit()       
    x,y = pygame.mouse.get_pos()
    #Setup mouse pos!!!
    if my_rect.collidepoint(x,y):
       for event in evc:
           if event.type ==pygame.MOUSEBUTTONUP:
               clear()
    labelfont = pygame.font.SysFont("Comic Sans MS", 12)
    new_text = labelfont.render('Play!!!!', 1, (255, 255, 255))
    screen.blit(new_text, (340, 425))

Upvotes: 0

Related Questions