2
votes

Here is my code, please keep in mind that I picked up python a couple of days ago so my code may be incorrectly made, ect. I am trying to make a window that will display some text (beta) and will display two small rectangles that I want to make into buttons.

import sys
import pygame
from pygame.locals import *
pygame.init()

size = width, height = 720, 480
speed = [2, 2]
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
red = (255,0,0)

screen = pygame.display.set_mode(size)
screen.fill((blue))

pygame.display.set_caption("BETA::00.0.1")

clock = pygame.time.Clock()

def game_intro():

  intro = True

  while intro:
     for event in pygame.event.get():
         print(event)
         if event.type == pygame.QUIT:
             pygame.quit()
             quit()

    gameDisplay.fill(blue)
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects("BETA", largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.draw.rect(gameDisplay, green,(150,450,100,50))
    pygame.draw.rect(gameDisplay, red,(550,450,100,50))

    pygame.display.flip(screen)
    pygame.display.update(screen)
    clock.tick(15)
1

1 Answers

1
votes

Problems:

  1. Indentation error
  2. Your game_intro() function is defined, but never called
  3. You wrote gameDisplay instead of screen 4 times, also display_width, display_height instead of width, height
  4. In game_intro(), code after your infinite loop is never called
  5. pygame.display.update(screen) does not work, it should be pygame.display.update() to update the whole screen. flip(screen) also needs to be changed to flip()
  6. The function text_objects() is not defined but I found the definition in the example you probably copied the code from :)

Corrected code:

import sys
import pygame
from pygame.locals import *
pygame.init()

size = width, height = 720, 480
speed = [2, 2]
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
red = (255,0,0)

screen = pygame.display.set_mode(size)

pygame.display.set_caption("BETA::00.0.1")

clock = pygame.time.Clock()


def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def game_intro():

  screen.fill(blue)

  largeText = pygame.font.Font('freesansbold.ttf',115)
  TextSurf, TextRect = text_objects("BETA", largeText)
  TextRect.center = ((width/2),(height/2))
  screen.blit(TextSurf, TextRect)

  pygame.draw.rect(screen, green,(150,450,100,50))
  pygame.draw.rect(screen, red,(550,450,100,50))

  pygame.display.flip()
  pygame.display.update()
  clock.tick(15)

  intro = True

  while intro:
     for event in pygame.event.get():
         print(event)
         if event.type == pygame.QUIT:
             pygame.quit()
             quit()


game_intro()