Pygame function error bug - python

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.

Related

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, right goes down

I'm using 2.7.10 on pygame and I can't understand why the right keeps going down instead of right. The left works perfect. I can't find any issues relating, so does anyone know what is wrong with this?
import pygame
pygame.init()
GameDisplay = pygame.display.set_mode((800, 600))
White = (255,255,255)
Black = (0,0,0)
Red = (255,0,0)
pygame.display.set_caption('Test')
PyQuit = False
lead_x = 300
lead_y = 300
while not PyQuit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
PyQuit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x -= 10
if event.key == pygame.K_RIGHT:
lead_y += 10
GameDisplay.fill(White)
pygame.draw.rect(GameDisplay, Black, [lead_x,lead_y,10,10])
pygame.display.update()
pygame.quit()
quit()
Instead of:
lead_y += 10
Try:
lead_x += 10
Use lead_x for horizontal and lead_y for vertical.
if event.key == pygame.K_RIGHT:
lead_y += 10'
You are making the lead_y += 10 when you should be adding 10 to the x coordinate
if event.key == pygame.K_RIGHT:
lead_x += 10'

Python quits after I launch the application [closed]

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!

Error when I press C to reload/re-execute code?

I am trying to make a snake game using TheNewBoston's tutorials, since I am a Middle School student and dont have much experience in python. The code is:
__author__ = 'Ded'
import pygame
import time
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Dipshit')
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
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
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
if event.key == pygame.K_RIGHT:
lead_x_change += block_size
lead_y_change = 0
if event.key == pygame.K_DOWN:
lead_y_change += block_size
lead_x_change = 0
if event.key == pygame.K_UP:
lead_y_change -= block_size
lead_x_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
lead_x_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
lead_y_change = 0
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(white)
pygame.draw.rect(gameDisplay, black, [lead_x,lead_y,block_size,block_size])
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
gameLoop()
However, I get this error when I execute, play, lose and press C to retry. Q works fine when I want to quit but C just quits, and prints this error:
C:\Python34\python.exe "C:/Users/Ded/PycharmProjects/PyGame/PyGame
Tutorial.py" Traceback (most recent call last): File
"C:/Users/Ded/PycharmProjects/PyGame/PyGame Tutorial.py", line 100, in
gameLoop() File "C:/Users/Ded/PycharmProjects/PyGame/PyGame Tutorial.py", line 56, in gameLoop
gameLoop() File "C:/Users/Ded/PycharmProjects/PyGame/PyGame Tutorial.py", line 76, in gameLoop
if event.type == pygame.KEYUP: UnboundLocalError: local variable 'event' referenced before assignment
Process finished with exit code 1
You need to indent these parts of the code:
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
lead_x_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
lead_y_change = 0
so that they are part of the for loop. Otherwise if there are no events to get and no iterations of the loop occur, the variable event won't be defined.

Categories