Python Forum
Why doesn't my code display?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why doesn't my code display?
#1
Hello, this is my first attempt at coding. I tried to run it but it wouldn't display?

#!/usr/bin/python

import pygame
from pygame.locals import *
import time
import sys
import os
from os import path

# colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
AQUA = (0, 255, 255)
FUSCHIA = (255,   0, 255)
GRAY = (128, 128, 128)
LIME = (0, 255, 0)
MAROON = (128,  0,   0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128,   0)
PURPLE = (128,  0, 128)
SILVER = (192, 192, 192)
TEAL = (0, 128, 128)


# main game starts here
def main():
    # Initialize screen and window
    pygame.init()
    screen = pygame.display.set_mode((720, 480))
    pygame.display.set_caption('Times Tables Fun')

    # Fill background
    while intro:
        screen.fill(white)
        background = background.convert()
        background.fill((250, 250, 250))
    # intro
    font = pygame.font.Font(None, 36)
    message_to_screen("Welcome to Times Tables fun!",
                      TEAL,
                      -100,
                      "large")
    message_to_screen(
        "The objective of the game is to practice your times tables.",
        OLIVE,
        -30)
    message_to_screen(
        "Try to get as many questions correct as you can!",
        OLIVE,
        -10)
    pygame.display.update()
    clock.tick(15)
    time.sleep(2)

    font = pygame.font.Font(None, 18)
    text = font.render(
        "The goal is to practice your times tables."
        "Try to answer as many questions as correctly as you can!",
        1, (10, 10, 10))
    textpos = text.get_rect()
    textpos.centerx = background.get_rect().centerx
    background.blit(text, textpos)

    # Blit everything to the screen
    screen.blit(background, (0, 0))
    pygame.display.flip()

    # Event loop

    running = True
    while running:
        # keep loop running at the right speed
        clock.tick(FPS)
        # Process input (events)
        for event in pygame.event.get():
            # check for closing window
            if event.type == pygame.QUIT:
                running = False

        if event.type == pygame.KEYDOWN:
            # Figure out if it was an arrow key. If so
            # adjust speed.
            if event.key == pygame.K_LEFT:
                x_speed = -3
            if event.key == pygame.K_RIGHT:
                x_speed = 3
            if event.key == pygame.K_UP:
                y_speed = -3
            if event.key == pygame.K_DOWN:
                y_speed = 3
    # End!!!
    pygame.display.flip()

    # Limit fps
    clock.tick(60)

# close window
pygame.quit()
sys.exit()
Reply
#2
you never call the 'main()' function.

You'll want to make the last lines of your code:

if __name__ == "__main__":
    main()
That should do it, provided the rest of your code is correct.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
(Apr-15-2018, 01:27 PM)sparkz_alot Wrote: you never call the 'main()' function. You'll want to make the last lines of your code:
 if __name__ == "__main__": main() 
That should do it, provided the rest of your code is correct.
Thank you so much!
Reply
#4
This is unrelated to your problem, but I saw you wrote out all the colors your self and I feel bad so here's a little something I made for you and anyone else to use. Thank me later. (btw use it by doing something like "bgColor = color.light_green")
import random

class colour:
  #Common colours

  #green
  light_green = (0, 252, 0)
  dark_green = (0, 58, 18)
  green = (5, 191, 24)

  #blue
  light_blue = (2, 162, 255)
  dark_blue = (0, 46, 73)
  blue = (5, 123, 191)

  #red
  light_red = (249, 2, 23)
  dark_red = (104, 1, 9)
  red = (201, 4, 4)
  brown = (60, 50, 0)
  orange = (255, 150, 0)

  #pink
  light_pink = (245, 107, 255)
  dark_pink = (85, 1, 91)
  pink = (182, 25, 193)
  purple = (85, 1, 91)

  #yellow
  yellow = (225, 225, 0)

  #Less Common Colours

  turquoise = (0, 255, 178)
  white = (255, 255, 255)
  black = (0, 0, 0)
  light_grey = (200, 200, 200)
  silver = (150, 150, 150)
  fog = (20, 20, 20)
  dim_grey = (50, 50, 50)
  mist = (120, 120, 120)
  sky = (209, 226, 255)
  dark_grey = (127, 127, 127)
  cyan = (81, 210, 239)
  magenta = (188, 5, 136)
  sarcoline = (155, 163, 160)
  liver = (76, 76, 76)
  lilac = (175, 140, 175)
  amber = (226, 168, 31)
  maroon = (109, 30, 26)
  

  #random colour generator

  randomCol = (0, 0, 0)

  col1 = random.randint(0, 255)
  col2 = random.randint(0, 255)
  col3 = random.randint(0, 255)
  randomCol = (col1,col2,col3)
Reply
#5
(Apr-22-2018, 11:45 PM)CharlieGallie Wrote: This is unrelated to your problem, but I saw you wrote out all the colors your self and I feel bad so here's a little something I made for you and anyone else to use. Thank me later. (btw use it by doing something like "bgColor = color.light_green")
 import random class colour: #Common colours #green light_green = (0, 252, 0) dark_green = (0, 58, 18) green = (5, 191, 24) #blue light_blue = (2, 162, 255) dark_blue = (0, 46, 73) blue = (5, 123, 191) #red light_red = (249, 2, 23) dark_red = (104, 1, 9) red = (201, 4, 4) brown = (60, 50, 0) orange = (255, 150, 0) #pink light_pink = (245, 107, 255) dark_pink = (85, 1, 91) pink = (182, 25, 193) purple = (85, 1, 91) #yellow yellow = (225, 225, 0) #Less Common Colours turquoise = (0, 255, 178) white = (255, 255, 255) black = (0, 0, 0) light_grey = (200, 200, 200) silver = (150, 150, 150) fog = (20, 20, 20) dim_grey = (50, 50, 50) mist = (120, 120, 120) sky = (209, 226, 255) dark_grey = (127, 127, 127) cyan = (81, 210, 239) magenta = (188, 5, 136) sarcoline = (155, 163, 160) liver = (76, 76, 76) lilac = (175, 140, 175) amber = (226, 168, 31) maroon = (109, 30, 26) #random colour generator randomCol = (0, 0, 0) col1 = random.randint(0, 255) col2 = random.randint(0, 255) col3 = random.randint(0, 255) randomCol = (col1,col2,col3) 
Thank you so much!!! I really appreciate it!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020