My Python thread starts but doesn't do anything - python

I'm working on custom basic functions to simplify coding for me, like wait(seconds) or msg(...), and now I'm working on the window setup and updating, it works, but when I put it in a thread, it just won't do anything. I don't get any errors, so I'm confused and frustrated. I dont need you to debug it or anything, I just need help to know where the problem is and why it's a problem.
Here's my script so far (the script is at the bottom):
# Imports
if True:
import pygame, math, random, time, sys, threading
from pygame.locals import *
pygame.init()
# Setup
if True:
win_n = "New Project"
win_w = 800
win_h = 600
win_c = (0, 0, 0)
# Code
if True:
def wait(seconds):
time.sleep(seconds)
def wait_until(bool):
while not bool:
wait(0.001)
# Execute
if True:
def e_ws():
mainClock = pygame.time.Clock()
pygame.display.set_caption(win_n)
monitor_size = [pygame.display.Info().current_w, pygame.display.Info().current_h]
screen = pygame.display.set_mode((win_w, win_h), pygame.RESIZABLE)
fullscreen = False
while True:
screen.fill(win_c)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == VIDEORESIZE:
if not fullscreen:
screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
if event.type == KEYDOWN:
if fullscreen:
screen = pygame.display.set_mode(monitor_size, pygame.FULLSCREEN)
else:
screen = pygame.display.set_mode((screen.get_width(), screen.get_height()),
pygame.RESIZABLE)
pygame.display.update()
mainClock.tick(60)
t_ws = threading.Thread(target=e_ws)
t_ws.start()
print("done")

Run a script with python yourscriptname.py from the command line.

Related

How can I make a function for summoning an object in pygame? [duplicate]

I tried to use python to display image:
import pygame
win = pygame.display.set_mode((500, 500))
DisplayImage("Prologue.jpg", win)
And when it runs, nothing happened. It also happened for
DisplayImage("Streets.jpg", win)
However, when I tried the exact same thing later on in the code, it ran perfectly.
I checked, the picture was in the same folder as the .py file, and I didn't type the name wrong.
The function is:
def DisplayImage(imageName, screen):
screen.fill((0, 0, 0))
Image = pygame.image.load(imageName).convert()
screen_rect = screen.get_rect()
Image_rect = Image.get_rect().fit(screen_rect)
Image = pygame.transform.scale(Image, Image_rect.size)
screen.blit(Image, [0, 0])
pygame.display.update()
Update:
I commented out all of the lines and copy and pasted that line out so it's the only line that runs. It runs perfectly.
Update 2:
Found the issue. The reason it doesn't work was that the pygame window was "not responding". I don't know what caused it to not respond, but in one of the test runs I didn't make it show "not responding", and the images were loaded fine. The "not responding" always shows up when I type in my player name, and the function looks like this:
def createName():
playerName = input("Enter the player name\n")
desiredName = input("Is "+playerName+" the desired name?[1]Yes/[2]No\n")
if desiredName == "1":
return playerName
elif desiredName == "2":
playerName = createName()
Sometimes when I type the player name nothing happens, and the letters only show up after a while. If this happens, the pygame window is bound to not respond.
You cannot use input in the application loop. input waits for an input. While the system is waiting for input, the application loop will halt and the game will not respond.
Use the KEYDOWN event instead of input:
run = True
while run:
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if pygame.key == pygame.K_1:
# [...]
if pygame.key == pygame.K_2:
# [...]
Another option is to get the input in a separate thread.
Minimal example:
import pygame
import threading
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
color = "red"
def get_input():
global color
color = input('enter color (e.g. blue): ')
input_thread = threading.Thread(target=get_input)
input_thread.start()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window_center = window.get_rect().center
window.fill(0)
pygame.draw.circle(window, color, window_center, 100)
pygame.display.flip()
pygame.quit()
exit()

I'm very new to python and one of my images will not draw onto the screen

