Black Screen

+2 Samuel Abusamra · February 17, 2015
every time i run this it gives me a black screen i dont know why? any advice?
Everytime i try to run this it gives me a black screen. When i move the window to the edges of my screen the black goes away and the game regains functionality. Why does the black screen happen?


import pygame
import time
import random

pygame.init() #start game

#define colors to use them, the higher the brighter

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
yellow = (200,0,0)

display_width = 800
display_hieght = 600

gameDisplay = pygame.display.set_mode((display_width,display_hieght)) #set width, hieght #hardcoding
pygame.display.set_caption('Crusader') #set title

img = pygame.image.load('Snakehead2.png') #insert image
##img2 = pygame.image.load('Apple2.jpg')

clock = pygame.time.Clock() #"import" FPS

block_size = 20 #thickness of snake
FPS = 15 #placement matters

direction = "right"

def game_intro():
    intro = True

    while intro:

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_n:
                    intro = False #bypasses start screen to game
                if event.key == pygame.K_q:
                    pygame.quit()
                    quit()
                    
        gameDisplay.fill(white)
        message_to_screen("Welcome to Crusader",
                          green,
                          -20
                          )
        message_to_screen("Try your best to eat the apple by using the arrow keys on your keyboard.",
                          red,
                          10
                          )
        message_to_screen("Note the snake will automatically movie after the start screen",
                          red,
                          40
                          )
        message_to_screen("Press N to start or Q to quit",
                          red,
                          80
                          )

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

font = pygame.font.SysFont(None, 25)

def snake(block_size, snakelist):

    if direction == "right":
        head = pygame.transform.rotate(img, 270)
    if direction == "left":
        head = pygame.transform.rotate(img, 90)
    if direction == "up":
        head = img
    if direction == "down":
        head = pygame.transform.rotate(img, 180)
    
    gameDisplay.blit(head, (snakelist[-1][0], snakelist[-1][1])) #exact position/location based on pixel of screen
    
    for XnY in snakelist[:-1]: #snake to grow
        pygame.draw.rect(gameDisplay, green, [XnY[0], XnY[1], block_size, block_size]) #size of rectanlge + location snake

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


def message_to_screen(msg,color, y_displace=0): #displacement from center
    textSurf, textRect = text_objects(msg, color)
##    screen_text = font.render(msg, True, color)
##    gameDisplay.blit(screen_text, [display_width/2.5,display_hieght/2]) #message location on screen
    textRect.center = (display_width / 2), (display_hieght / 2) +y_displace
    gameDisplay.blit(textSurf, textRect)
def gameLoop():

    global direction #global variable for direction
    gameExit = False
    gameOver = False

    lead_x = display_width/2
    lead_y = display_hieght/2
    
    lead_x_change = 15
    lead_y_change = 0

    snakeList = []
    snakeLength = 1 #the starting length
    
    randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0 #rounding for location
    randAppleY = round(random.randrange(0, display_hieght-block_size)/10.0)*10.0
    
    while not gameExit:

        while gameOver == True:
            gameDisplay.fill(black) #set new background color
            message_to_screen("New Game N Quit Q", red) #create message
            pygame.display.update()

            for event in pygame.event.get():
                   if event.type == pygame.KEYDOWN: 
                       if event.key == pygame.K_q: #choose which key you want for quit
                           gameExit = True
                           gameOver = False
                       if event.key == pygame.K_n: #choose which key you want for continue 
                           gameLoop()
        
        for event in pygame.event.get(): #controls via keyboard
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    direction = "left"
                    lead_x_change = -block_size
                    lead_y_change = 0
                if event.key == pygame.K_RIGHT:
                    direction = "right"
                    lead_x_change = block_size
                    lead_y_change = 0    
                if event.key == pygame.K_UP:
                    direction = "up"
                    lead_y_change = -block_size
                    lead_x_change = 0
                if event.key == pygame.K_DOWN:
                    direction = "down"
                    lead_y_change = block_size
                    lead_x_change = 0

    #GameOver, set boundries for up, down, left, right
        if lead_x >= display_width or lead_x < 0 or lead_y >= display_hieght or lead_y < 0:
            gameOver = True

        lead_x += lead_x_change
        lead_y += lead_y_change
        
        gameDisplay.fill(white) #background color

        AppleThickness = 30 #pixel of apple

        
        
        pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, AppleThickness, AppleThickness]) #creates apple

        #defining snake growth
        
        snakeHead = []
        snakeHead.append(lead_x)
        snakeHead.append(lead_y)
        snakeList.append(snakeHead)
        snake(block_size, snakeList) #define snake actions

        if len(snakeList) > snakeLength:
            del snakeList[0]

        for eachSegment in snakeList[:-1]: #running into yourself is gameover
            if eachSegment == snakeHead:
                gameOver = True
        
        pygame.display.update()

##        if lead_x >= randAppleX and lead_x <= randAppleX + AppleThickness: #allows for snake to enter into any part of x&y boundary of apple 
##            if lead_y >= randAppleY and lead_y <= randAppleY + AppleThickness:
##                randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
##                randAppleY = round(random.randrange(0, display_hieght-block_size)/10.0)*10.0
##                snakeLength += 1

        if lead_x > randAppleX and lead_x < randAppleX + AppleThickness or lead_x + block_size > randAppleX and lead_x + block_size < randAppleX + AppleThickness:
            if lead_y > randAppleY and lead_y < randAppleY + AppleThickness:
                
                randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
                randAppleY = round(random.randrange(0, display_hieght-block_size)/10.0)*10.0
                snakeLength += 1

            elif lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
                    
                randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
                randAppleY = round(random.randrange(0, display_hieght-block_size)/10.0)*10.0
                snakeLength += 1

        
        clock.tick(FPS) #frames per second
        
    pygame.quit()
    quit()
    
game_intro()    
gameLoop()

#sprite
#lead = snake
#randApple = Apple
#blocksize= width of snake


Post a Reply

Replies

Oldest  Newest  Rating
0 Nishant Kumar Thakur · February 22, 2015
Hi Samuel,

It is very difficult to figure out the issue if the indentation is messed up in a python program. Could you please use the 'code' button to get a code field and submit the code there, like this
def simplefunc():
print "I am a simple function"
simplefunc()
  • 1

Python

107,249 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator