I have this code, and when I click ESC, I don't see "Game Over". The program waits for two seconds and closes without displaying text.
Pygame 1.9.6
What am I doing wrong?
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
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_x_change = 0
lead_y_change = -block_size
elif event.key == pygame.K_DOWN:
lead_x_change = 0
lead_y_change = block_size
elif event.key == pygame.K_ESCAPE:
run = False
game_display.fill(white)
lead_x += lead_x_change
lead_y += lead_y_change
pygame.draw.rect(game_display, black, [lead_x, lead_y, width, height])
pygame.display.update()
clock.tick()
draw_text_middle("Game Over", 40, (0, 0, 0, 255), game_display)
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
You've to remove the pygame.quit() call from the event loop. pygame.quit() uninitialize all pygame modules. After calling this function, nothing can be drawn any further calls to any pygame instructions will cause an exception.
I recommend to process the pygame events by pygame.event.pump() before .delay the application. This allows pygame to handle internal actions:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# pygame.quit() <---------- delete
# [...]
draw_text_middle("Game Over", 40, (0, 0, 0, 255), game_display)
pygame.display.update()
pygame.event.pump()
pygame.time.delay(2000)
pygame.quit()
Related
import sys
import pygame
(width, height) = (800, 600)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
#Title and Icon
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("spaceship.png")
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load("player.png")
playerX = 370
playerY = 480
player_change = 0
def player(x, y):
screen.blit(playerImg, (x, y))
#Game loop
running = True
while running:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
#RGB- Red, Green, Blue
screen.fill((0, 128, 128))
# if keystroke is pressed check whether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
# 5 = 5 + -0.1 -> 5 = 5 -0.1
# 5 = 5 + 0.1
playerX += playerX_change
player(playerX, playerY)
pygame.display.update()
New atcoding so I followed this youtube video and there his image loaded and mine did too at first. But later if
I added these lines of code, it stopped appearing on the screen. I followed his instructions step by step.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
How can i solve this problem
You just need 1 application loop. All the events must be handled in the event loop. You have to correct the Indentation.
Also see How to get keyboard input in pygame?:
speed = 1
# application loop
clock = pygame.time.Clock()
running = True
while running:
clock.tick(100)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
# clear display
screen.fill((0, 128, 128))
# draw scene
player(playerX, playerY)
# update diesplay
pygame.display.update()
pygame.quit()
sys.exit()
The typical PyGame application loop has to:
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
handle the events by calling either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by calling either pygame.display.update() or pygame.display.flip()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
#RGB- Red, Green, Blue
screen.fill((0, 128, 128))
# if keystroke is pressed check whether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
# **This code is outside of main loop**
playerX += playerX_change
player(playerX, playerY)
pygame.display.update()
You are running main loop twice(while running) and the new code you added was out of it's scope.
I am currently following a pygame tutorial on a chromebook on which i have installed and linux to use IDLE. i am writing a block of code which assigns and x-axis increase or decrase to the arrow keys:
import pygame
pygame.init()
displayWidth = 800
displayHeight = 600
black = (0, 0, 0)
white = (255, 255, 255)
clock = pygame.time.Clock()
crashed = False
carImg = pygame.image.load('racecar.png')
def car(x,y):
gameDisplay.blit(carImg, (x,y))
x = (displayWidth * 0.45)
y = (displayHeight * 0.8)
x_change = 0
car_speed = 0
gameDisplay = pygame.display.set_mode((displayHeight, displayWidth))
pygame.display.set_caption('Zoomer')
clock = pygame.time.Clock()
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
car(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
when i try to run the code, the car sprite won't budge. is this due to the fact that i am on chromebook and key names are different or is it an other reason? thanks.
It doesn't work because you have wrong indentations.
You check pygame.KEYDOWN inside if ... pygame.QUIT: which is executed only when you close window.
You need all if event.type start in the same column
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
import pygame, sys
pygame.init()
display_width = 800
display_height = 500
white = (255, 255, 255)
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pong')
clock = pygame.time.Clock()
platform_y = display_height * 0.38
def platform(color, x, y):
global platform_y
pygame.draw.rect(display, color, (x, y, 25, 100))
pygame.display.update()
clock.tick(80)
def ball():
pygame.draw.circle(display, white, (400, 250), 12)
pygame.display.update()
def game():
global platform_y
y_change = 0
while True:
platform(white, 100, platform_y)
platform(white, 675, platform_y)
ball()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
sys.exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change -= 2
if event.key == pygame.K_DOWN:
y_change += 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
platform_y += y_change
pygame.display.update()
clock.tick(80)
if __name__ == '__main__':
game()
I guess it's not the best code but I've been messing around with pygame and got to the point when I have to create Pong, although I'm not sure why the rectangles (platforms) are just getting higher instead of going up and down (I know that both platforms will rise together, will fix that, this is just for test)
You had couple of issues with your code:
You have to call pygame.display.update only once, not after every pygame.draw,
clock.tick is set in main game loop, not in other places,
You have to clear the screen before drawing.
Here is what will probably work as you expect:
import pygame, sys
pygame.init()
display_width = 800
display_height = 500
white = (255, 255, 255)
display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pong')
clock = pygame.time.Clock()
platform_y = display_height * 0.38
def platform(color, x, y):
global platform_y
pygame.draw.rect(display, color, (x, y, 25, 100))
def ball():
pygame.draw.circle(display, white, (400, 250), 12)
def game():
global platform_y
y_change = 0
while True:
platform(white, 100, platform_y)
platform(white, 675, platform_y)
ball()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
sys.exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change -= 2
if event.key == pygame.K_DOWN:
y_change += 2
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
platform_y += y_change
pygame.display.update()
clock.tick(80)
display.fill((0, 0, 0))
if __name__ == '__main__':
game()
Couple of advices:
Don't use global! It's smells on bad code. There are certainly better ways to achieve what you wanted without using it.
Try to avoid a lot of conditions nesting. It's very hard to read, and will require much mental energy to read after day or two.
Happy coding!
So, I'm making my first game in pygame, and have done OK up to this point. I just can't move the image. Can I please get some help?
mc_x = 20
mc_y = 20
spider_x = 690
spider_y = 500
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
mc_x -= 5
elif event.key == pygame.K_RIGHT:
mc_x += 5
elif event.key == pygame.K_UP:
mc_y += 5
elif event.key == pygame.K_DOWN:
mc_y -= 5
screen.blit(background,(0,0))#fixed
screen.blit(spider_small,(spider_x,spider_y))#FIXED
screen.blit(mc,(mc_x,mc_y))
pygame.display.update()
Based on your code:
screen.blit(mc,(mc_x,mc_y))
pygame.display.update()
should be inside the loop so that it would update/refresh your game for every keystroke.
You forgot to update the screen. Set the update function inside the main game loop. This is going to work fine!
Here's my sample code
import pygame
pygame.init()
WHITE = (255, 255, 255)
RED = (255, 0, 0)
canvas = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Example')
gameExit = False
lead_x, lead_y = 300, 300
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:
lead_x -= 10
elif event.key == pygame.K_RIGHT:
lead_x += 10
elif event.key == pygame.K_DOWN:
lead_y += 10
elif event.key == pygame.K_UP:
lead_y -= 10
canvas.fill(WHITE)
pygame.draw.rect(canvas, RED, [lead_x, lead_y, 30, 30])
pygame.display.update()
pygame.quit()
quit()
So heres my script:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((600,600))
pygame.display.set_caption('Doge Adventures')
gameexit = False
move_x = 300
move_y = 300
move_x_change = 0
move_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_LEFT:
move_x_change = -10
if event.key == pygame.K_RIGHT:
move_y_change = 10
move_x += move_x_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [move_x, move_y, 10, 10])
pygame.display.update()
pygame.quit()
quit()
The problem is that when I run it, nothing happens, no errors. Just the idle pops up, but no window. I was wondering if there was anything wrong with my code. I'm running python 2.6 with pygame 2.6
As I said you need to indentate everything, but I found many problems in your code, here's a version I did out of it:
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((600,600))
pygame.display.set_caption('Doge Adventures')
gameexit = False
move_x = 300
move_y = 300
white = (255, 255, 255)
black = (0, 0, 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_LEFT:
move_x -= 10
elif event.key == pygame.K_RIGHT:
move_x += 10
elif event.key == pygame.K_UP:
move_y -= 10
elif event.key == pygame.K_DOWN:
move_y += 10
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [move_x, move_y, 10, 10])
pygame.display.update()
pygame.quit()
quit()
I recommend you the Al Sweigart book of Python/Pygame. It's such a great book.