So I am currently mid way through coding snake, in an attempt at learning pygame. I am quite new to coding so apologies if I have explained this badly. I have a game loop which is active when running = True, when the snake hits a boundary, running becomes False, ending the game loop and switches to a different while loop that says game over. I will of course add more to this screen later down the line. I am just currently trying to figure out how to make it when I click r, within this second loop, running becomes true again, triggering the game loop once again to effectively restart this game. I would really appreciate any suggestions, thank you!
import pygame
pygame.init()
display_x = 400
display_y = 300
display = pygame.display.set_mode((display_x, display_y))
pygame.display.set_caption("Ned's snake game")
x1 = display_x / 2
y1 = display_y / 2
x1_change = 0
y1_change = 0
clock = pygame.time.Clock()
fps = 45
game_over_font = pygame.font.Font("freesansbold.ttf", 16)
def game_over():
over_text = game_over_font.render("GAME OVER", True, (200, 0, 0))
display.blit(over_text, ((display_x / 2) - 54, (display_y / 2) - 16))
running = True
while running is True:
print("thing")
display.fill((50, 200, 20))
pygame.draw.rect(display, (0, 0, 255), [x1, y1, 10, 10])
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -5
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = 5
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -5
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = 5
x1_change = 0
elif event.key == pygame.K_ESCAPE:
pygame.quit()
x1 += x1_change
y1 += y1_change
if x1 <= 0 or x1 >= display_x or y1 <= 0 or y1 >= display_y:
running = False
end_screen = True
clock.tick(fps)
pygame.display.update()
while end_screen is True:
display.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
elif event.key == pygame.K_r:
running = True
end_screen = False
game_over()
pygame.display.update() ```
One way to approach this would be to put your entire while running == True block into a def. Doing so will push a number of your variables out of scope. To get around that you can move most of them inside of the def and use the global keyword to access the rest, as I've shown in the code.
I've called that def playGame(), and since you've put that block inside of playGame() you'll have to execute the statement playGame() to kickstart it, as you can see in the code. When the while in playGame() goes false it then falls through to the while end_screen == True block which then checks for that r key and, if it finds it, re-runs playGame().
This is a very quick and dirty fix to get you to where you want to be quickly, and I suspect that this has probably resulted in one or two bugs being created, but it's a start. If I were to offer you a constructive criticism it would be that you do not have a proper game loop in your program. If I get time later this evening I will add more commentary on that.
import pygame
pygame.init()
display_x = 400
display_y = 300
display = pygame.display.set_mode((display_x, display_y))
pygame.display.set_caption("Ned's snake game")
clock = pygame.time.Clock()
fps = 45
game_over_font = pygame.font.Font("freesansbold.ttf", 16)
def game_over():
over_text = game_over_font.render("GAME OVER", True, (200, 0, 0))
display.blit(over_text, ((display_x / 2) - 54, (display_y / 2) - 16))
def playGame():
global display_x, display_y, end_screen #"globals" are variables defined outside of the def block
x1 = display_x / 2 #moved these inside the def block
y1 = display_y / 2
x1_change = 0
y1_change = 0
#just moved this inside of a def block
running = True
while running is True:
#print("thing")
display.fill((50, 200, 20))
pygame.draw.rect(display, (0, 0, 255), [x1, y1, 10, 10])
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -5
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = 5
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -5
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = 5
x1_change = 0
elif event.key == pygame.K_ESCAPE:
pygame.quit()
x1 += x1_change
y1 += y1_change
if x1 <= 0 or x1 >= display_x or y1 <= 0 or y1 >= display_y:
running = False
end_screen = True
clock.tick(fps)
pygame.display.update()
playGame() #kickstart the game
while end_screen is True:
display.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
elif event.key == pygame.K_r:
running = True
end_screen = False
playGame() #restart the game
game_over()
pygame.display.update()
pygame.quit()
Related
I have a question regarding the restart and quit option.
Whenever I press q after hitting the "wall" it quits the program as intended, however the c button doesn't seem to restart the gameloop() but I can't find the reason why.
I did try adding game_over and game_close = False in that same line, however that did not seem to fix the problem. I also tried to add Else gameloop() which did not solve it either.
import pygame
import time
pygame.init()
dis_width = 800
dis_height = 600
dis=pygame.display.set_mode((dis_width,dis_height))
pygame.display.set_caption('Snake delux')
#define colors
blue=(0,0,255)
red=(255,0,0)
white = (255, 255, 255)
black = (0, 0, 0)
#starting values
x1_change = 0
y1_change = 0
x1 = dis_width/2
y1 = dis_height/2
#game parameters
snake_speed = 144
snake_block = 10
font_style = pygame.font.SysFont(None, 30)
game_over = False
game_close = False
clock = pygame.time.Clock()
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width/3, dis_height/3])
def gameLoop():
global game_over
global game_close
global x1_change
global y1_change
global x1
global y1
while not game_over:
while game_close == True:
dis.fill(black)
message("Press Q-Quit or C-Play Again", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
pygame.draw.rect(dis, blue, [x1, y1, snake_block, snake_block])
pygame.display.update()
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
If you reach the line where event.key == pygame.K_c is evaluated, then game_close must be True. You recursively call into the gameLoop function if c is being held down, but you don't change the game_over or game_close values so nothing will change.
If you imagine stepping through the code while pressing 'c':
if event.key == pygame.K_c:
This evaluates to True, so execution goes into the gameLoop function, game_close is still True, so you go right back to where you were.
I think, rather than recursively calling gameLoop() if the c key is pressed, what you need to do is change your game_close variable similar to what you do for quitting the game.
Do not call the gameLoop recursively, but run the game in a loop. Calling the main function recursively will mess up your variables and actually result in a game running within a game.
e.g.:
def gameLoop():
global game_end
# [...]
while not game_over:
while game_close == True:
[...]
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
game_end = True
if event.key == pygame.K_c:
game_over = True
game_close = False
game_end = False
# [...]
game_end False
while not game_end
x1_change = 0
y1_change = 0
x1 = dis_width/2
y1 = dis_height/2
snake_speed = 144
snake_block = 10
game_over = False
game_close = False
gameLoop()
Here is my code below. Every time when I try to increase the length, It does not seem to increase and when I run the game.
When I run the game, It sometimes makes the head of the snake flicker/blink certain times and when snake eats the food, still it does not increase the length instead of blinking little at certain times
I have attached code with my applied logic. Please is there any solution to this problem?
Here is my code with what I applied as the increase length logic:
Here is my logic:
import pygame
import time
import sys
import random
a = print("1) Easy")
b = print("2) Medium")
c = print("3) Hard")
while True:
difficulty = input("Enter Difficulty Level: ")
if difficulty == "1":
speed = 5
break
elif difficulty == "2":
speed = 6
break
elif difficulty == "3":
speed = 8
else:
print("Choose from Above Options Only!")
# Initialise Game
pygame.init()
clock = pygame.time.Clock()
# Screen and Window Size:
screen_width = 800
screen_height = 700
screen = pygame.display.set_mode((screen_width, screen_height))
caption = pygame.display.set_caption("Snake Game")
icon = pygame.image.load("snake.png")
pygame.display.set_icon(icon)
# Colors
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
white = (255, 255, 255)
# Snake Editing
x1 = 350
y1 = 300
snake = pygame.Rect([x1, y1, 20, 20])
x1_change = 0
y1_change = 0
snake_size = 15
snk_list = []
snk_length = 1
# Snake Food
food_x = random.randint(30, screen_width - 40)
food_y = random.randint(30, screen_height - 40)
food_height = 15
food_width = 15
# Game State
game_over = True
# Game over
font = pygame.font.SysFont("freelansbold.tff", 64)
# Score Counter
score = 0
score_font = pygame.font.SysFont("chiller", 50)
# TO INCREASE SNAKE LENGTH LOGIC:
def plot_snake(gameWindow, color, snk_list, snake_size):
for x, y in snk_list:
pygame.draw.rect(gameWindow, color, [x, y, snake_size, snake_size])
def game_over_text(text, color):
x = font.render(text, True, (240, 0, 0))
screen.blit(x, [screen_width//2 - 135, screen_height//2 - 25])
def score_show():
text = score_font.render("Score: " + str(score), True, (255, 255, 255))
screen.blit(text, (20, 10))
def main_loop():
global x1, y1, x1_change, y1_change, game_over, food_x, food_y, score, speed, snk_list, snake_size, snk_length
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# User Input
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
x1_change = speed * -1
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = speed
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = speed * -1
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y1_change = speed
x1_change = 0
# Game Over Checking
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_over = False
x1 += x1_change
y1 += y1_change
if abs(x1 - food_x) < 7 and abs(y1 - food_y) < 7:
score += 1
food_x = random.randint(30, screen_width - 40)
food_y = random.randint(30, screen_height - 40)
speed += 0.2
snk_length += 5
# Drawing On Screen
screen.fill((0, 0, 0))
pygame.draw.rect(screen, red, [x1, y1, 15, 15])
pygame.draw.rect(screen, green, [food_x, food_y, food_width, food_height])
score_show()
pygame.display.flip()
#SNAKE LENGTH LOGIC
head = []
head.append(x1)
head.append(y1)
snk_list.append(head)
if len(snk_list) > snk_length:
del snk_list[0]
plot_snake(screen, red, snk_list, snake_size)
# Final Initialisation
pygame.display.flip()
clock.tick(70)
# Main Game Loop
while game_over:
main_loop()
# Game_Over
screen.fill((0, 0, 0))
game_over_text("Game Over!!!", (255, 0, 0))
pygame.display.flip()
time.sleep(2)
pygame.quit()
quit()
The moving distance of the snake must be "snake_size":
def main_loop():
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# User Input
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
x1_change = -snake_size
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = snake_size
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = -snake_size
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y1_change = snake_size
x1_change = 0
But use pygame.time.Clock to control the frames per second and thus the game speed.
def main_loop():
# [...]
clock.tick(speed)
Use pygame.Rect and collidrect to find the collision of the snake and the food. See also How to detect collisions between two rectangular objects or images in pygame. Increment snk_length, when a collision is detected:
def main_loop():
# [...]
global snk_length
# [...]
snake_rect = pygame.Rect(x1, y1, snake_size, snake_size)
food_rect = pygame.Rect(food_x, food_y, snake_size, snake_size)
if snake_rect.colliderect(food_rect):
snk_length += 1
score += 1
food_x = random.randint(30, screen_width - 40)
food_y = random.randint(30, screen_height - 40)
speed += 1
Follow the instructions of How do I chain the movement of a snake's body?. Put the new head position of the snake at the head of snk_list, but delete the elements at the tail:
def main_loop():
# [...]
#SNAKE LENGTH LOGIC
snk_list.insert(0, [x1, y1])
if len(snk_list) > snk_length:
del snk_list[-1]
Complete main_loop:
def main_loop():
global x1, y1, x1_change, y1_change, game_over, food_x, food_y, score, speed, snk_list, snake_size
global snk_length
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# User Input
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
x1_change = -snake_size
y1_change = 0
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
x1_change = snake_size
y1_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_w:
y1_change = -snake_size
x1_change = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
y1_change = snake_size
x1_change = 0
# Game Over Checking
if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_over = False
x1 += x1_change
y1 += y1_change
snake_rect = pygame.Rect(x1, y1, snake_size, snake_size)
food_rect = pygame.Rect(food_x, food_y, snake_size, snake_size)
if snake_rect.colliderect(food_rect):
snk_length += 1
score += 1
food_x = random.randint(30, screen_width - 40)
food_y = random.randint(30, screen_height - 40)
speed += 1
# Drawing On Screen
screen.fill((0, 0, 0))
pygame.draw.rect(screen, red, [x1, y1, 15, 15])
pygame.draw.rect(screen, green, [food_x, food_y, food_width, food_height])
score_show()
pygame.display.flip()
#SNAKE LENGTH LOGIC
snk_list.insert(0, [x1, y1])
if len(snk_list) > snk_length:
del snk_list[-1]
plot_snake(screen, red, snk_list, snake_size)
# Final Initialisation
pygame.display.flip()
clock.tick(speed)
I was working on the basic game "Snake" in Python using "pygame". When I move around at the start and I am alive, my arrow keys are working fine. When I die, then I can't use any key and I can't close the window by pressing the X button on the top right of the window. The only way to terminate it is by pressing Ctrl-x in the console, and that way it doesn't close.
When I debugged it, my console says that my q and c values are 59, 248 accordingly but the pygame. K_q and pygame.K_c have the values 113, 99. Does anybody knows the reason? My code when I die is the following:
while game_close == True:
self.dis.fill(colors("blue"))
self.message("You Lost! Press C-Play Again or Q-Quit", colors("red"))
self.Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
elif event.key == pygame.K_c:
game_close = False
self.gameLoop()
If anybody has any clue why this happened it will be useful.
It is my first time writing any quest so sorry for not having the best description or there is any duplicate( I have searched and found nothing that works for me)
edit: This is the whole script:
game_over = False
game_close = False
x1 = self.dis_width / 2
y1 = self.dis_height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, self.dis_height - self.snake_block) / 10.0) * 10.0
flagex=True
flagey=True
while not game_over:
while game_close :
self.dis.fill(colors.blue())
self.message("You Lost! Press C-Play Again or Q-Quit", colors.red())
self.Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_close = True
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
elif event.key == pygame.K_c:
game_close = False
self.gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and flagey==True:
flagey = False
flagex = True
x1_change = -self.snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT and flagey==True:
flagey = False
flagex = True
x1_change = self.snake_block
y1_change = 0
elif event.key == pygame.K_UP and flagex==True:
flagey = True
flagex = False
y1_change = -self.snake_block
x1_change = 0
elif event.key == pygame.K_DOWN and flagex==True:
flagey = True
flagex = False
y1_change = self.snake_block
x1_change = 0
if x1 >= self.dis_width or x1 < 0 or y1 >= self.dis_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
self.dis.fill(colors.blue())
pygame.draw.rect(self.dis, colors.green(), [foodx, foody, self.snake_block, self.snake_block])
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
self.our_snake(self.snake_block, snake_List)
self.Your_score(Length_of_snake - 1,)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, self.dis_width - self.snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, self.dis_height - self.snake_block) / 10.0) * 10.0
Length_of_snake += 1
self.clock.tick(self.snake_speed)
pygame.quit()
quit()
I won't be able to answer your question completely because it would be nice to see the whole code but I would definietly change it to this:
while game_close:
self.dis.fill(colors("blue"))
self.message("You Lost! Press C-Play Again or Q-Quit", colors("red"))
self.Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_close = True
game_over = True
# exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_q]:
game_over = True
game_close = False
if keys[pygame.K_c]:
game_close = False
self.gameLoop()
Try changing it to this
for event in pygame.event.get():
if event.type == pygame.KEYUP:
if event.key == pygame.K_q:
game_over = True
game_close = False
elif event.key == pygame.K_c:
game_close = False
self.gameLoop()
The whole code is not included, a class is missing and some methods are not defined. But first I would suggest changing all the key presses to this:
keys = pygame.key.get_pressed()
if keys[pygame.K_yourkey]:
# do something
if keys[pygame.K_yourotherkey]:
# do something
# and so on
and dont put it in event for loop
Basically I am trying to add some boundaries to a small game I am making, I have a sprite which can only move left to right and I am trying to prevent it from it from moving off the game window, if the sprite reaches the edge they can go no further and can then go in the opposite direction.
My current code:
tankSprite = pygame.image.load('Sprites/Tank.png') #fixed apostrophe
tankSprite_width = 28
def tank(x,y):
gameWindow.blit(tankSprite, (x,y))
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
gameExit = False
gameMove = True
while not gameExit and gameMove == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = False
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFTor event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameWindow.fill(white)
tank(x,y)
if x > display_width - tankSprite_width or x < 0:
gameMove = False
elif x < display_width - tankSprite_width or x > 0:
gameMove = True
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
My current code partially works, when the sprite reaches the edge the solution closes itself rather than letting me go back. If I remove:
pygame.quit()
quit()
Then the sprite just stops and I can't move it back. In a project I did a while back I used something like:
if event.key == pygame.K_LEFT and sprite.x < 590:
sprite.x += 5
The above code worked well from what I can remember but I can't seem to figure it out for my project now, can anyone help me out please?
I wish to make it so that the sprite can't go past the screen border, when the player reaches the border they either stay or go back in the opposite direction. Thanks
p.s. Sorry about the dodgy formatting
Just split your if-statement and check the condition for each direction separately:
tankSprite = pygame.image.load('Sprites/Tank.png')
tankSprite_width = 28
def tank(x,y):
gameWindow.blit(tankSprite, (x,y))
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
gameExit = False
gameMove = True
while not gameExit and gameMove == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = False
if event.type ==pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFTor event.key == pygame.K_RIGHT:
x_change = 0
#valid move, add x_change
if((x+x_change)> 0 and (x+x_change)<(display_width - tankSprite_width):
x += x_change
gameWindow.fill(white)
tank(x,y)
#deleted stuff here
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
With this method, you don't need to even use the gameMove variable anymore.
You could also get rid of the x_change variable entirely by simply applying the changes directly to x instead.
Also, I think you may have meant gameExit = True under the if statement if event.type == pygame.QUIT:, as it makes sense to exit when event pygame.QUIT is triggered.
I'm working on the basics of a game right now, but for some reason I cannot get my sprite to move. I know that it's registering when I'm pressing the key down, but the sprite for some reason just stays still.
import pygame
import sys
from pygame.locals import *
import time
pygame.init()
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("RADICAL")
screen.fill(black)
imga = pygame.image.load('coina.png')
imgb = pygame.image.load('coinb.png')
sound = pygame.mixer.Sound('coin.mp3')
FPS = 80
imgx = 10
imgy = 10
pixMove = 10
steps = 0
x1 = 0
y1 = 0
keystate = pygame.key.get_pressed()
GameOver = False
while not GameOver:
screen.fill(white)
points = (steps)
font = pygame.font.SysFont(None, 30)
text = font.render('Score: '+str(points), True, black)
screen.blit(text, (0,0))
if steps % 2 == 0:
screen.blit(imga, (imgx, imgy))
else:
screen.blit(imgb, (imgx, imgy))
for event in pygame.event.get():
print event
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if keystate[pygame.K_UP]:
y1 -= pixMove
elif keystate[pygame.K_DOWN]:
y1 += pixMove
elif keystate[pygame.K_LEFT]:
x1 -= pixMove
elif keystate[pygame.K_RIGHT]:
x1 += pixMove
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x1 = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y1 = 0
steps +=1
if event.type == K_SPACE:
sound.play()
time.sleep(1)
sound.stop()
pygame.display.update()
fpsTime.tick(FPS)
Generally you blit() images on to the screen (pygame.display.set_mode()), for the changes to to reflect on the screen we call pygame.display.update(), In your case the game never comes the statement, which is out of while loop.