I was making a code where on clicking a button, a string will be chosen randomly from myList and be displayed. I am doing this using pygame module. The problem here is that the text does not remain, it just flashes for one frame.
Here's the code:
import pygame
import random
pygame.init()
size = (500, 400)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Project")
bg = pygame.image.load("bg.jpg")
# declaring variables and lists
white = (255, 255, 255)
black = (0, 0, 0)
light_grey = (224, 224, 224)
dark_grey = (200, 200, 200)
text = pygame.font.SysFont("Agency FB", 20)
myList = ["China", "Italy", "Russia", "India", "USA", "Canada", "France", "Japan", "Brazil", "Egypt"]
# text for button
button_text = text.render("Country", True, black)
rb = random.choice(myList)
font = pygame.font.SysFont("Agency FB", 50)
bFont = font.render(str(rb), True, white)
var = True
while var:
screen.blit(bg, (0, 0))
# store mouse position
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
var = False
# button click
if event.type == pygame.MOUSEBUTTONDOWN:
# pick one from list and blit
if 50 <= mouse[0] <= 50 + 75 and 350 <= mouse[1] <= 350 + 35:
screen.blit(bFont, (50, 100))
# make button
if 50 <= mouse[0] <= 50 + 75 and 350 <= mouse[1] <= 350 + 35:
pygame.draw.rect(screen, dark_grey, (50, 350, 75, 35), 0)
else:
pygame.draw.rect(screen, light_grey, (50, 350, 75, 35), 0)
screen.blit(button_text, (55, 355))
pygame.display.update()
How can I get the text to be there and not vanish in the next frame?
Do not create bFont before the main application loop, but initialize it with None:
bFont = None
Choose and render a random string when the button is pressed:
while var:
# [...]
for event in pygame.event.get():
# [...]
# button click
if event.type == pygame.MOUSEBUTTONDOWN:
# pick one from list and blit
button_rect = pygame.Rect(50, 350, 75, 35)
if button_rect.collidepoint(event.pos):
rb = random.choice(myList)
bFont = font.render(str(rb), True, white)
Draw the text in the main application loop if bFont is set:
var = True
while var:
# [...]
if bFont:
screen.blit(bFont, (50, 100))
pygame.display.update()
Complete example:
import pygame
import random
pygame.init()
size = (500, 400)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Project")
bg = pygame.image.load("bg.jpg")
# declaring variables and lists
white = (255, 255, 255)
black = (0, 0, 0)
light_grey = (224, 224, 224)
dark_grey = (200, 200, 200)
text = pygame.font.SysFont("Agency FB", 20)
myList = ["China", "Italy", "Russia", "India", "USA", "Canada", "France", "Japan", "Brazil", "Egypt"]
# text for button
button_text = text.render("Country", True, black)
button_rect = pygame.Rect(50, 350, 75, 35)
font = pygame.font.SysFont("Agency FB", 50)
bFont = None
var = True
while var:
# store mouse position
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
var = False
# button click
if event.type == pygame.MOUSEBUTTONDOWN:
# pick one from list and blit
if button_rect.collidepoint(event.pos):
rb = random.choice(myList)
bFont = font.render(str(rb), True, white)
screen.blit(bg, (0, 0))
# make button
if button_rect.collidepoint(mouse):
pygame.draw.rect(screen, dark_grey, (50, 350, 75, 35), 0)
else:
pygame.draw.rect(screen, light_grey, (50, 350, 75, 35), 0)
screen.blit(button_text, (55, 355))
if bFont:
screen.blit(bFont, (50, 100))
pygame.display.update()
Try using another boolean var
if event.type == pygame.MOUSEBUTTONDOWN:
if 50 <= mouse[0] <= 50 + 75 and 350 <= mouse[1] <= 350 + 35:
show_text = True
if show_text:
time_check = pygame.time.get_ticks ()
b_font = font.render (random.choice (my_list), True,(255,255,255))
screen.blit (b_font, (50, 100)
if time_check >= 2000: #2000 miliseconds
show_text = False
time_check = 0
Here it will display your text for two seconds
Related
when I use "rb = random.choice(myList) and bFont", it's function.
then I use "for i in range(len(myList)) and bFont" and click,
it cannot loop, just display a fix list value, why?
import pygame
import random
import re
pygame.mixer.init()
pygame.init()
size = (1280, 580)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("1")
bg = pygame.image.load("b")
white = (0, 0, 0)
black = (230, 0, 100)
light_grey = (224, 224, 224)
dark_grey = (200, 200, 200)
text = pygame.font.SysFont("Agency FB", 20)
myList = ["a5一个5ˈei", "am5是5æm", "are5是5a:r"]
font = pygame.font.Font('./simhei.ttf', 25)
button_text = font.render("aaaaa", True, black)
button_rect = pygame.Rect(70, 350, 200, 200)
bFont = None
var = True
while var:
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
var = False
if event.type == pygame.MOUSEBUTTONDOWN:
if button_rect.collidepoint(event.pos):
for i in range(len(myList)):
#rb = random.choice(myList)
bFont = font.render(myList[i], True, white)
bFont1 = font.render(myList[i], True, white)
#bFont = font.render(rb, True, white)
#bFont1 = font.render(rb, True, white)
screen.blit(bg, (0, 0))
if button_rect.collidepoint(mouse):
pygame.draw.rect(screen, dark_grey, (70, 350, 200, 200), 0)
else:
pygame.draw.rect(screen, light_grey, (70, 350, 200, 200), 0)
screen.blit(button_text, (90, 450))
if bFont:
screen.blit(bFont, (70, 100))
screen.blit(bFont1, (70, 220))
pygame.display.update()
\help me
\help
I am trying to make a score system so that whenever i click "j", the score in the top left of the screen, the score increases. It worked but the previous score stays on the screen, so to resolve this i decided to put a small Rect object over the score, then paint the new score over the Rect, but it is not working, please help. This is the function for the increasing score:
def score_increase():
global score
score += 1
font = pygame.font.SysFont("Comic Sans", 50, True, False)
surface = font.render(str(score), True, (130, 46, 75))
pygame.draw.rect(screen, ((35, 140, 120)), (0, 0, 50, 75))
screen.blit(surface, (0, 0))
This is the rest of my code if it helps:
import pygame, sys, random
from pygame.locals import *
pygame.init()
pygame.display.set_caption("waht da dog doin")
screen = pygame.display.set_mode((800, 600))
screen.fill((35, 140, 120))
dog = pygame.image.load("resources/dog.jpg").convert()
score = 0
font = pygame.font.SysFont("Comic Sans", 50, True, False)
surface = font.render(str(score), True, (130, 46, 75))
screen.blit(surface, (0, 0))
troll = ["corned", "balled", "chimkened", "torkelled", "kanaquined", "khazaqed", "bo burnam'd", "talal'd", "chammaked", "australiad", "lotto pie'd", "tetrised", "caca'd", "scrutinized", "bulbasaured"]
troll_item = random.choice(troll)
dog_y = random.randint(0, 400)
dog_x = random.randint(0, 500)
text_y = random.randint(0, 400)
text_x = random.randint(0, 500)
def score_increase():
global score
score += 1
font = pygame.font.SysFont("Comic Sans", 50, True, False)
surface = font.render(str(score), True, (130, 46, 75))
pygame.draw.rect(screen, ((35, 140, 120)), (0, 0, 50, 75))
screen.blit(surface, (0, 0))
def print_trolled():
screen.fill((35, 140, 120))
screen.blit(dog, (dog_x, dog_y))
font = pygame.font.SysFont("Comic Sans", 30, True, True)
surface_text = font.render("u have been" + " " + troll_item, True, (130, 46, 75))
screen.blit(surface_text, (text_x, text_y))
def start_print():
font = pygame.font.SysFont("Comic Sans", 50, True, False)
surface = font.render("press 'j' plsz", True, (130, 46, 75))
screen.blit(surface, (250, 250))
#def again_print():
font = pygame.font.SysFont("Comic Sans", 50, True, True)
surface = font.render("do it again", True, (130, 46, 75))
screen.blit(surface, (text_x, text_y))
start_print()
while True:
for event in pygame.event.get():
if event.type == KEYDOWN:
pass
if event.key == K_j:
print_trolled()
score_increase()
screen.blit(surface, (0, 0))
dog_y = random.randint(0, 400)
dog_x = random.randint(0, 500)
text_y = random.randint(0, 400)
text_x = random.randint(0, 500)
troll = ["corned", "balled", "chimkened", "torkelled", "kanaquined", "khazaqed", "bo burnam'd", "talal'd", "chammaked", "australiad", "lotto pie'd", "tetrised", "caca'd", "scrutinized", "bulbasaured"]
troll_item = random.choice(troll)
if event.type == QUIT:
sys.exit
if event.type == K_ESCAPE:
sys.exit
pygame.display.update()
You have to draw the scene in the application loop. Increment score and render a core_surface in score_increase
core_surface = None
def score_increase():
global score, score_surface
score += 1
font = pygame.font.SysFont("Comic Sans", 50, True, False)
score_surface = font.render(str(score), True, (130, 46, 75))
But redraw the entire scene in the a application loop in every frame:
while True:
for event in pygame.event.get():
if event.type == KEYDOWN:
pass
if event.key == K_j:
score_increase()
dog_y = random.randint(0, 400)
dog_x = random.randint(0, 500)
text_y = random.randint(0, 400)
text_x = random.randint(0, 500)
troll = ["corned", "balled", "chimkened", "torkelled", "kanaquined", "khazaqed", "bo burnam'd", "talal'd", "chammaked", "australiad", "lotto pie'd", "tetrised", "caca'd", "scrutinized", "bulbasaured"]
troll_item = random.choice(troll)
if event.type == QUIT:
sys.exit
if event.type == K_ESCAPE:
sys.exit
screen.fill(0)
start_print()
print_trolled()
score_surface = font.render(str(score), True, (130, 46, 75))
pygame.draw.rect(screen, ((35, 140, 120)), (0, 0, 50, 75))
if score_surface:
screen.blit(score_surface, (0, 0))
pygame.display.update()
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()
I'll start with simply copy and pasting my huge chunk of code:
# Libraries #
import pygame
from pygame.locals import *
import ResolutionMenu
# Classes #
class GuiSettings:
def __init__(self):
self.TextSize = 20
self.button_color = (35, 65, 145)
self.button_color_hover = (70, 105, 150)
class Button():
def __init__(self, x, y, width, height, outline, settings, text = "", action = None):
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
self.settings = settings
self.action = action
self.outline = outline
def draw(self, screen, outline = None):
if self.outline:
pygame.draw.rect(screen, self.outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
color = self.settings.button_color if not self.isOver(pygame.mouse.get_pos()) else self.settings.button_color_hover
pygame.draw.rect(screen, color, (self.x, self.y, self.width, self.height), 0)
if self.text != "":
font = pygame.font.SysFont('segoeuisemibold', self.settings.TextSize)
text = font.render(self.text, 1, (0, 0, 0))
screen.blit(text, (self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))
def update(self, events):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN and self.isOver(pygame.mouse.get_pos()) and self.action:
self.action()
def isOver(self, pos):
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
# Variables #
settings = GuiSettings()
Click = False
Clock = pygame.time.Clock()
H, W = ResolutionMenu.resolution
pygame.init()
pygame.display.set_caption("Main Menu")
font = pygame.font.SysFont(None, settings.TextSize)
screen = pygame.display.set_mode((ResolutionMenu.resolution))
# Functions #
def draw_text_center(text, font, colour, surface):
textobj = font.render(text, 1, colour)
textrect = textobj.get_rect()
textrect.center = ((H / 2), (W / 10))
surface.blit(textobj, textrect)
def draw_text(text, font, colour, surface, XandY):
textobj = font.render(text, 1, colour)
textrect = textobj.get_rect()
textrect.topleft = (XandY)
surface.blit(textobj, textrect)
def MainMenu():
while True:
if ResolutionMenu.resolution == (800, 600):
screen.fill ((0, 0, 0))
draw_text_center("Main Menu Navigation", font, (255, 255, 255), screen)
mx, my = pygame.mouse.get_pos()
button1 = pygame.Rect((W/10), (H/5), (W/3), (H/15))
button2 = pygame.Rect((W/10), (H/2), (W/3), (H/15))
if button1.collidepoint((mx, my)):
if Click:
game()
if button2.collidepoint((mx, my)):
if Click:
OptionsMenu()
pygame.draw.rect(screen, (255, 0, 0), button1)
pygame.draw.rect(screen, (255, 0, 0,), button2)
Click = False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
Click = True
pygame.display.update()
Clock.tick(60)
def game():
running = True
while running:
screen.fill((0,0,0))
draw_text('Test 1', font, (255, 255, 255), screen, (20, 20))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pygame.display.update()
Clock.tick(60)
def OptionsMenu():
screen = pygame.display.set_mode((800, 600))
settings = GuiSettings()
buttons = [
Button(100, 150, 200, 50, (0, 0, 0), settings, "Set Text Size: 14", lambda: settings.__setattr__('TextSize', 14)),
Button(500, 150, 200, 50, (0, 0, 0), settings, "Set Text Size: 16", lambda: settings.__setattr__('TextSize', 16)),
Button(100, 300, 200, 50, (0, 0, 0), settings, "Set Text Size: 18", lambda: settings.__setattr__('TextSize', 18)),
Button(500, 300, 200, 50, (0, 0, 0), settings, "Set Text Size: 20", lambda: settings.__setattr__('TextSize', 20))
]
running = True
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
for button in buttons:
button.update(events)
screen.fill((100, 100, 100))
for button in buttons:
button.draw(screen)
pygame.display.flip()
MainMenu()
So as you can see, recently I've been trying to make a main menu navigation panel for a game, and all is going well so far. I have stumbled into a problem, however.
The problem is thus: I have created a class of which holds the settings for text size, the button colour and the button hover colour (the colour of the button will change when the mouse pos is inside of the button's area).
class GuiSettings:
def __init__(self):
self.TextSize = 20
self.button_color = (35, 65, 145)
self.button_color_hover = (70, 105, 150)
This works completely fine when I'm inside of the OptionMenu() function, but when breaking that function, by pressing esc and returning to the MainMenu() function, the text size of the "Main Menu Navigation" text (Code below) doesn't change, despite using the 'settings.TextSize' as the size variable. I'm not sure why this is seeing as on the options menu uses a lambda function to change the value of the text size.
buttons = [
Button(100, 150, 200, 50, (0, 0, 0), settings, "Set Text Size: 14", lambda: settings.__setattr__('TextSize', 14)),
Button(500, 150, 200, 50, (0, 0, 0), settings, "Set Text Size: 16", lambda: settings.__setattr__('TextSize', 16)),
Button(100, 300, 200, 50, (0, 0, 0), settings, "Set Text Size: 18", lambda: settings.__setattr__('TextSize', 18)),
Button(500, 300, 200, 50, (0, 0, 0), settings, "Set Text Size: 20", lambda: settings.__setattr__('TextSize', 20))
]
Sorry that this has been such a long question, thank you for reading and I hope someone better at this than me can give me some help.
You create the font object before the main application loop with the font size settings.TextSize. This font object doesn't magically change when you change the settings.TextSize afterwards. The font size is used to rasterize the glyphs in the font. You need to create a new font object with a new size after the function OptionsMenu is left.
Furthermore you need to use the global statement, since font and settings are variables in global namespace.
def MainMenu():
global font # font is a variable in global namespace
while True:
if ResolutionMenu.resolution == (800, 600):
# [...]
if button2.collidepoint((mx, my)):
if Click:
OptionsMenu()
# create new font with new size
font = pygame.font.SysFont(None, settings.TextSize)
# [...]
def OptionsMenu():
global settings # settings is a variable in global namespace
screen = pygame.display.set_mode((800, 600))
# settings = GuiSettings() # <--- DELETE use the existing object instead
# [...]
When calling win.instructionScreen(screen, win), the instructions screen appears but the text from startScreen remains. Using screen.fill(BLACK) does not work because the main loop causes the text in startScreen to reappear, and using return to stop the startScreen method does not work.
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
class Game:
def __init__(self):
self.tickets = 0
def startScreen(self, screen, win):
titleText = pygame.font.SysFont('Showcard Gothic', 60)
subText = pygame.font.SysFont('Showcard Gothic', 20)
text = titleText.render("Our Game", True, WHITE)
cs = subText.render("Final Project", True, WHITE)
names = subText.render("Name 1, Name 2, Name 3, Name 4", True, WHITE)
screen.blit(text, [220, 200])
screen.blit(cs, [310, 265])
screen.blit(names, [150, 290])
mouse = pygame.mouse.get_pos()
if 493 > mouse[0] > 343 and 461 > mouse[1] > 411:
pygame.draw.rect(screen, RED, (343, 411, 150, 50))
else:
pygame.draw.rect(screen, GREEN, (343, 411, 150, 50))
buttonText = pygame.font.SysFont('Showcard Gothic', 30)
start = buttonText.render("Start!", True, WHITE)
screen.blit(start, [365, 425])
for event in pygame.event.get():
if 494 > mouse[0] > 343 and 461 > mouse[1] > 411:
if event.type == pygame.MOUSEBUTTONDOWN:
win.instructionScreen(screen, win)
pygame.display.update()
return
pygame.display.update()
def instructionScreen(self, screen, win):
background = pygame.image.load("background.png").convert()
screen.blit(background, [0, 0])
caption = pygame.image.load("caption.png").convert()
oak = pygame.image.load("oak.png").convert()
oak.set_colorkey(BLACK)
screen.blit(oak, [570, 130])
titleText = pygame.font.SysFont('Showcard Gothic', 60)
subText = pygame.font.SysFont('Showcard Gothic', 25)
text = titleText.render("Instructions", True, WHITE)
captionText = subText.render("Hey! Welcome to our game! Start by walking up", True, BLACK)
captionText2 = subText.render("to and playing Higher or Lower and racking up", True, BLACK)
captionText3 = subText.render("tickets. Then, when you get enough tickets,", True, BLACK)
captionText4 = subText.render("different games will be unlocked. Have fun!", True, BLACK)
screen.blit(text, [200, 80])
mouse = pygame.mouse.get_pos()
if 480 > mouse[0] > 325 and 550 > mouse[1] > 500:
pygame.draw.rect(screen, RED, (325, 500, 150, 50))
else:
pygame.draw.rect(screen, GREEN, (325, 500, 150, 50))
buttonText = pygame.font.SysFont('Showcard Gothic', 30)
screen.blit(caption, [3, 300])
pygame.draw.rect(screen, WHITE, (45, 320, 670, 110))
screen.blit(captionText, [45, 325])
screen.blit(captionText2, [45, 350])
screen.blit(captionText3, [45, 375])
screen.blit(captionText4, [45, 400])
play = buttonText.render("Play!", True, WHITE)
screen.blit(play, [357, 515])
#if 480 > mouse[0] > 325 and 550 > mouse[1] > 500:
# if event.type == pygame.MOUSEBUTTONDOWN:
# from gamescreen.py import gamescreen
pygame.display.update()
def clearScreen(self, screen):
screen.fill(WHITE)
def main():
pygame.init()
size = [800, 600]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Arcade City")
done = False
clock = pygame.time.Clock()
win = Game()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
done = True
win.startScreen(screen, win)
clock.tick(60)
if __name__ == "__main__":
main()
It's easier if you don't use nested functions. Call win.InstructionScreen() from the main function instead of from the method win.startScreen(). Use state variables to control the flow and return which state it should be from the methods.
win = Game()
current_screen = 'start'
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
done = True
if current_screen == 'start':
current_screen = win.startScreen(screen, win)
elif current_screen == 'instruction':
current_screen = win.instructionScreen(self, screen, win)
clock.tick(60)
This is just a mock-up, so you'll you have to change the methods after your own fittings. Put a return statement in both methods so they always return what the current screen should be.
TIP:
Instead of loading in your images and fonts every frame you could load them in the __init__ method and save them in a attribute variable.
EDIT:
To answer your actual question: You cannot stop an outer method to run. Think of it this way: You have a box in a room; can you get the box without entering the room? No. You have an inner method inside an outer method; can you get the inner method without entering the outer method? No.
If you only want the inner method to run without the outer method to be run, you have to call the inner method directly and not call the outer method.
I'm trying to code a game in python ( its a school assignment) and I want the game to have multiple screens, for example a title screen, an about screen, and the screen where you get to play the game. However, to keep things simple, I want all these screens to show up on the same window.
Currently, I've been defining commands that are running a specific screen, and telling python to run that command when the variables are correct (i.e. when the user has pressed the button to take them to the next screen). However, all I get is a blackscreen, and python immediately closes. The funny thing is that the log shows no sign of any problems, which is very strange.
If you could help me fix this code, it would be greatly appreciated.
import pygame
import random
name = 'Squash Ninja'
size = (700, 500)
rect_x = 50
rect_y = 50
rect_x1 = 345
rect_y1 = 397
rect_x_change = 10
rect_y_change = 10
rect_x_change1 = -10
rect_y_change1 = -10
vari = 0
#Colours#
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (18, 128, 13)
YELLOW = (234, 255, 0)
ORANGE = (255, 132, 0)
LGREEN = (0,255,0)
PURPLE = (204, 14, 166)
BLACK = (0, 0, 0)
#Starts Pygame#
pygame.init()
#Opens window#
size = (700, 500)
screen = pygame.display.set_mode(size)
#Running Variables#
done = False
title = True
about = False
#Screens#
def title():
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
print('User pressed mouse.')
elif event.type == pygame.KEYDOWN:
print("User pressed a key")
pygame.draw.line(screen, BLUE, [0,0], [100,100], 5)
elif event.type == pygame.KEYUP:
print("User let go of a key.")
elif event.type == pygame.QUIT:
done = True
screen.fill(BLUE)
pygame.draw.rect(screen,BLACK,[200,20,350,100],0)
pygame.draw.rect(screen,GREEN,[280,180,200,50],0)
pygame.draw.rect(screen,GREEN,[280,240,200,50],0)
pygame.draw.rect(screen,GREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 45, True, False)
text = font.render("Squash Ninja", True, LGREEN)
screen.blit(text, [255, 75])
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Play", True, YELLOW)
screen.blit(text, [350, 200])
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("About", True, YELLOW)
screen.blit(text, [340, 260])
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Quit", True, YELLOW)
screen.blit(text, [355, 320])
font = pygame.font.SysFont('Calibri', 10, True, False)
text = font.render("Version 1.2", True,WHITE)
screen.blit(text, [650, 470])
font = pygame.font.SysFont('Calibri', 10, True, False)
text = font.render("Created by Adrian Ngai", True,WHITE)
screen.blit(text, [600, 480])
font = pygame.font.SysFont('Calibri', 10, True, False)
text = font.render("Copyright 2014, all rights reserved.", True,WHITE)
screen.blit(text, [555, 490])
mouse = pygame.mouse.get_pos ()
if 280 + 200 > mouse [0] > 280 and 180+50 > mouse[1] > 180:
pygame.draw.rect(screen,LGREEN,[280,180,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Play", True, RED)
screen.blit(text, [350, 200])
if 280 + 200 > mouse [0] > 280 and 240+50 > mouse[1] > 240:
pygame.draw.rect(screen,LGREEN,[280,240,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("About", True, RED)
screen.blit(text, [340, 260])
if event.type == pygame.MOUSEBUTTONDOWN:
title = False
about = True
if 280 + 200 > mouse [0] > 280 and 300+50 > mouse[1] > 300:
pygame.draw.rect(screen,LGREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Quit", True, RED)
screen.blit(text, [355, 320])
if event.type == pygame.MOUSEBUTTONDOWN:
done = True
#bouncing rectangle#
pygame.draw.rect(screen, LGREEN, [rect_x, rect_y, 50, 50])
rect_x += rect_x_change
rect_y += rect_y_change
if rect_y > 450 or rect_y < 0:
rect_y_change = rect_y_change * -1
if rect_x > 650 or rect_x < 0:
rect_x_change = rect_x_change * -1
pygame.draw.rect(screen, PURPLE, [rect_x1, rect_y1, 50, 50])
rect_x1 += rect_x_change1
rect_y1 += rect_y_change1
if rect_y1 > 450 or rect_y1 < 0:
rect_y_change1 = rect_y_change1 * -1
if rect_x1 > 650 or rect_x1 < 0:
rect_x_change1 = rect_x_change1 * -1
for i in range(50):
x = random.randrange(0, 800)
y = random.randrange(0, 800)
pygame.draw.circle(screen,WHITE,[x,y],3)
pygame.display.flip()
clock.tick(20)
def about() :
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
print('User pressed mouse.')
elif event.type == pygame.KEYDOWN:
print("User pressed a key")
pygame.draw.line(screen, BLUE, [0,0], [100,100], 5)
if event.type == pygame.QUIT:
done = True
screen.fill(BLUE)
pygame.draw.rect(screen,BLACK,[200,20,350,100],0)
font = pygame.font.SysFont('Calibri', 45, True, False)
text = font.render("About", True, LGREEN)
screen.blit(text, [255, 75])
pygame.draw.rect(screen,GREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Back", True, YELLOW)
screen.blit(text, [355, 320])
mouse = pygame.mouse.get_pos ()
if 280 + 200 > mouse [0] > 280 and 300+50 > mouse[1] > 300:
pygame.draw.rect(screen,LGREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Back", True, RED)
screen.blit(text, [355, 320])
if event.type == pygame.MOUSEBUTTONDOWN:
title = True
about = False
pygame.display.flip()
clock.tick(20)
def done():
while not done:
print()
while done:
pygame.quit()
#Game Loop#
while vari == 0:
done()
while title:
title()
while about:
about()
#Closing Sequence#
print('Test fail :(')
pygame.quit()
I think your problem might be here
done = False
...
def done():
while not done:
print()
while done:
pygame.quit()
Even though you have the variable "done" assigned earlier, you're reassigning it to a function. Because done is no longer false, the function executes the second loop ("while done:") and immediately quits.
To get the behavior you're expecting, you should rename the function done() to something like is_done(). i.e.
def is_done():
while not done:
print()
while done:
pygame.quit()