Member Avatar for Urbandude23

I am trying to run my game in Python, with Pygame. However, I am presented with a black screen, but my game shows up for a fraction of a second after I exit the Shell. What's wrong with my code, I think it's an issue with my timer.

# Import library
import pygame, sys, random, math
import os
import time
# Initialise game engine
def draw_miner(screen,x,y):
    pygame.draw.ellipse(screen,black,[35+x,35+y,25,25])
def draw_gold(screen,x,y):
    pygame.draw.ellipse(screen,black,[40+x,40+y, randint(20,370), randint(20,370)])




pygame.init()
# Examples
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
# Screen dimensions
size=[700,500]
screen=pygame.display.set_mode(size)

pygame.display.set_caption("Lewis Tough - Find The Gold")
pi=3.141592653

done=False
clock=pygame.time.Clock()
x_coord=10
y_coord=10

x_speed=0
y_speed=0
font=pygame.font.Font("C:/Windows/Fonts/TCM_____.ttf", 50)
while done==False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True

        if event.type == pygame.KEYDOWN:
            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

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                x_speed=0
            if event.key == pygame.K_RIGHT:
                x_speed=0
            if event.key == pygame.K_UP:
                y_speed=0
            if event.key == pygame.K_DOWN:
                y_speed=0

#all evt processing above here

#start of game logic
    x_coord += x_speed
    y_coord += y_speed
#end of game logic

#start of drawing
    screen.fill(white)
    draw_miner(screen,x_coord,y_coord)

    pygame.draw.rect(screen,green,[20,20,50,50],2)
    pygame.draw.rect(screen,green,[70,70,50,50],2)
    pygame.draw.rect(screen,green,[20,70,50,50],2)
    pygame.draw.rect(screen,green,[70,20,50,50],2)
    pygame.draw.rect(screen,green,[120,20,50,50],2)
    pygame.draw.rect(screen,green,[170,70,50,50],2)
    pygame.draw.rect(screen,green,[120,70,50,50],2)
    pygame.draw.rect(screen,green,[170,20,50,50],2)
#breakafter8
    pygame.draw.rect(screen,green,[120,120,50,50],2)
    pygame.draw.rect(screen,green,[170,170,50,50],2)
    pygame.draw.rect(screen,green,[120,170,50,50],2)
    pygame.draw.rect(screen,green,[170,120,50,50],2)
    pygame.draw.rect(screen,green,[20,120,50,50],2)
    pygame.draw.rect(screen,green,[70,170,50,50],2)
    pygame.draw.rect(screen,green,[20,170,50,50],2)
    pygame.draw.rect(screen,green,[70,120,50,50],2)
#sec3
    pygame.draw.rect(screen,green,[220,20,50,50],2)
    pygame.draw.rect(screen,green,[270,70,50,50],2)
    pygame.draw.rect(screen,green,[220,70,50,50],2)
    pygame.draw.rect(screen,green,[270,20,50,50],2)
    pygame.draw.rect(screen,green,[320,20,50,50],2)
    pygame.draw.rect(screen,green,[370,70,50,50],2)
    pygame.draw.rect(screen,green,[320,70,50,50],2)
    pygame.draw.rect(screen,green,[370,20,50,50],2)
#sec4
    pygame.draw.rect(screen,green,[320,120,50,50],2)
    pygame.draw.rect(screen,green,[370,170,50,50],2)
    pygame.draw.rect(screen,green,[320,170,50,50],2)
    pygame.draw.rect(screen,green,[370,120,50,50],2)
    pygame.draw.rect(screen,green,[220,120,50,50],2)
    pygame.draw.rect(screen,green,[270,170,50,50],2)
    pygame.draw.rect(screen,green,[220,170,50,50],2)
    pygame.draw.rect(screen,green,[270,120,50,50],2)
