Python quits after I launch the application [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm pretty new to programming. I have probably made some stupid mistake, but I can't find it. Any help is appreciated. Here is the code:
import pygame
import time
import random
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Moving square')
#####################Set the display parameters and title#####################
gameExit = False
gameOver = False
lead_x = display_width/2
lead_y = display_height/2
lead_x_change = 0
lead_y_change = 0
FPS = 15
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 25)
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
block_size = 10
###########################Setting the variables############################
def message_2_screen(msg, color):#setting a variable for the text
themessage = font.render(msg, True, color)
gameDisplay.blit(themessage, [265, display_height/2])
def gameloop():
gameExit = False
gameOver = False
lead_x = display_width/2
lead_y = display_height/2
lead_x_change = 10
lead_y_change = 0
FPS = 15
block_size = 10
while not gameExit:
while gameOver == True:
gameDisplay.fill(white)
message_2_screen('Press c to continue and q to quit', red)
pygame.display.update()
for event in pygame.event.get():#creating a loop for the quitting and continue
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameloop()
for event in pygame.event.get():#loop for quitting
if event.type is pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:#setting the control buttons
if event.key == pygame.K_LEFT:
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -block_size
lead_x_change = 0
elif key.event == pygame.K_DOWN:
lead_y_change = block_size
lead_x_change = 0
if lead_x >= display_width or lead_y >=display_height or lead_x < 0 or lead_y < 0:#Setting limits for where the square can move
gameExit = True
lead_x+= lead_x_change
lead_y+= lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size ])#setting the position of the square
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
gameloop()
I checked the problem a couple of times and focused mostly on the quit loops, but still nothing.

Remove one indent from your quit() call. This will ensure it is called only after the loop exits.
Best of luck, and happy coding!

Related

Pygame function error bug

I can't seem to the error in this code, the game simply quits right after launching it. If anybody could read through my code and answer me what I'm doing wrong, and I'd be very grateful. I've already read through all of it and couldn't figure it out. I don't know why the game quits right after launching it. (Meaning I can't even move my block).
import pygame
import time
pygame.init()
width = 800
height = 600
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("First snake game.")
clock = pygame.time.Clock()
block_size = 1
FPS = 100
def gameloop():
gameExit = False
lead_x = width/2
lead_y = height/2
lead_x_change = 0
lead_y_change = 0
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
lead_y_change = -block_size
lead_x_change = 0
if event.key == pygame.K_s:
lead_y_change = block_size
lead_x_change = 0
if event.key == pygame.K_d:
lead_x_change = block_size
lead_y_change = 0
if event.key == pygame.K_a:
lead_x_change = -block_size
lead_y_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
lead_x_change = 0
if event.key == pygame.K_w or event.key == pygame.K_s:
lead_y_change = 0
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])
pygame.display.update()
time.sleep(2)
pygame.quit()
quit()
clock.tick(FPS)
gameloop()
`
Not sure if I'm understanding the code correctly or not, but at the end of the gameloop() function you have pygame.quit() and quit() so comment that out and see what happens.

random module's randrange not working in pygame

I am trying to make a game in pygame and want to add apples at random places but the random module is not working. I tried looking online but the guy there was able to use this without any problem
My code and The output is down below
impoort pygame
imporrt random
pygame.init()
display_width = 1000
display_height = 500
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("SlikiSnake")
clock = pygame.time.Clock()
FPS = 15
block_size = 10
def GameLoop():
lead_x = display_width/2
lead_y = display_height/2
lead_x_change =0
lead_y_change =0
randAppleX = random.randint(0, display_width - block_size)
randAppleY = random.randint(0 ,display_height - block_size)
pygame.display.update()
gameExit = False
gameOver = False
while not gameExit:
while gameOver == True:
gameDisplay.fill(white)
message_on_screen("Game Over,press r to start again or Q to quit", black)
pygame.display.update()
for event in pygame.event.get():
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_r:
GameLoop()
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if lead_x >= display_width or lead_x < 0 or lead_y >=display_height or lead_y < 0:
gameOver = True
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(random)
pygame.draw.rect(gameDisplay,red,[randAppleX,randAppleY,block-size,block_size])
pygame.draw.rect(gameDisplay,blue,[lead_x,lead_y,block_size,block_size])
clock.tick(FPS)
pygame.display.flip()
pygame.quit()
quit()
GameLoop()
`
but the error is:
Next time write your code and errors directly in your question body, not as a screenshot.
You must have assigned random to a tuple somewhere between import random to the lines you posted.

