So when I set
while not game_over = False
, my window opens properly, but the text doesn't show. Whereas if I set it to True, the text shows but window closes immediately.
Here's the code:
***#Write a Python program to create a simple math quiz.
#Importing libraries
import pygame
import sys
import math
from pygame.locals import *
#Initialising fonts
pygame.init()
pygame.font.init()
#Assigning variables
WIDTH = 600
HEIGHT = 600
#Other important things
background_color = (127,255,0)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
pygame.display.set_caption("Math Game")
screen.fill(background_color)
pygame.display.flip()
font = pygame.font.Font("freesansbold.ttf", 40)
textX=10
textY=10
def show_text(x,y):
text=font.render("Math Game", True, (0,0,0))
screen.blit(text,(x,y))
"""
#Adding the Calculator bit
x = input("Enter Your First Nummber To Add: ")
y = input("Enter Your Second Number To Add: ")
z = str(int(x)+int(y))
print(z)
"""
#Game loop
game_over = True
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
show_text(textX, textY)
pygame.display.update()***
It is a matter of Indentation. show_text and pygame.display.update() must be called in the application loop instead of after the application loop.
Additionally you should clear the display in the application loop.
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# INDENTATION
#-->|
screen.fill(background_color)
show_text(textX, textY)
pygame.display.update()
The typical PyGame application loop has to:
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()
limit the frames per second to limit CPU usage with pygame.time.Clock.tick
Related
I keep getting an error about system not being initialised. The error is about the pygame.display.update()
Where is this meant to go?
import pygame #IMPORTS THE PYGAME CLASSES, METHODS AND ATTRIBUTES
from pygame.locals import * #IMPORTING ALL PYGAME MODULES
pygame.init() #INITIALISING PYGAME
WIDTH, HEIGHT = 1000, 600
WINDOW = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("On The Run")
blue = 146,244,255 #BACKGROUND COLOUR
width = 80
height = 60
x = 200 #X-POSITION OF THE CHARACTER
y = 100 #Y-POSITION OF THE CHARACTER
player1 = pygame.image.load('assets/characterMove3.jpg') #DISPLAYING THE IMAGE ONTO THE SCREEN
player1 = pygame.transform.scale(player1,(width,height)) #SCALING THE IMAGE TO SUITABLE DIMENSIONS
WINDOW.blit(player1,(x,y))
def game_loop(): #WHERE THE WINDOW IS CREATED
run = True
while run:
WINDOW.fill(blue)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False #WE WILL QUIT THE GAME AS THE VARIABLE run IS NOW FALSE
pygame.quit() #IT WON'T SHOW THE MOST RECENT THING I DREW UNLESS I MANUALLY UPDATE IT
pygame.display.update()
game_loop()
The problem is that pygame.quit() is called in the application loop. pygame.quit() deinitializes all Pygame modules and crashes all subsequent Pygame API calls. pygame.quit() must be the very last Pygame API call. Call pygame.quit() after the application loop:
def game_loop(): #WHERE THE WINDOW IS CREATED
run = True
while run:
WINDOW.fill(blue)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#pygame.quit() <-- DELETE
pygame.display.update()
pygame.quit() # <-- INSERT
game_loop()
I cannot find, what is wrong with background, even with debugger it is not doing nothing
#importing
from turtle import width
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
#game start
if __name__ == "__main__":
main()
You have to call draw_window in the application loop:
#importing
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
pygame.quit()
#game start
if __name__ == "__main__":
main()
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()
When you write a function, it is just some instructions on memory, it doesn't run unless you call it. This is the case. You write a set of instructions, draw_window, but you never call it. What you need to do is before the game loop, call draw_window. So the code would be something like this.
#importing
from turtle import width
import pygame
#resolution
pygame.display.set_caption("Tanks")
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#background color (my problem)
def draw_window():
WIN.fill((255, 0, 0))
pygame.display.update()
#main functions
def main():
run = True
while run:
draw_window()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
#game start :)
if __name__ == "__main__":
main()
This way the result will be a red window which is rgb(0, 0).
The screen pops up with the green rectangle but I can't move it.
I don't understand why it won't work. any help would be highly appreciated!
import pygame
pygame.init()
width=1800
height=900
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
white=(0,0,0)
black=(255,255,255)
x=800
y=150
running = True
def main():
global running, screen, x, y
screen = pygame.display.set_mode((width,height))
pygame.draw.rect(screen, green,(x,y,100,300))
pygame.display.update()
while running:
ev = pygame.event.get()
for event in ev:
if event.type==pygame.KEYDOWN:
if event.type==pygame.K_RIGHT:
x+=20
pygame.display.update()
if event.type==pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
main()
You have to draw the object in the application not just before the application loop. See My program will run, but the K_RIGHT command will not work:
import pygame
pygame.init()
width, height = 1800, 900
x, y = 800, 150
def main():
global running, screen, x, y
screen = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()
running = True
while running:
clock.tick(100)
ev = pygame.event.get()
for event in ev:
if event.type==pygame.QUIT:
running = False
if event.type==pygame.KEYDOWN:
if event.type==pygame.K_RIGHT:
x+=20
screen.fill("black")
pygame.draw.rect(screen, "green", (x,y,100,300))
pygame.display.update()
pygame.quit()
exit()
main()
The typical PyGame application loop has to:
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()
limit frames per second to limit CPU usage with pygame.time.Clock.tick
I wanted to do something after a period of time. On stack overflow I found a question that helps solve that, (Link) but when I run the program the code works, however it goes away after a millisecond. Whereas I want it to stay there after the amount of time I want it to wait. In this case for a test run I am blitting some text onto the screen. Here is the code:
import pygame
# Importing the modules module.
pygame.init()
# Initializes Pygame
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((800, 600))
# Sets the screen to pygame looks and not normal python looks.
pygame.display.set_caption("Test Run")
# Changes the title
# Heading
headingfont = pygame.font.Font('Bouncy-PERSONAL_USE_ONLY.otf', 45)
headingX = 230
headingY = 10
class Other():
def show_heading():
Heading = headingfont.render("Health Run!", True, (255, 255, 255))
screen.blit(Heading, (headingX, headingY))
pygame.time.set_timer(pygame.USEREVENT, 100)
running = True
while running:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.USEREVENT:
Other.show_heading()
#Update Display
pygame.display.update()
If you want to draw the text permanently, you need to draw it in the application loop. Set a Boolean variable "draw_text" when the timer event occurs. Draw the text depending on draw_text in the application loop:
draw_text = False
running = True
while running:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.USEREVENT:
draw_text = True
if draw_text:
Other.show_heading()
#Update Display
pygame.display.update()
For more information about timers and timer events, see Spawning multiple instances of the same object concurrently in python, How can I show explosion image when collision happens? or Adding a particle effect to my clicker game and many more.
I am making a text based RPG as my first python/pygame project. In my game I would like to present the player with choices then ask them to type it in. I was able to download and import a module which accepts user input and displays it on the screen. However, before using it for anything like..lets say, if the user input is 'yes', then they go to a new area, I'd like the program to only accept the user input once they hit the enter key. I believe the tutorial for the textinput module I downloaded has directions to do this, but to be honest I just don't understand what it's saying. I've tried multiple types of loops but nothing is happening. Any help will be appreciated. Here is my main game code:
import pygame_textinput
import pygame
pygame.init()
#fps
clock=pygame.time.Clock()
# create font here
font_name = pygame.font.get_default_font()
WHITE_TEXT_COLOR = (255, 255, 255)
# create screen and window and display and font here
screen_width, screen_height = 800, 700
background_color_black = (0, 0, 0)
screen = pygame.display.set_mode((screen_width, screen_height))
our_game_display = pygame.Surface((screen_width, screen_height))
pygame.display.set_caption('MyRPGGame')
#create text input object
textinput = pygame_textinput.TextInput()
def draw_text(text, size, x, y):
pygame.font.init()
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE_TEXT_COLOR)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
our_game_display.blit(text_surface, text_rect)
def choose_to_play():
draw_text("You've decided to play",20,screen_width/2,screen_height/2+50)
def first_area():
our_game_display.fill(background_color_black)
draw_text('The story of this game depends on your choices. Do you wish to play?', 20, screen_width / 2,screen_height / 2 - 100)
draw_text('Type your answer and hit enter.', 20, screen_width / 2,screen_height / 2 - 50)
draw_text('Yes', 20, screen_width/2,screen_height/2+50)
draw_text('No', 20, screen_width/2,screen_height/2+100)
screen.blit(our_game_display, (0, 0))
pygame.display.update()
while True:
our_game_display.fill((background_color_black))
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
first_area()
# Feed it with events every frame
textinput.update(events)
# Blit its surface onto the screen
screen.blit(textinput.get_surface(), (10, 600))
pygame.display.update()
clock.tick(30)
Now here is the source for the pygame textinput module, figured I would link to the code so this post isn't too crowded:
https://github.com/Nearoo/pygame-text-input
You need to a variable for the state of the game. Once enter is pressed change the state. Implement different cases in the application loop depending on the state of the game::
game_state = 'start'
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
game_state = 'input'
our_game_display.fill((background_color_black))
if game_state == 'input':
textinput.update(events)
# [...]