#next half
    pygame.draw.rect(screen,green,[20,220,50,50],2)
    pygame.draw.rect(screen,green,[70,270,50,50],2)
    pygame.draw.rect(screen,green,[20,270,50,50],2)
    pygame.draw.rect(screen,green,[70,220,50,50],2)
    pygame.draw.rect(screen,green,[120,220,50,50],2)
    pygame.draw.rect(screen,green,[170,270,50,50],2)
    pygame.draw.rect(screen,green,[120,270,50,50],2)
    pygame.draw.rect(screen,green,[170,220,50,50],2)
#breakafter8
    pygame.draw.rect(screen,green,[120,320,50,50],2)
    pygame.draw.rect(screen,green,[170,370,50,50],2)
    pygame.draw.rect(screen,green,[120,370,50,50],2)
    pygame.draw.rect(screen,green,[170,320,50,50],2)
    pygame.draw.rect(screen,green,[20,320,50,50],2)
    pygame.draw.rect(screen,green,[70,370,50,50],2)
    pygame.draw.rect(screen,green,[20,370,50,50],2)
    pygame.draw.rect(screen,green,[70,320,50,50],2)
#sec3
    pygame.draw.rect(screen,green,[220,220,50,50],2)
    pygame.draw.rect(screen,green,[270,270,50,50],2)
    pygame.draw.rect(screen,green,[220,270,50,50],2)
    pygame.draw.rect(screen,green,[270,220,50,50],2)
    pygame.draw.rect(screen,green,[320,220,50,50],2)
    pygame.draw.rect(screen,green,[370,270,50,50],2)
    pygame.draw.rect(screen,green,[320,270,50,50],2)
    pygame.draw.rect(screen,green,[370,220,50,50],2)
#sec4
    pygame.draw.rect(screen,green,[320,320,50,50],2)
    pygame.draw.rect(screen,green,[370,370,50,50],2)
    pygame.draw.rect(screen,green,[320,370,50,50],2)
    pygame.draw.rect(screen,green,[370,320,50,50],2)
    pygame.draw.rect(screen,green,[220,320,50,50],2)
    pygame.draw.rect(screen,green,[270,370,50,50],2)
    pygame.draw.rect(screen,green,[220,370,50,50],2)
    pygame.draw.rect(screen,green,[270,320,50,50],2)

    draw_miner(screen,x_coord,y_coord)

#start of text

    text=font.render("Welcome to Find the Gold!", True, black)
    screen.blit(text, [100,425])
s=0
frame_count = 0
frame_rate = 60
start_time = 0

#def main():
 #   global lives
  #  time = "Time=" +str(s)

total_seconds = frame_count//frame_rate
minutes = total_seconds//60
seconds = total_seconds%60
output_string = "Time:{0:02}:{1:02}".format(minutes, seconds)
text=font.render(output_string,True,black)
screen.blit(text,[450, 100])


#while s<=60:
 #   os.system('cls')
  #  texta = font.render(time,'Seconds', True, black)
   # screen.blit(texta, [500,100])
    #time.sleep(1)
    #s+=1
    #if s==60:
       # m+=1
        #s=0

#pygame.display.update()    

pygame.display.flip()


#end of drawing


clock.tick(20)

pygame.quit()

Apologies for the inefficiency of the rectangle drawing, I'm still new to Python!

Recommended Answers

All 2 Replies

You have an event loop up at the top (while done==False:) that runs until the user has quit. Because of the indention, your event loop runs until completion and only then draws what you wanted it to. Your indention signifies a 'code block'.Everything in that code block will run until the code block is exited. Right now only a portion of your code is inside of the event loop. The rest of it (the important part) is outside of the code block, and doesn't run until the event loop completes. By indenting all of your logic and draw-events to the same level as the while block I was able to make your game run.

Example:

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            print('User has exited!')
            done = True

    print('We are inside of the event loop, press CTRL + C!')

print('We are outside of the event loop.')
print('Now is not a good time to start drawing stuff.')

Even i had this problem at first , but all u gotta do is type all the loop thing at the end of your program !
just try this once bc it did worked for me :)

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.