Why is my Pygame window black (Added Detail)

Why is my Pygame window black (Added Detail)

Postby merlinthecat » Sat Nov 09, 2013 7:03 pm

Ok, this is the code:


Code: Select all
import pygame
from pygame import *
pygame.init()
name=input("Hello, What is your name?")
#set the window size
window = pygame.display.set_mode((800,600) ,0,32)
pygame.display.set_caption("Fifa 14")
fillcolour=(255,255,255)
window.fill(fillcolour
#pygame.display.update()
#create text strings for screen info
text1 = "      Hello,Welcome To Fifa 14"
text2 = "        To Continue, Sign Into EA"
text3= " MOUSE DOWN POSITION"
text4= ""
mouse_x=0
mouse_y=0
#endless loop for main programme           
running=True
while running:
   window.fill(fillcolour)
   for event in pygame.event.get():
      pos = pygame.mouse.get_pos()
   if event.type == MOUSEBUTTONDOWN:
      text3=str(pos)
   elif event.type == MOUSEMOTION:
      mouse_x = pos[0]
      mouse_y = pos[1]
      text4="mouse x position "+str(mouse_x)     
  #load graphics for game
  myfont = pygame.font.SysFont("Britannic Bold", 55)
  label1 = myfont.render(text1+name, 1, (255,0,0))
  label2 = myfont.render(text2, 1, (255,0,0))
  label3 = myfont.render(text3, 1, (255,0,0))
  label4 = myfont.render(text4, 1, (0,0,255))
  backG = pygame.image.load('fifa.jpg')
#update screen
window.blit(backG,(0,0))
window.blit(label1, (0,20))
window.blit(label2, (500,550))
window.blit(label3, (0,20))
window.blit(label4, (500,550))                         
pygame.display.update()


I am trying to get my game screen up with a background and for the co ordinates of the mouse to also show up. However when I run it I enter the input (name) and the pygame screen is just black.
However if I take the loop:
Code: Select all
running=True
while running:
   window.fill(fillcolour)
   for event in pygame.event.get():
      pos = pygame.mouse.get_pos()
   if event.type == MOUSEBUTTONDOWN:
      text3=str(pos)
   elif event.type == MOUSEMOTION:
      mouse_x = pos[0]
      mouse_y = pos[1]
      text4="mouse x position "+str(mouse_x)     


out of the code the screen comes up with text on and the background that I want and the text that would show the co ordinates, but not working propely because it is missing the required code.
Anyone Know why this may be?
Last edited by Yoriz on Sat Nov 09, 2013 8:03 pm, edited 1 time in total.
Reason: Added code tags
merlinthecat
 
Posts: 4
Joined: Wed Nov 06, 2013 10:52 pm

Re: Why is my Pygame window black (Added Detail)

Postby DrakeMagi » Sat Nov 09, 2013 7:25 pm

1. PLEASE USE CODE TAGS !!!!!
Example :
Code: Select all
[code]your code[/code]

or
hilight code and hit Code button above.

just edit and add code tags.
don't create new thread.
DrakeMagi
 
Posts: 72
Joined: Sun May 12, 2013 8:36 pm

Re: Why is my Pygame window black (Added Detail)

Postby DrakeMagi » Thu Nov 14, 2013 12:20 am

Because your graphics and pygame.display.update() is not in loop. (indentation error)

Tip: also load image outside of main loop.
Tip: try to keep font render out of main loop.
Example:
Code: Select all
#load graphics for game
myfont = pygame.font.SysFont("Britannic Bold", 55)
label1 = myfont.render(text1+name, 1, (255,0,0))
label2 = myfont.render(text2, 1, (255,0,0))
backG = pygame.image.load('fifa.jpg')

running=True
while running:
    window.fill(fillcolour)
    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()
    if event.type == MOUSEBUTTONDOWN:
        text3=str(pos)
        label3 = myfont.render(text3, 1, (255,0,0))
    elif event.type == MOUSEMOTION:
        mouse_x = pos[0]
        mouse_y = pos[1]
        text4="mouse x position "+str(mouse_x)
        label4 = myfont.render(text4, 1, (0,0,255))

    #update screen
    window.blit(backG,(0,0))
    window.blit(label1, (0,20))
    window.blit(label2, (500,550))
    if label3:
        window.blit(label3, (0,20))
    if label4:
        window.blit(label4, (500,550))                         
    pygame.display.update()
DrakeMagi
 
Posts: 72
Joined: Sun May 12, 2013 8:36 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests