I am trying to create a Tic tac toe game in pygame. I want to display an image on screen when a key is pressed. Pressing the escape key allows me to quit game however when I press key 1 to blit the image nothing happens. Here is the code
import pygame
from pygame.locals import (
K_1,
K_2,
K_3,
K_4,
K_5,
K_6,
K_7,
K_8,
K_9,
K_ESCAPE,
K_UP,
KEYDOWN,
QUIT
)
clock = pygame.time.Clock()
pygame.init()
# Constants
display_width = 300
display_height = 300
white = (255, 255, 255)
black = (0, 0, 0)
run = True
# Creating game screen
display = pygame.display.set_mode((300, 300))
pygame.display.set_caption("Tic Tac Toe Game")
display.fill(white)
# importing images to pygame
circle_image = pygame.image.load("resizedimage1.png")
cross_image = pygame.image.load("resizedimage2.png")
# main game loop
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
run = False
elif event.key == K_1:
display.blit(cross_image, (10, 10))
clock.tick(30)
display.fill(white)
pygame.draw.line(display, black, (100, 0), (100, 300))
pygame.draw.line(display, black, (200, 0), (200, 300))
pygame.draw.line(display, black, (0, 100), (300, 100))
pygame.draw.line(display, black, (0, 200), (300, 200))
pygame.display.update()
pygame.quit()
You have to put the line
display.fill(white)
at the top of your while run. Because now you are rendering the picture and in the next frame you paint everything white. So you are not able to see the image.
You also should use the event KEYUP. Because KEYDOWN will be fired multiple times, so it will lead to flickering effects.
Of course you have to draw an blit all the objects after display.fill(), because pygame.Surface.fill fill the Surface with a solid color.
Anyway, I recommend to create a list of images for the nine fields of the game. Init the list by None:
field = [None] * 9
Set the corresponding filed in the list if a button is pressed. Note list indices start at 0:
elif event.key == K_1:
field[0] = cross_image
Draw the fields of the list in a loop. Use enumerate() to iterate through the list. The row of a field can be computed by the // (floor division) operator and the column by the % (modulo) operator (see Binary arithmetic operations):
for i, img in enumerate(field):
if img:
column = i % 3
row = i // 3
display.blit(img, (column * 100 + 10, row * 100 + 10))
Example code:
import pygame
from pygame.locals import (
K_1, K_2, K_3, K_4, K_5, K_6, K_7, K_8, K_9,
K_ESCAPE, K_UP, KEYDOWN,
QUIT
)
clock = pygame.time.Clock()
pygame.init()
# Constants
display_width = 300
display_height = 300
white = (255, 255, 255)
black = (0, 0, 0)
run = True
# Creating game screen
display = pygame.display.set_mode((300, 300))
pygame.display.set_caption("Tic Tac Toe Game")
display.fill(white)
# importing images to pygame
circle_image = pygame.image.load("resizedimage1.png")
cross_image = pygame.image.load("resizedimage2.png")
field = [None] * 9
# main game loop
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
run = False
elif event.key == K_1:
field[0] = cross_image
display.fill(white)
pygame.draw.line(display, black, (100, 0), (100, 300))
pygame.draw.line(display, black, (200, 0), (200, 300))
pygame.draw.line(display, black, (0, 100), (300, 100))
pygame.draw.line(display, black, (0, 200), (300, 200))
for i, img in enumerate(field):
if img:
column = i % 3
row = i // 3
display.blit(img, (column * 100 + 10, row * 100 + 10))
pygame.display.update()
pygame.quit()
Related
I am having an issue where the display doesn't update for some reason. I have confirmed that x goes up but the display doesn't show it for some reason.
Code:
import pygame
SIZE = (500, 500)
CENTER = (SIZE[0] / 2, SIZE[1] / 2)
FPS = 60
COLORS = {
"white": (255, 255, 255),
"red": (255, 0, 0),
"green": (0, 255, 0),
"blue": (0, 0, 255),
"black": (0, 0, 0)
}
WIN = pygame.display.set_mode(SIZE)
pygame.display.set_caption("what da dog doing?")
def main():
clock = pygame.time.Clock()
run = True
player = pygame.draw.rect(WIN, COLORS.get(
"white"), (CENTER[0] - 400 / 2, CENTER[1] - 70 / 2, 400, 70), border_radius=10)
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
player.x += 1
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
Any help is appreciated.
you need to place stuff that is supposed to be updated in the main loop, also you need to fill the screen with some color (or blit an image that is large enough to cover the area you need) to draw over the previously drawn stuff
def main():
clock = pygame.time.Clock()
run = True
player = pygame.draw.rect(WIN, COLORS.get(
"white"), (CENTER[0] - 400 / 2, CENTER[1] - 70 / 2, 400, 70), border_radius=10)
while run:
clock.tick(FPS)
WIN.fill((0, 0, 0))
pygame.draw.rect(WIN, COLORS.get('white'), player, border_radius=10)
player.x += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
With pygame, I created a 20x20 pixel window and added a 2x2 pixel rectangle.
When I run the program, the window size is super small and I can barely see the rectangle. How can I increase the window size whilst keeping the number of pixels constant, i.e. increase the pixel size? I am aware of this similar question, but there a somewhat more complicated case is discussed.
import pygame
screen_width, screen_height = 20, 20
x, y = 10, 10
rect_width, rect_height = 2, 2
vel = 2
black = (0, 0, 0)
white = (255, 255, 255)
pygame.init()
win = pygame.display.set_mode((screen_width, screen_height))
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill(black)
pygame.draw.rect(win, white, (x, y, rect_width, rect_height))
pygame.display.update()
pygame.quit()
Don't draw directly to the screen, but to another Surface.
Then scale that new Surface to the size of the screen and blit it onto the real screen surface.
Here's an example:
import pygame
screen_width, screen_height = 20, 20
scaling_factor = 6
x, y = 10, 10
rect_width, rect_height = 2, 2
vel = 2
black = (0, 0, 0)
white = (255, 255, 255)
pygame.init()
win = pygame.display.set_mode((screen_width*scaling_factor, screen_height*scaling_factor))
screen = pygame.Surface((screen_width, screen_height))
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill(black)
pygame.draw.rect(screen, white, (x, y, rect_width, rect_height))
win.blit(pygame.transform.scale(screen, win.get_rect().size), (0, 0))
pygame.display.update()
pygame.quit()
I’m the first time to ask on stack overflow! I’m a 12-year-old boy who living in Hong Kong, so if my English was wrong, please tell me and please don’t keep in mind. Apart from that, I’m a newer of Python. I don’t know that the meaning of the code. Can everyone making a # to tell me the meaning? Thank you!!
I am doing a project that use Python. Also, for the display, I use Pygame too. But there have some problem with the displays.
Here is my testing code:
# install pygame
import pygame
from pygame import *
pygame.init()
# play music
mixer.init()
mixer.music.load("game_music.mp3")
mixer.music.play()
# colour
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
grass_green = (112, 173, 71)
white = (255, 255, 255)
black = (0, 0, 0)
# screen settings
window = pygame.display.set_mode((640, 600))
window.fill(grass_green)
pygame.display.set_caption("What's Your Number?")
# fonts settings
default_font = pygame.font.get_default_font()
font_a = pygame.font.Font(default_font, 57)
font_b = pygame.font.Font(default_font, 30)
font_c = pygame.font.Font(default_font, 18)
# text
title = font_a.render("What's Your Number?", 1, white)
enter_to_start = font_b.render("Press the Enter to start", 1, white)
random_mode = font_a.render("Random Mode", 1, black)
your_mode = font_a.render("Your Mode", 1, black)
des_random_1 = font_c.render("The PC choose a random number for you to", 1, black)
des_random_2 = font_c.render("guess! Can you guess it correct?", 1, black)
des_your = font_c.render("Pick a number and let you friends to guess!", 1, black)
random_control = font_b.render("Press the up arrow", 1, black)
your_control = font_b.render("Press the down arrow", 1, black)
# image
up_arrow = pygame.image.load("up_arrow_new.png")
down_arrow = pygame.image.load("down_arrow_new.png")
def blit_img(img, x, y):
window.blit(img, (x, y))
# game
game = False
temp = True
while not game:
if temp:
window.blit(title, (10, 150))
window.blit(enter_to_start, (160, 350))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_KP_ENTER:
temp = False
window.fill(grass_green)
pygame.draw.rect(window, red, [50, 50, 540, 225])
pygame.draw.rect(window, blue, [50, 325, 540, 225])
window.blit(random_mode, (55, 65))
window.blit(des_random_1, (55, 150))
window.blit(des_random_2, (55, 200))
window.blit(random_control, (55, 240))
blit_img(up_arrow, 450, 150)
window.blit(your_mode, (55, 335))
window.blit(des_your, (55, 425))
window.blit(your_control, (55, 515))
blit_img(down_arrow, 450, 425)
pygame.display.flip()
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
if temp == False:
temp = True
window.fill(grass_green)
pygame.display.flip()
if event.key == pygame.K_ESCAPE:
game = True
if event.type == pygame.QUIT:
game = True
I want to make it just like a book or a PowerPoint to have pages. In this code, my problem is that the text which displayed cannot erase and blit the new text on it. Can someone help me? Thank you very much!!
I'd fill the window every frame and then render and blit the current text. The texts can be stored in a list and the current text can be accessed with an index variable that you increment in the event loop.
import pygame
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
window = pygame.display.set_mode((640, 600))
clock = pygame.time.Clock() # A clock to limit the frame rate.
font = pygame.font.Font(None, 57) # Use the default font.
texts = ['Hello', "what's", 'up?']
text_index = 0
done = False
while not done:
# Handle events.
for event in pygame.event.get():
# Allow the user to quit by clicking on the 'X' button.
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key in (pygame.K_KP_ENTER, pygame.K_RETURN):
# Make sure that we don't get an IndexError.
if text_index < len(texts)-1:
# Increment the index.
text_index += 1
# Insert additional game logic here.
# Finally draw everything.
window.fill(white) # Use fill to clear the window.
what_I_say = font.render(texts[text_index], True, black)
window.blit(what_I_say, (10, 150))
pygame.display.flip()
clock.tick(30) # Limit the frame rate to 30 FPS.
If you really only need to update the window when the user wants to switch to the next page (an event occurs), you could also use pygame.event.wait instead of pygame.event.get.
The way to remove previous stuff in pygame is to draw the background and everything you want to keep over it. In this case, you want to remove the Hello, so you will have to stop pygame from drawing it, and then draw the background over it
It should look like this:
import pygame
pygame.init()
#colour settings
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
#making a screen(display)
window = pygame.display.set_mode((640, 600))
window.fill(green)
# fonts settings
default_font = pygame.font.get_default_font()
font = pygame.font.Font(default_font, 57)
temp = True
#the things I want to display
while True:
if temp:
what_I_say = font.render("Hello", 1, white)
window.blit(what_I_say, (10, 150))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
temp = False
window.fill(green)
pygame.draw.rect(window, red, [50, 50, 540, 500])
The boolean temp marks whether to draw Hello. On pressing return (my keyboard doesn't have keypad enter), temp is set to false (so it will stop drawing Hello), the window is refilled with green (to cover the previous hello), and then a red rectangle is drawn.
I am attempting to create a racing game in pygame. I want it such that when the car goes off the track, it slows down. I have tried to do this by having another sprite that is an outline of the track and when the car touches that sprite, it slows down. This does not work and I don't know why. Is there a better way to do this?
Img is the car image
Back is the racetrack
BackHit is the outline
I receive this error code:
Traceback (most recent call last):
File "C:\Users\Daniella\Desktop\Python\Games\game.py", line 75, in
if pygame.sprite.collide_mask(Img, BackHit):
File "C:\Users\Daniella\AppData\Roaming\Python\Python36\site-packages\pygame\sprite.py", line 1470, in collide_mask
xoffset = right.rect[0] - left.rect[0]
AttributeError: 'pygame.Surface' object has no attribute 'rect'
This is the code:
import pygame
Width = 800
Height = 600
Black = (0, 0, 0)
White = (255, 255, 255)
Red = (255, 0, 0)
Green = (0, 255, 0)
Blue = (0, 0, 255)
Yellow = (255, 255, 0)
BackColour = (198, 151, 107)
pygame.init()
GameDisplay = pygame.display.set_mode((Width, Height))
pygame.display.set_caption("A bit Racey")
Clock = pygame.time.Clock()
Img = pygame.image.load("download.png")
ImgWidth = 46
ImgHeight = 68
Img = pygame.transform.scale(Img, (ImgWidth, ImgHeight))
Back = pygame.image.load("back1.png")
BackWidth = Width*4
BackHeight = Height*4
Back = pygame.transform.scale(Back, (BackWidth, BackHeight))
BackHit = pygame.image.load("back1 hit1.png")
BackHitWidth = Width*4
BackHitHeight = Height*4
BackHit = pygame.transform.scale(BackHit, (BackHitWidth, BackHitHeight))
def Car():
GameDisplay.blit(Img, (400-ImgWidth/2, 300-ImgHeight/2))
def Background(X, Y):
GameDisplay.blit(Back, (X, Y))
def BackgroundHit(X, Y):
GameDisplay.blit(BackHit, (X, Y))
X = (Width*0.45)
Y = (Height*0.5)
XChange = 0
YChange = 0
Changer = 1
Crashed = False
while not Crashed:
for Event in pygame.event.get():
if Event.type == pygame.QUIT:
Crashed = True
elif Event.type == pygame.KEYDOWN:
if Event.key == pygame.K_LEFT:
Img = pygame.transform.rotate(Img, -90)
XChange = 5 / Changer
elif Event.key == pygame.K_RIGHT:
Img = pygame.transform.rotate(Img, 90)
XChange = -5 / Changer
elif Event.key == pygame.K_UP:
Img = pygame.transform.rotate(Img, 0)
YChange = 5 / Changer
elif Event.key == pygame.K_DOWN:
Img = pygame.transform.rotate(Img, 180)
YChange = -5 / Changer
if Event.type == pygame.KEYUP:
if Event.key == pygame.K_LEFT or Event.key == pygame.K_RIGHT:
XChange = 0
elif Event.key == pygame.K_UP or Event.key == pygame.K_DOWN:
YChange = 0
if pygame.sprite.collide_mask(Img, BackHit):
Changer = 2
Y += YChange
X += XChange
GameDisplay.fill(White)
BackgroundHit(X, Y)
Background(X, Y)
Car()
pygame.display.update()
Clock.tick(200)
pygame.quit()
quit()
Here's a little example that shows you how you can use pygame.mask.from_surface and pygame.Mask.overlap for pixel perfect collision detection.
import pygame as pg
# Transparent surfaces with a circle and a triangle.
circle_surface = pg.Surface((60, 60), pg.SRCALPHA)
pg.draw.circle(circle_surface, (30, 90, 200), (30, 30), 30)
triangle_surface = pg.Surface((60, 60), pg.SRCALPHA)
pg.draw.polygon(triangle_surface, (160, 250, 0), ((30, 0), (60, 60), (0, 60)))
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
# Use `pygame.mask.from_surface` to get the masks.
circle_mask = pg.mask.from_surface(circle_surface)
triangle_mask = pg.mask.from_surface(triangle_surface)
# Also create rects for the two images/surfaces.
circle_rect = circle_surface.get_rect(center=(320, 240))
triangle_rect = triangle_surface.get_rect(center=(0, 0))
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEMOTION:
triangle_rect.center = event.pos
# Now calculate the offset between the rects.
offset_x = triangle_rect.x - circle_rect.x
offset_y = triangle_rect.y - circle_rect.y
# And pass the offset to the `overlap` method of the mask.
overlap = circle_mask.overlap(triangle_mask, (offset_x, offset_y))
if overlap:
print('The two masks overlap!', overlap)
screen.fill((30, 30, 30))
screen.blit(circle_surface, circle_rect)
screen.blit(triangle_surface, triangle_rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
To create a mask for the background or track you need to create an extra image and either leave the track transparent or the area where the car should slow down and then check if the car collides with the track or with the outside area. Here I check if the green triangle collides with the "track" (the blue lines), and in your game you would then slow the car down if it doesn't collide with the track.
import pygame as pg
bg_surface = pg.Surface((640, 480), pg.SRCALPHA)
pg.draw.lines(
bg_surface, (30, 90, 200), True,
((60, 130), (300, 50), (600, 200), (400, 400), (150, 300)),
12)
triangle_surface = pg.Surface((60, 60), pg.SRCALPHA)
pg.draw.polygon(triangle_surface, (160, 250, 0), ((30, 0), (60, 60), (0, 60)))
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
bg_mask = pg.mask.from_surface(bg_surface)
triangle_mask = pg.mask.from_surface(triangle_surface)
bg_rect = bg_surface.get_rect(center=(320, 240))
triangle_rect = triangle_surface.get_rect(center=(0, 0))
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEMOTION:
triangle_rect.center = event.pos
offset_x = triangle_rect.x - bg_rect.x
offset_y = triangle_rect.y - bg_rect.y
overlap = bg_mask.overlap(triangle_mask, (offset_x, offset_y))
if overlap:
print('The two masks overlap!', overlap)
screen.fill((30, 30, 30))
screen.blit(bg_surface, bg_rect)
screen.blit(triangle_surface, triangle_rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
pygame.sprite.collide_mask is meant for pygame.sprite.Sprite objects. Instead use mask_var.overlap(other_mask, offset). To calculate the offset between the two masks, use offset = [other_object_x - object_x, other_object_y - object_y]. And to get the mask of an image, use mask_var = pygame.mask.from_surface(image).
I've been trying to make a game, and everything in there works so far except that the pause button , that when pressed the button P should pause and when pressed S should continue. I kinda understand the problem such that once in enters the while loop in the main code it wont get out. I tried putting the pause function inside the while loop. Please do help or provide tips to fix if possible thank you.
import pygame
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
Blue = (2,55,55)
def recursive_draw(x, y, width, height):
""" Recursive rectangle function. """
pygame.draw.rect(screen, WHITE,
[x, y, width, height],
1)
speed = [10,0]
rect_change_x = 10
rect_change_y = 10
# Is the rectangle wide enough to draw again?
if (width > 25):
# Scale down
x += width * .1
y += height * .1
width *= .8
height *= .8
# Recursively draw again
recursive_draw(x, y, width, height)
def recursive_draw2(x, y, width, height):
""" Recursive rectangle function. """
pygame.draw.rect(screen, Blue,
[x, y, width, height],
1)
speed = [10,0]
rect_change_x = 10
rect_change_y = 10
# Is the rectangle wide enough to draw again?
if (width > 25):
x += width * .1
y += height * .1
width *= .8
height *= .8
# Recursively draw again
recursive_draw2(x, y, width, height)
def paused():
screen.fill(black)
largeText = pygame.font.SysFont("comicsansms",115)
TextSurf, TextRect = text_objects("Paused", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#gameDisplay.fill(white)
button("Continue",150,450,100,50,green,bright_green,unpause)
button("Quit",550,450,100,50,red,bright_red,quitgame)
pygame.display.update()
clock.tick(15)
pygame.init()
#rectanglelist = [big()]
# Set the height and width of the screen
size = [700, 500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
black=(0,0,0)
end_it=False
time = 100
USEREVENT = 0
pygame.time.set_timer(USEREVENT+1, 10)
milliseconds = 0
seconds = 0
start_it = False
while (end_it==False):
screen.fill(black)
myfont=pygame.font.SysFont("Britannic Bold", 40)
nlabel=myfont.render("Welcome to "+ " Jet shooter ", 1, (255, 0, 0))
label=myfont.render("Click on the mouse to start ", 1, (255, 0, 0))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
end_it=True
screen.blit(nlabel,(200, 100))
screen.blit(label, (170,300))
pygame.display.flip()
while (start_it==False):
screen.fill(black)
myfont2=pygame.font.SysFont("Britannic Bold", 40)
label2=myfont2.render("Ready?", 1, (255, 0, 0))
screen.blit(label2, (300,250))
pygame.display.flip()
pygame.time.wait(3000)
start_it = True
fall = False
while (fall==False):
nlist = [3,2,1]
for i in (nlist):
screen.fill(black)
n = str(i)
myfont3=pygame.font.SysFont("Britannic Bold", 40)
score = myfont3.render(n,1,(255,0,0))
screen.blit((score), (350,250))
pygame.display.flip()
pygame.time.wait(1000)
screen.fill(black)
myfont4=pygame.font.SysFont("Britannic Bold", 40)
label4=myfont3.render("GOOO!!!", 1, (255, 0, 0))
screen.blit(label4, (300,250))
pygame.display.flip()
pygame.time.wait (1000)
fall = True
pause = 0
b = 0
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.KEYUP:
if event.key==K_p:
pause=True
if pause == True:
screen.fill(black)
font=pygame.font.SysFont("Britannic Bold", 40)
nlabelBB=myfont.render("Pause", 1, (255, 0, 0))
screen.blit(nlabelBB,(200, 100))
pygame.display.flip()
# Set the screen background
screen.fill(BLACK)
flip = 1
a = 0
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
recursive_draw(0, 0, 700, 500)
recursive_draw2(35,25, 625, 450)
**###I TRIED TO PUT THE PAUSE GAME HERE AND IF PRESSED P PAUSE AND S CONTINUE
while a == 0 :
if flip == 1 :
recursive_draw(35,25,625,450)
recursive_draw2(0, 0, 700, 500)
flip = flip + 1
pygame.display.flip()
if event.type == pygame.KEYUP:
if event.key==K_p:
a = 1
screen.fill(black)
font=pygame.font.SysFont("Britannic Bold", 40)
nlabelBB=myfont.render("Pause", 1, (255, 0, 0))
screen.blit(nlabelBB,(200, 100))
pygame.display.flip()
if event.key==K_s:
a = 0
if flip == 2 :
recursive_draw(0, 0, 700, 500)
recursive_draw2(35, 25, 625, 450)
flip = flip - 1
pygame.display.flip()
if event.type == pygame.KEYUP:
if event.key==K_p:
a = 1
screen.fill(black)
font=pygame.font.SysFont("Britannic Bold", 40)
nlabelBB=myfont.render("Pause", 1, (255, 0, 0))
screen.blit(nlabelBB,(200, 100))
pygame.display.flip()
if event.key==K_s:
a = 0**
if event.type == pygame.QUIT:
done = True
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(20)
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()
Just use a single game loop for everything and keep track of the current state (e.g. main menu, pause screen, game scene) of your game..
Here's an example where we keep track of the state by a simple variable called state and act in our game loop accordingly:
import pygame, math, itertools
def magnitude(v):
return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))
def sub(u, v):
return [u[i]-v[i] for i in range(len(u))]
def normalize(v):
return [v[i]/magnitude(v) for i in range(len(v))]
pygame.init()
screen = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()
path = itertools.cycle([(26, 43), (105, 110), (45, 225), (145, 295), (266, 211), (178, 134), (250, 56), (147, 12)])
target = next(path)
ball, speed = pygame.rect.Rect(target[0], target[1], 10, 10), 3.6
pause_text = pygame.font.SysFont('Consolas', 32).render('Pause', True, pygame.color.Color('White'))
RUNNING, PAUSE = 0, 1
state = RUNNING
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT: break
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_p: state = PAUSE
if e.key == pygame.K_s: state = RUNNING
else:
screen.fill((0, 0, 0))
if state == RUNNING:
target_vector = sub(target, ball.center)
if magnitude(target_vector) < 2:
target = next(path)
else:
ball.move_ip([c * speed for c in normalize(target_vector)])
pygame.draw.rect(screen, pygame.color.Color('Yellow'), ball)
elif state == PAUSE:
screen.blit(pause_text, (100, 100))
pygame.display.flip()
clock.tick(60)
continue
break
As you can see, the rectangle keeps moving until you press P, which will change the state to PAUSE; and a simple message will now be displayed instead of drawing/moving the rectangle further.
If you press S the state switches back to the normal mode; all done in a single game loop.
Further reading:
Pygame level/menu states
Mulitple Displays in Pygame
Trying to figure out how to track Pygame events and organize the game's functions