Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
We're doing a "virus" now and I was told to make a delay in my code. Help me, please. Here's the code:
import pygame
from tkinter import *
from tkinter import messagebox
import time
import random
pygame.init()
screen = pygame.display.set_mode((800, 300))
pygame.display.set_caption("ЗАРАЖЕНИЕ АЧЕ)")
Tk().wm_withdraw()
font = pygame.font.SysFont("Lucida Console", 35)
label2 = font.render("DELETING ALL PASSWORDS. . . . .", 1, (12, 140, 0, 1))
a = 0
while True:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
time.sleep(0.5)
screen = pygame.display.set_mode((800, 300))
pygame.display.set_caption("ЗАРАЖЕНИЕ АЧЕ)")
messagebox.showerror("ERROR!", " Вы попытались закрыть окно! Начинаю удаление файлов")
warning = f"Процент удаления: {a}"
label3 = font.render(warning, 1, (12, 140, 0, 1))
a = a + 1
time.sleep(0.1)
if a >= 100:
break
screen.blit(label2, (50, 50))
screen.blit(label3, (50, 100))
pygame.display.update()
My question is, I don't know which command to use to delay and where to put it.
Thank you in advance!
With pygame, you don't use time.sleep, you need to replace your time.sleep code with pygame.time.wait, which is written in milliseconds, so your updated code would look like so:
import pygame
from tkinter import *
from tkinter import messagebox
import time
import random
pygame.init()
screen = pygame.display.set_mode((800, 300))
pygame.display.set_caption("ЗАРАЖЕНИЕ АЧЕ)")
Tk().wm_withdraw()
font = pygame.font.SysFont("Lucida Console", 35)
label2 = font.render("DELETING ALL PASSWORDS. . . . .", 1, (12, 140, 0, 1))
a = 0
while True:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.time.wait(500)
screen = pygame.display.set_mode((800, 300))
pygame.display.set_caption("ЗАРАЖЕНИЕ АЧЕ)")
messagebox.showerror("ERROR!", " Вы попытались закрыть окно! Начинаю удаление файлов")
warning = f"Процент удаления: {a}"
label3 = font.render(warning, 1, (12, 140, 0, 1))
a = a + 1
pygame.time.wait(100)
if a >= 100:
break
screen.blit(label2, (50, 50))
screen.blit(label3, (50, 100))
pygame.display.update()
Also with your code I replaced pygame.quit() with pygame.display.quit() so the code will continue after the so called "error".
Related
I am trying to make a button in python and I attempted to use the Pygame_widgets library but my functions will not execute
import basic
import pygame as pg
import pygame_widgets as pw
from pygame_widgets.button import Button
def startWindow():
pg.init()
win = pg.display.set_mode((600, 600))
button = Button(
win, 100, 100, 300, 150, text='Hello',
fontSize=50, margin=20,
inactiveColour=(255, 0, 0),
pressedColour=(0, 255, 0), radius=20,
onClick=lambda: basicKit = True
)
run = True
while run:
events = pg.event.get()
for event in events:
if event.type == pg.QUIT:
pg.quit()
run = False
quit()
win.fill((255, 255, 255))
button.listen(events)
button.draw()
pg.display.update()
if __name__ == '__main__':
stop = False
print("* Welcome To \"Drum Easy\" V 1.0 *")
startWindow()
while stop == False:
kit = input("\nWhat Kit? \n\nBasic\n ^\n 1\n\n>")
if kit == "1":
basic.Kit()
stop = True
else: print('Invalid Kit')
see now in the button = Button part of the code has multiple options that can run functions but none of them seem to work, they all seem to have invalid syntax
A lambda must have an expression, not a statement (assignment, if,
while, ...). So add = lambda x: x + 1 is a valid lambda. But
lambda: basicKit = True is syntactically incorrect. You can instead
define a function that updates your variable and gives this
function to the Button constructor.
Something like that:
def update_basic_kit():
global basicKit
basicKit = True
button = Button(
win, 100, 100, 300, 150, text='Hello',
fontSize=50, margin=20,
inactiveColour=(255, 0, 0),
pressedColour=(0, 255, 0), radius=20,
onClick=update_basic_kit
)
Here's the code, i'm working on Atom. I've been using pygame for a lot of time, and that rarely happens. I never knew what the problem was, but i never needed to, until now.
import pygame
from random import randint as rnd
from colorama import Cursor
import math
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (600,90)
pygame.init()
xsize, ysize = 700, 700
screen = pygame.display.set_mode((xsize, ysize))
screen.fill((0, 0, 0))
def dis(p1, a1):
return math.sqrt((p1[0] - a1[0])**2 + (p1[1] - a1[1])**2)
inner = 0
tot = 0
pygame.draw.circle(screen, (255, 255, 255), (350, 350), 350, 2)
while True:
pos = (rnd(0, xsize), rnd(0, ysize))
if dis((350, 350), pos) < 350:
color = (50, 255, 50)
inner += 1
else:
color = (50, 50, 250)
tot += 1
pygame.draw.circle(screen, color, pos, 2)
print(" pi = " + str(4 * inner / tot) + "\nnº dots: " + str(tot), Cursor.UP(2))
pygame.display.flip()
The window freeze, because you do not handle the events. You have to handle the events by either pygame.event.pump() or pygame.event.get(), to keep the window responding.
Add an event loop, for instance:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# [...]
This question already has answers here:
How can I create a text input box with Pygame?
(5 answers)
Closed 5 years ago.
I'm doing a project that using Python and Pygame. I want to ask some question about the key in Pygame.
Thank you Shubhitgarg. I use his/her suggestions(use pygame_textinput), it succeed. Now I want to change the text input position with pressed an enter, but it can't detect the enter. How can I fix it? My real code:
# installing package
import pygame
from pygame import *
import pygame_textinput
pygame.init()
# colour settings
red = (255, 0, 0)
green = (0, 255, 0)
grass_green = (112, 173, 71)
blue = (0, 0, 255)
yellow = (255, 255, 0)
white = (255, 255, 255)
black = (0, 0, 0)
# screen settings
window = pygame.display.set_mode((680, 600))
window.fill(grass_green)
pygame.display.flip()
# font settings
default_font = pygame.font.get_default_font()
big_font = pygame.font.Font(default_font, 96)
font_a = pygame.font.Font(default_font, 50)
font_b = pygame.font.Font(default_font, 30)
font_c = pygame.font.Font(default_font, 18)
# text input settings
textinput = pygame_textinput.TextInput("freeansbold.ttf", 96, True, black, white, 400, 35)
# timer
start = pygame.time.get_ticks()
# text
please_guess_a_number = font_a.render("Please guess a number. ", 1, white)
text_range = font_c.render("from to", 1, white)
wrong_bigger = font_b.render("Sorry, You haven’t guess it rightly. Please try again.(The answer must be bigger.)", 1, white)
wrong_smaller = font_b.render("Sorry, You haven’t guess it rightly. Please try again.(The answer must be smaller.)", 1, white)
correct = font_b.render("Congratulations! You guess it correctly!!", 1, white)
# game
ask_you_to_set_range_1 = True
ask_you_to_set_range_2 = False
while True:
if ask_you_to_set_range_1:
window.blit(please_guess_a_number, (60, 80))
pygame.draw.rect(window, yellow, [60, 200, 230, 300])
pygame.draw.rect(window, yellow, [390, 200, 230, 300])
window.blit(text_range, (10, 550))
pygame.display.flip()
while True:
events = pygame.event.get()
textinput.update(events)
window.blit(textinput.get_surface(), (110, 300))
pygame.display.flip()
if ask_you_to_set_range_2:
while True:
events = pygame.event.get()
textinput.update(events)
window.blit(textinput.get_surface(), (440, 300))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if ask_you_to_set_range_1 and event.key == pygame.K_KP_ENTER:
ask_you_to_set_range_1 = False
ask_you_to_set_range_2 = True
if textinput.update(event):
num1 = textinput.get_text()
text1 = big_font.render(num1, 1, black)
window.blit(text1, (110, 300))
Can anyone teach me to solve it?
You see this https://github.com/ShubhitGarg/pygame-text-input .
You can import this and use the text_input class and blit it on the same or next page
Use this
textinput = pygame_textinput.TextInput()
while True:
screen.fill((225, 225, 225))
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
# Feed it with events every frame
textinput.update(events)
# Blit its surface onto the screen
screen.blit(textinput.get_surface(), (10, 10))
pygame.display.update()
I am a beginner for learning Python and I just ran into a problem when I debug a series of codes I copied from a book, and I get a white screen window instead of one with pattern, here is the code:
import pygame, sys, random
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
pygame.init()
windowWidth = 640
windowHeight = 480
surface = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption('Pygame Shapes!')
while True:
surface.fill((200, 0, 0))
pygame.draw.rect(surface, (255, 0, 0), (random.randint(0, windowWidth), random.randint(0, windowHeight), 10, 10))
greenSquareX = windowWidth / 2
greenSquareY = windowHeight / 2
while True:
surface.fill((0, 0, 0))
pygame.draw.rect(surface, (0, 255, 0),
(greenSquareX, greenSquareY, 10, 10))
greenSquareX += 1
# greenSquareY += 1
pygame.draw.rect(surface, (0, 0, 255),
(blueSquareX, blueSquareY, 10, 10))
blueSquareX = 0.0
blueSquareY = 0.0
blueSquareVX = 1
blueSquareVy = 1
while True:
surface.fill((0, 0, 0))
pygame.draw.rect(surface, (0, 0, 255),
(blueSquareX, blueSquareY, 10, 10))
blueSquareX += blueSquareVX
blueSquareY += blueSquareVY
blueSquareVX += 0.1
blueSquareVY += 0.1
for event in GAME_EVENTS.GET():
if event.type == GAME_GLOBALS.QUIT:
pygame.quit()
sys.exit()
pygame.dispaly.update()
And the first error I got was: E1101:Module 'pygame' has no 'init' member'(4 ,1). I have ran other Python code with pygame.init() before, and the result turned out well; but this time I get a white screen window. What is wrong with my code? Thanks for help!!
This took me a great deal of debugging to fix, haha.
First of all, you are not escaping or updating in any of your while loops. Once the code enters your first while loop, nothing (the pygame.display) ever updates after that – resulting in your white-screen syndrome.
In addition, please name your variables consistently and check for typos in your code. You created a blueSquareVy variable only to try to refer to it as blueSquareVY later, and you misspelled pygame.display.update() at the end of the code. Capitalization matters – and that's just a few of the typos!
There are also logical errors within the code. You should not fill your window surface after your graphics have been drawn onto the screen. Looking at your variables, it seems that you would like the little squares to move. You would create the position variables outside of the while loop, as if you create them within the loop, they get recreated at their initial value every iteration of the loop.
The annotated and bugfixed code:
import pygame, sys, random
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
pygame.init()
windowWidth = 640
windowHeight = 480
surface = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption('Pygame Shapes!')
# Renamed variables to be consistent
# Moved variables out of the while loop
greenSquareX = windowWidth / 2
greenSquareY = windowHeight / 2
blueSquareX = 0.0
blueSquareY = 0.0
blueSquareVX = 1
blueSquareVY = 1
# Joined the two while loops into one
while True:
surface.fill((200, 0, 0))
pygame.draw.rect(surface, (255, 0, 0), (random.randint(0, windowWidth), random.randint(0, windowHeight), 10, 10))
surface.fill((0, 0, 0))
pygame.draw.rect(surface, (0, 255, 0),
(greenSquareX, greenSquareY, 10, 10))
greenSquareX += 1
greenSquareY += 1
pygame.draw.rect(surface, (0, 0, 255),
(blueSquareX, blueSquareY, 10, 10))
blueSquareX += blueSquareVX
blueSquareY += blueSquareVY
blueSquareVX += 0.1
blueSquareVY += 0.1
# Do not capitalize the .get() method for pygame.event class
for event in GAME_EVENTS.get():
if event.type == GAME_GLOBALS.QUIT:
pygame.quit()
sys.exit()
# Misspelled pygame.display
pygame.display.update()
Hope this helped!
So, I am in the middle of making two, very similar games (Exact details are irrelevant right now.)
Anyway, I need to know where to insert my "pygame.time.wait" code.
The exact code I need looks like this:
pygame.time.wait(100)
score = score + 1
Currently, my code looks like so:
import sys, pygame
from pygame.locals import *
pygame.init()
size = width, height = 800,500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("One Score (Created by - Not Really Working Lamp Productions:)")
WHITE = (255,255,255)
score = 0
screen.fill (WHITE)
myfont = pygame.font.SysFont("monospace", 16)
scoretext = myfont.render("Score = "+str(score), 1, (0,0,0))
screen.blit(scoretext, (5, 10))
disclaimertext = myfont.render("Copyright, 2013, Not Really Working Lamp Productions.", 1, (0,0,0))
screen.blit(disclaimertext, (5, 480))
while 1:
for event in pygame.event.get():
pygame.display.flip()
if event.type == pygame.QUIT:sys.exit()
The problem is though, that I don't know WHERE in this code I should put my pygame.time.wait code. Could anybody fill me in? (I think it goes in the "while 1:" loop, but I don't know where.)
Good afternoon. You should put your 'wait code' into the body of FOR circle:
while 1:
for event in pygame.event.get():
pygame.display.flip()
if event.type == pygame.QUIT:sys.exit()
pygame.time.wait(100)
score = score + 1