Python Snake Game Boundries not working

I'm new to python and I'm trying following along with a tutorial that uses PyGame to create a snake like game. For some reason my boundaries are not working. It may be something simple but I can't see any reason why it wouldn't work. I don't get any errors, the snake just goes past the boundaries and the game doesn't end.
import pygame
import time
import random
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Slither')
clock = pygame.time.Clock()
block_size = 10
FPS = 30
font = pygame.font.SysFont(None, 25)
def message_to_screen(msg,color):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [display_width/2, display_height/2])
def gameLoop():
gameExit = False
gameOver = False
lead_x = display_width/2
lead_y = display_height/2
lead_x_change = 0
lead_y_change = 0
randAppleX = random.randrange (0, display_width-block_size)
randAppleY = random.randrange (0, display_height-block_size)
while not gameExit:
while gameOver == True:
gameDisplay.fill(white)
message_to_screen("Game over, press C to play again or Q to quit", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -block_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -block_size
lead_x_change = 0
elif event.key == pygame.K_DOWN:
lead_y_change = block_size
lead_X_change = 0
**if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
gameOver == True #boundaries**
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, block_size, block_size])
pygame.draw.rect(gameDisplay, black, [lead_x , lead_y, block_size, block_size])
pygame.display.update()
clock.tick(FPS)
message_to_screen("You Lose", red)
pygame.display.update()
time.sleep(2)
pygame.quit()
quit()
gameLoop()
In your exit condition, you're using the equality comparison, not the assignment operator:
if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
gameOver == True #boundaries
in the above,
gameOver == True
should be
gameOver = True

Pygame Help Trying to add apples in Snakegame

import pygame
import random
import time
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('slither')
white = (255, 255, 255)
black = (0,0,0)
red = (252,25,25)
blue = (20,20,250)
purple = (90,33,146)
movement_size = 10
block_size = 20
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 25)
def message_to_screen(msg,colour):
screen_text = font.render(msg, True, colour)
gameDisplay.blit(screen_text, [display_width/2, display_height/2])
def gameLoop():
gameExit = False
gameOver = False
lead_x = display_width/2.0
lead_y = display_height/2.0
lead_x_change = 0
lead_y_change = 0
randAppleX = random.randrange(0, display_width-block_size)
randAppleY = random.randrange(display_height-block_size, 0)
while not gameExit:
while gameOver == True:
gameDisplay.fill(purple)
message_to_screen("Game Over, Press 'C' to play again or 'Q' to Quit", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change -= movement_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change += movement_size
lead_y_change= 0
elif event.key == pygame.K_UP:
lead_y_change -= movement_size
lead_x_change= 0
elif event.key == pygame.K_DOWN:
lead_y_change += movement_size
lead_x_change= 0
if lead_x >= 782 or lead_x < 0 or lead_y >= 582 or lead_y < 0:
gameOver = True
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, block_size, block_size])
pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])
pygame.display.update()
clock.tick(11)
pygame.quit()
quit()
gameLoop()
I was trying to add 'apples' in my snake game. However I was troubled in doing so. Any help debugging code would be very much appreciated! I'm getting some error on lines; 94,42 and 218 something has gone horribly wrong with the randrange stuff.
http://i63.tinypic.com/1h4ggj.png <----- To See
the error happens in
randAppleY = random.randrange(display_height-block_size, 0)
As the error stack trace indicates that randrange function got parameters start=580, stop=0. From inspecting the source file for this function shows that the default value for step is 1.
This is causing the problem as 0 is less than 580 and connot be reached by adding 1 (step parameter) any number of times.
What you can do to fix this is either pass step parameter as -1
randAppleY = random.randrange(display_height-block_size, 0, -1)
Or pass the smaller value as start and larger value as stop parameter
randAppleY = random.randrange(0, display_height-block_size)
import pygame
import random
import time
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('slither')
white = (255, 255, 255)
black = (0,0,0)
red = (252,25,25)
blue = (20,20,250)
purple = (90,33,146)
movement_size = 10
block_size = 20
clock = pygame.time.Clock()
gameExit = gameOver = False
font = pygame.font.SysFont(None, 25)
def message_to_screen(msg,colour):
screen_text = font.render(msg, True, colour)
gameDisplay.blit(screen_text, [display_width/2, display_height/2])
def gameLoop():
gameExit = False
gameOver = False
lead_x = display_width/2.0
lead_y = display_height/2.0
lead_x_change = 0
lead_y_change = 0
randAppleX = random.randint(0, display_width-block_size)
randAppleY = random.randint(0,display_height-block_size)
while not gameExit:
while gameOver:
gameDisplay.fill(purple)
message_to_screen("Game Over, Press 'C' to play again or 'Q' to Quit", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change -= movement_size
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change += movement_size
lead_y_change= 0
elif event.key == pygame.K_UP:
lead_y_change -= movement_size
lead_x_change= 0
elif event.key == pygame.K_DOWN:
lead_y_change += movement_size
lead_x_change= 0
if lead_x >= 782 or lead_x < 0 or lead_y >= 582 or lead_y < 0:
gameOver = True
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, block_size, block_size])
pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])
pygame.display.update()
clock.tick(11)
pygame.quit()
quit()
gameLoop()