I was trying to draw an image onto the screen but I keep getting errors. The error keeps saying "video system not initialized". I am new to python, can anyone help?
import pygame
import os
#game window
WIN = pygame.display.set_mode((1000, 800))
NAME = pygame.display.set_caption("Space War!")
#FPS limit
FPS = (60)
#image i am trying to draw onto screen
SPACE_BACKGROUND = pygame.image.load(os.path.join('space_background.png'))
pygame.init()
#allows pygame to quit
def main():
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#updates images
pygame.display.update()
#calling def main(): function
if __name__ == "__main__":
main()
do:
def main():
clock = pygame.time.Clock()
run = True
FPS = 60
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#updates images
WIN.blit(SPACE_BACKGROUND, (0, 0)) #you FORGOT this part
pygame.display.update()
BTW the main function cannot access the global 'FPS' variable so declare that within the 'main' function(make it local).

I get a pygame error after a quit my pygame window

I am trying to understand how to display fonts in pygame. But i get an error saying pygame.error: Library not initialized
This error happens after i press the cross button or quit my pygame window.
Can anyone tell my why this error is happening and how can i fix it please?
import pygame
from pygame.locals import *
import sys
from win32api import GetSystemMetrics
pygame.init()
WIDTH = GetSystemMetrics(0)
HEIGHT = GetSystemMetrics(1)-64
WIDTH_HEIGHT = (WIDTH, HEIGHT)
WINDOW = pygame.display.set_mode(WIDTH_HEIGHT)
pygame.init()
font = pygame.font.Font('freesansbold.ttf', 32)
text = ""
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
running = False
text = font.render('Hi', True, (255,255,255))
WINDOW.blit(text, (0, 0))
pygame.display.update()
This is one way to exit (just finishes the program):
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
This is another way (a bit more forceful):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()

Why is my Pygame window only displaying for only a few seconds?

I'm new to PyGame (and python in general) and I am just trying to get a window to pop up. That's all I'm after for now. Here's my code:
import pygame
pygame.init()
win = pygame.display.set_mode((600, 600))
pygame.display.set_caption('First Game')
I am using Python 3.7.0 in Pycharm and PyGame 1.9.4.
Following Python's PyGame tutorial, your next step is to start a while loop to handle the game frames:
import pygame
pygame.init()
win = pygame.display.set_mode((600, 600))
pygame.display.set_caption('First Game')
clock = pygame.time.Clock() # Determine FPS (frames-per-second)
crashed = False
# Game loop
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
pygame.display.update()
clock.tick(60)
Because you need to first put it inside a loop (so it refreshes whatever is inside it), that way it stays on until something alters the condition of that loop.
# Event loop (HERE YOU PUT IT).
while 1:
for event in pygame.event.get():
if event.type == QUIT:
return
screen.blit(background, (0, 0))
pygame.display.flip()
if __name__ == '__main__': main()

PyGame: Nothing Happens

I am making a game in pygame and when I go to run the game nothing is happening. A black box appears but does nothing at all there is no display etc. Also what bugs me is the fact the Python Shell is not displaying any errors at all. Here is the code for the main file:
import pygame
import sys
import random
import pygame.mixer
import math
from constants import *
from player import *
class Game():
def __init__(self):
#States (Not country states)
self.game_state = STATE_INGAME
#State variables
#self.stateMenu =
#Screen
size = SCREEN_WIDTH, SCREEN_HEIGHT
self.screen = pygame.display.set_mode(size)
pygame.display.set_caption('WIP')
self.screen_rect = self.screen.get_rect()
# Player
self.player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)
def run(self):
clock = pygame.time.Clock()
if self.game_state == STATE_INGAME:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.player_move()
self.player.update()
self.player.render(self.screen)
clock.tick(100)
def player_move(self):
# move player and check for collision at the same time
self.player.rect.x += self.player.velX
self.player.rect.y += self.player.velY
Game().run()
I have checked the player file many times and there are NO errors in there at all. Well not from what I can see. Thanks for the help!
You need a while-loop:
def run(self):
clock = pygame.time.Clock()
while True:
if self.game_state == STATE_INGAME:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
self.player_move()
self.player.update()
self.player.render(self.screen)
clock.tick(100)

Categories