Pygame "Not Responding" [duplicate] - python

This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Why is my PyGame application not running at all?
(2 answers)
How to wait some time in pygame?
(3 answers)
Closed 2 days ago.
I'm trying to add a score to my program, But when I run my code a black screen appears and it says that the app isn't responding. Without the score function it runs properly.
Here is my code:
def score_add():
global score
wait(1)
score = score + 1
while not Game_Over:
score_add()
text = font.render("Score: " + str(score), True, (0, 0, 0))
screen.blit(text, (10, 10))
pygame.display.flip()
I've tried removing pygame.display.flip() Same result.

Related

Python random module shuffle keeps returning same output [duplicate]

This question already has answers here:
random.choice() returns same value at the same second, how does one avoid it?
(3 answers)
random.randint(2, 12) returns same results every time it's run in Python
(5 answers)
Closed 2 years ago.
I am trying to get a random pair of colors from a Python list. Here is my code so far:
from random import *
color_choices = ['#FAACA8', '#DDD6F3', '#21D4FD', '#B721FF', '#08AEEA', '#2AF598', '#FEE140', '#FA709A', '#8EC5FC', '#E0C3FC', '#FBAB7E', '#F7CE68', '#D9AFD9', '#97D9E1', '#FBDA61', '#FF5ACD', '#F76B1C']
shuffle(color_choices)
print(color_choices)
The problem is the it keeps returning the same "random" order on each run of the program
['#21D4FD', '#FBDA61', '#B721FF', '#F76B1C', '#FBAB7E', '#DDD6F3', '#E0C3FC', '#2AF598', '#F7CE68', '#FF5ACD', '#FEE140', '#FA709A', '#8EC5FC', '#08AEEA', '#FAACA8', '#97D9E1', '#D9AFD9']
I ran the program three times. It cannot be a coincidence that the shuffle will return all colors in the exact same order three times.
What am I missing?
Thanks.

How to jump on platforms in pygame [duplicate]

This question already has answers here:
How do I detect collision in pygame?
(5 answers)
How to detect collisions between two rectangular objects or images in pygame
(1 answer)
Closed 2 years ago.
I'm currently using this code for jumping:
if not Jump:
if keys[pygame.K_UP]:
Jump=True
right=False #right, left and standing are for character's facing
left=False # and they serve no purpose for jumping
standing=False
steps=0
else:
if jumpCount >= -8.5:
standing=False
neg=1
if JumpCount < 0:
neg=-1
y-=(JumpCount**2)/2 *neg
jumpCount-=1
else:
Jump=False
jumpCount=8.5 # I've set it to 8.5 bc that value works for me best
Anyway, this code works for basic jumping, but a problem occurs if I want to jump, let's say, on a box.
I tried to set it up like this:
self.line1=[self.x,self.y+60] #this is the bottom left coordinate of the
#character
self.line2=[self.x+28,self.y+60] #this is the bottom right coordinate
def on(self,a):
if (a.line1[0]+12)>self.x and (a.line1[0]+12)<(self.x+40): # self.x+40
#is the top right coordinate of the box
if a.line1[1]<=self.y and (a.line1[1]+a.v)>=self.y:
return True
return False
def collision(self,a):
if self.on(a):
a.y=self.y-60
a.Jump=False
return
def all(self,a):
self.on(a)
self.collision(a)
return
But this doesn't work. I've managed to set up that when the character hits sides of the box it can't go further, but I couldn't do the jumping part.
Is it possible to jump on something with this code, or should I change the jumping code completely, use gravity-style jumping or something? Any kind of help is welcome.

Python time.sleep method not working properly [duplicate]

This question already has answers here:
How to print one character at a time on one line?
(4 answers)
How to make it look like the computer is typing? [duplicate]
(1 answer)
Closed 5 years ago.
I have written this small code in Python. It should print every character in a string with a small sleeptime between them...
import time, sys
def writeText(string, t):
i = 0
while i < len(string):
sys.stdout.write(string[i])
time.sleep(float(t))
i += 1
writeText("Hello World", 0.5)
but it only prints the whole string after 0.5 seconds... I often have this issue but I haven't found a solution yet.
sys.stdout.flush() might solve your problem.
import time, sys
def writeText(string, t):
i = 0
while i < len(string):
sys.stdout.write(string[i])
time.sleep(float(t))
i += 1
sys.stdout.flush()
writeText("Hello World", 0.5)
You should add
sys.stdout.flush()
after writing, to force the output of the text.

How to use randint for strings in Python 3? [duplicate]

This question already has answers here:
How can I randomly select an item from a list?
(17 answers)
Closed 5 years ago.
I am making a mini version of Russian Rullet.
I have a code which gives me a random number from 1 - 36 but I also need to ask my program to randomly give either Red or Black?
from random import randint
print(randint(1,36))
print (randint ('Red', 'Black'))
I wouldn't recommend this, but
from random import randint
print(["Red","Black"][randint(0,1)])
Check this
a = randint(0,1)
color = 'Red' if a == 1 else color == 'Black'
print(color)

Window opening procedure [duplicate]

This question already has answers here:
Why is my pygame display not responding while waiting for input?
(1 answer)
Pygame Window not Responding after few seconds
(3 answers)
Why does pygame.display.update() not work if an input is directly followed after it?
(1 answer)
Closed 1 year ago.
I have an instruction I need to do:
a procedure taking into parameter two integers 'l' and 'h' (length and width)assumed positive and a filename with the corresponding extension of an image. it will create a window of 'l' length and 'h' width and will affect a random value (R,G,B form) to each of it's pixels, finally it will save this image in the current directory under the name passed into parameter
Here's my (messy) code:
def surf (l, h, imagech):
pygame.init()
ecran = pygame.display.set_mode("RGB",(l, h))
fond = pygame.image.load("MPchiffre.bmp").convert()
surface.blit(fond, (0,0))
pygame.display.flip()
continue = 1
while continue:
continue = int(input())
pixels = getPixelArray('MPchiffre.bmp')
nu.random.randint(0, 255)
pygame.image.save(surface, "imagech.bmp")
my main issue is syntax basics and basic understanding of python, I don't have time to review all of it, if someone could tell me what's wrong with this code it would be nice.

Categories