Reputation: 13
I have tried to implement the snake game tutorial from the link but the screen closes instantly after running the .py file. I have looked up for the screen instantly closing error and tried fixing it by adding a run block but now the screen just becomes black whenever I try to draw a rectangle.
import os
os.environ['SDL_AUDIODRIVER'] = 'dsp'
import pygame
import sys
import random
import subprocess
import pygame
pygame.init()
display_width = 500
display_height = 500
display = pygame.display.set_mode((display_width,display_height))
window_color= (200,200,200)
red = (255,0,0)
black = (0,0,0)
apple_image = pygame.image.load('apple.jpg')
snake_head = [250,250]
pygame.display.set_caption("Snake AI")
snake_position = [[250,250],[240,250],[230,250]]
apple_position = [random.randrange(1,50)*10,random.randrange(1,50)*10]
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run =False
if event.type == pygame.KEYDOWN:
command = "python sample.py"
subprocess.call(command)
def display_snake(snake_position):
for position in snake_position:
pygame.draw.rect(display,red,pygame.Rect(position[0],position[1],10,10))
def display_apple(display,apple_position, apple):
display.blit(apple,(apple_position[0], apple_position[1]))
pygame.display.update()
pygame.quit()
Upvotes: 1
Views: 574
Reputation: 2779
You have your definitions of display_apple()
and display_snake()
where the calls to them should be.
Take the definitions of the display_apple()
and display_snake()
functions out of the while loop and move them up top somewhere. Where they used to be defined, call them instead. You will see your snake and apple show up on the screen.
def display_snake(snake_position):
for position in snake_position:
pygame.draw.rect(display,red,pygame.Rect(position[0],position[1],10,10))
def display_apple(display,apple_position, apple):
display.blit(apple,(apple_position[0], apple_position[1]))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run =False
if event.type == pygame.KEYDOWN:
command = "python sample.py"
subprocess.call(command)
display_apple(display, apple_position, apple_image)
display_snake(snake_position)
pygame.display.update()
I have put the definitions just above the while
loop so you can see them, but I would normally define them higher up, before the pygame.init()
call would be a good place.
You generally want your function definitions and class definitions (if you have any, but you do not at the moment) to be separated from the executed code.
In fact I prefer to put the main body of the executing code inside a function as well, usually that function gets called main()
, and then just call that function. That means there is no real executing code at the top level, other than the call of the function main()
. Doing that does require you to be careful about global variables though and would cause you some problems here.
Upvotes: 1
Reputation: 286
You need to place your pygame.quit() command under the check events 'for' loop. Because what its currently doing is running through your program and once it completes the main loop once it quits:
while True: # you don't need a flag here, unless
# you have an activation button of
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
command = "python sample.py"
subprocess.call(command)
Also in general a could practice would be to refactor all your above code into fuctions or complete them in different modules and import them as objects into a so called main.py
file (which is this one, contains main game loop).
Upvotes: 1