Pygame won't print text in certain circumstances

So I'm trying to print "you lose" to the screen when the user moves a box off the playable screen, however this doesn't seem to work unless I call it from outside my main while loop. I have defined a function to handle the creation of the text, the rendering and 'blit'ing of it, although this has no effect when it is called from inside the while loop however it does when it is called from outside it at the bottom. I have checked and the function is executed from both locations, though it only seems to work from one.
import pygame
import time
pygame.init()
red = (255,0,0)
black = (0,0,0)
white = (255,255,255)
gamewidth = 900
gameheight = 900
snakecolour = black
gameDisplay = pygame.display.set_mode((gamewidth,gameheight))
pygame.display.set_caption("Snake")
gameExit = False
boxDimensions = 10
lead_x = (gamewidth // 2) - ((gamewidth // 2) % 20)
lead_y = (gameheight // 2) - ((gameheight // 2) % 20)
lead_x_change = 0
lead_y_change = 0
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 25)
def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [gamewidth//2, gameheight//2])
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if not (lead_x_change == boxDimensions):
lead_x_change = -boxDimensions
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
if not (lead_x_change == -boxDimensions):
lead_x_change = boxDimensions
lead_y_change = 0
elif event.key == pygame.K_UP:
if not (lead_y_change == boxDimensions):
lead_y_change = -boxDimensions
lead_x_change = 0
elif event.key == pygame.K_DOWN:
if not (lead_y_change == -boxDimensions):
lead_y_change = boxDimensions
lead_x_change = 0
lead_x += lead_x_change
lead_y += lead_y_change
if lead_x > gamewidth or lead_x < 0 or lead_y > gameheight or lead_y < 0:
snakecolour = red
gameExit = True
message_to_screen("You Lose!", red)
pygame.display.update()
#message_to_screen("You Lose!", red) WONT WORK HERE
if lead_x > gamewidth:
lead_x = gamewidth - boxDimensions
lead_x_change = 0
elif lead_x < 0:
lead_x = 0
lead_x_change = 0
elif lead_y > gameheight:
lead_y = gameheight - boxDimensions
elif lead_y < 0:
lead_y = 0
lead_y_change = 0
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, snakecolour, [lead_x,lead_y,boxDimensions,boxDimensions])
pygame.display.update()
clock.tick(15)
#message_to_screen("You Lose!", red) DOES WORK HERE
#pygame.display.update()
time.sleep(3)
pygame.quit()
message_to_screen("YOU LOSE!",(255,0,0))
pygame.display.update()
sleep(3)
From AirThomas comment, this is working. Outside of the while loop, put this statement.
For score board:
text = pygame.font.SysFont("None", 30)
score=0
text1=text.render("{}".format(score), True,(255,255,255))
while running:
screen.fill((0, 0, 0))
#codes
#codes
#codes
if sneakeatssomething:
score += 1
text1=text.render("{}".format(score), True,(255,255,255)) #rendering the score again
#codes
#codes
screen.blit(text1,(275,6))#showing the score
pygame.display.flip()
clock.tick(150)
This is updating the score when sneak eats and printing it to the pygame screen

Categories