wolfeater017 0 Light Poster

When I finally got the input to work in this code it stopped displaying the txt on the screen. Any suggestions on how to fix this.

import pygame, sys 
from pygame.locals import *
import time
mo = 0

# set up pygame 
pygame.init() 

# set up the window 
windowSurface = pygame.display.set_mode((500, 400), 0, 32) 
pygame.display.set_caption('Test') 

# set up the colors 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
GREEN = (0, 255, 0) 
BLUE = (0, 0, 255) 

# set up fonts 
basicFont = pygame.font.SysFont(None, 48) 
# draw the black background onto the surface 
windowSurface.fill(BLACK)
#Click Here
text2 = basicFont.render ('Test',False,WHITE,BLUE)
text = basicFont.render('Click Here to continue...', False, GREEN, BLACK) 
textRect = text.get_rect() 
textRect.centerx = windowSurface.get_rect().centerx 
textRect.centery = windowSurface.get_rect().centery
text2Rect = text.get_rect()
text2Rect.centerx = windowSurface.get_rect().centerx
text2Rect.centery = windowSurface.get_rect().centery-150

# draw the text onto the surface 
windowSurface.blit(text, textRect) 
windowSurface.blit(text2,text2Rect)


mo == 0
while True: 
    # check for events 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
            pygame.quit() 
            sys.exit() 
        if event.type == KEYDOWN: 
            # change the keyboard variables 
            if event.key == K_LEFT or event.key == ord('a'): 
                print('yes') 
            if event.key == K_UP or event.key == ord('w'): 
                print('yes ')
            if event.key == K_DOWN or event.key == ord('s'): 
                print('yes')
        if event.type == KEYUP: 
            if event.key == K_ESCAPE: 
                pygame.quit() 
                sys.exit()  
            if event.key == K_RIGHT or event.key == ord('d'): 
                print('yes') 
            if event.key == ord('x'): 
                print('correct') 

        if event.type == MOUSEBUTTONUP: 
            print('perfect')
            mo = mo+1
# get a pixel array of the surface 
pixArray = pygame.PixelArray(windowSurface) 
pixArray[480][380] = BLACK 
del pixArray 

# draw the text onto the surface 
windowSurface.blit(text, textRect) 
windowSurface.blit(text2,text2Rect)

# draw the window onto the screen 
pygame.display.update()


# run the game loop 
mo==1
while True: 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
            pygame.quit() 
            sys.exit()

Dani AI

Generated

In your snippet, , the program never actually draws the final frame to the screen because the draw/update steps are placed outside the running loop and some state checks use the equality operator instead of controlling program flow. Pygame requires that you draw (fill, blit) and then call pygame.display.update() or pygame.display.flip() each frame while the main loop is running. Also avoid using two back-to-back infinite loops for different screens; use a single loop and switch a state variable on MOUSEBUTTONUP.

Concrete fixes:

  • Replace the == checks you used for flow control with a boolean or an integer state and test that state inside one main loop.
  • Move screen.fill(...), all blit calls and pygame.display.flip()/update() inside that loop so the window is refreshed every iteration.
  • Create the second text rect from the second Surface (use text2.get_rect()), otherwise positioning may be wrong.
  • Don’t use PixelArray just to set one pixel; prefer surface.set_at((x,y), color) or map the color with surface.map_rgb(color) if you need low-level pixel work.
  • Add a pygame.time.Clock() and call clock.tick(30) (or similar) to cap the frame rate and avoid pegging the CPU.

Minimal pattern to follow (conceptual):

# init pygame, create screen, fonts
running = True
clicked = False
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONUP:
            clicked = True

    screen.fill((0,0,0))
    if not clicked:
        screen.blit(instr_surface, instr_rect)
    else:
        # draw next screen
        pass

    pygame.display.flip()
    clock.tick(30)

pygame.quit()

For reference on updating the display and pixel operations see the pygame docs: pygame.display.update and PixelArray.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.