Hey I'm making this game and i need a 1 or more second delay?
Got any ideas?
heres where i need a delay in between tx3 = 1000 and cheesyx = 1000.
if x < 300 and y < 300 and not duringfight:
win.blit(cheesyt3, (tx3, ty3))
if x < 250 and y < 250 and not duringfight:
tx3 = 1000
cheesyx = 1000
if cheesyx == 1000:
deathx -= 5
if deathx == 600:
deathx += 5
deathmove = False
wmx = 1000
win.blit(deathtext, (dtext, 400))
if x > 400:
dtext = 1000
win.blit(deathhanpup, (deathx, deathy))
deathy = 1000
Since time.sleep doesnt work that well with pygame, you can use the time module to compare the current time with the last execution, and execute your code in a (event)loop only after more than a second has passed.
import time
last_execution = time.time()
if time.time() - last_execution > 1:
last_execution = time.time
execute_code_you_want()
Pygame time
In pygame, time.sleep(1) doesn't work well, so you could do pygame.time.delay(1), that works fine.
Link: https://www.pygame.org/docs/ref/time.html#pygame.time.delay.
Related
It seems like with one image - I can create 36 pictures, but for some reason, I cannot create 36 different images and display them on Canvas. Only one random image is shown in position 30, for the reason I do not quite get :)
There will be an image added. It seems like generating a random image in the for loop does not work. I have tried to move it around - does not help.
Here is what I get
from tkinter import *
import math
import random
time_to_remember = 60
suit = ["clubs","diamonds","spades","hearts"]
names_cards = ["6","7","8","9","10","jack","ace","king"]
def countdown(count):
count_min = math.floor(count / 60)
count_sec = math.floor(count % 60)
if count_sec < 10:
count_sec = f"0{math.floor(count % 60)}"
timer_text.config( text=f"{count_min}:{count_sec}")
if count < 10:
timer_text.config( fg ="red")
if count > 0:
global timer
timer = window.after(1000, countdown, count - 1)
print(count)
window = Tk()
window.minsize(1000, 800)
canvas = Canvas(height=1000, width = 1000)
canvas.grid(row = 1, rowspan=6, column=0,columnspan=10 )
b_img = PhotoImage(file= "/Users/oleksandrzozulia/PycharmProjects/memory_project/Images/Screenshot 2022-08-27 at 11.48.49.png",
height=130,
width=80)
y_cor = 20
x_cor = 90
leng = 10
count = 0
ii = []
for i in range(0,4):
if count == 3:
leng = 6
for i in range(0,leng):
i = canvas.create_image(x_cor,y_cor, image=b_img, anchor="ne")
x_cor += 100
count +=1
x_cor = 90
y_cor += 150
#Display Random cards==================================================================
y_n = 20
x_n = 90
leng_n = 10
count_n = 0
for i in range(0,3):
if count_n == 3:
leng_n = 6
for i in range(0,leng_n):
img_n = PhotoImage(
file = f"Images/PNG-cards-1.3/{random.choice(names_cards)}_of_{random.choice(suit)}.png",
height=130,
width = 80)
i = canvas.create_image(x_n,y_n, image=img_n, anchor="ne")
x_n += 100
count +=1
x_n = 90
y_n += 150
I wrote a simple code to compare the performance of broadcasting vs for loop. The code is as below:
import numpy as np
D = 3072
num_train = 5000
test = np.random.rand(D)
X_train = np.random.rand(num_train, D)
def time_function(f, *args):
"""
Call a function f with args and return the time (in seconds) that it took to execute.
"""
import time
tic = time.time()
f(*args)
toc = time.time()
return toc - tic
def one_test_one_loop():
dists = np.zeros(num_train)
for i in range(num_train):
square_sum = np.sum((test - X_train[i]) ** 2)
dists[i] = square_sum ** (1 / 2)
def one_test_no_loop():
dists = np.zeros(num_train)
square_diffs = (test - X_train) ** 2
square_sums = np.sum(square_diffs, 1)
dists = square_sums ** (1 / 2)
one_loop_time, no_loop_time = 0, 0
for i in range(10):
one_loop_time += time_function(one_test_one_loop)
no_loop_time += time_function(one_test_no_loop)
print ("X_train's shape: (%d, %d)" % X_train.shape)
print ("test's shape: (%d, )" % test.shape)
print('One loop version took %f seconds' % one_loop_time)
print('No loop version took %f seconds' % no_loop_time)
And the result is as below:
X_train's shape: (5000, 3072)
test's shape: (3072, )
One loop version took 0.484136 seconds
No loop version took 0.934610 seconds
Basically, I'm computing the L2 distances between one test sample to all the 5000 train data. And the time function just returns the running time of a function.
I was expecting that broadcasting will be faster than the loop version, however, the broadcasting version is two times slower than the loop version. Why?
~Why the hell has this had down votes.... you people are weird!
Ok so this is a very simply HTML5 and jQuery and PHP game. Sorry to the people who have answered, I forgot to say this is a php script, i have updated here to reflect.
the first level takes 1 minute. Every level after that takes an extra 10 seconds than the last level. like so;
level 1 = 60 seconds
level 2 = 70 seconds
level 3 = 80 seconds
level 4 = 90 seconds
and so on infinitely.
I need an equation that can figure out what is the total amount of seconds played based on the users level.
level = n
i started with (n * 10) + (n * 60) but soon realized that that doesn't account for the last level already being 10 seconds longer than the last. I have temporarily fixed it using a function calling a foreach loop stopping at the level number and returning the value. but i really want an actual equation.
SO i know you wont let me down :-)
Thanks in advance.
this is what i am using;
function getnumberofsecondsfromlevel($level){
$lastlevelseconds = 60;
while($counter < $level){
$totalseconds = $lastlevelseconds+$totalseconds;
$lastlevelseconds = $lastlevelseconds + 10;
$counter++;
}
return $totalseconds;
}
$level = $_SESSION['**hidden**']['thelevel'];
$totaldureationinseconds = getnumberofsecondsfromlevel($level);
but i want to replace with an actual equation
like so;(of course this is wrong, this is just the example of the format i want it in i.e an equation)
$n = $_SESSION['**hidden**']['thelevel']; (level to get total value of
in seconds)
$s = 60; (start level)
$totaldureationinseconds = ($n * 10) + ($s * $n);
SOLVED by Gopalkrishna Narayan Prabhu :-)
$totalseconds = 60 * $level + 5* (($level-1) * $level);
var total_secs = 0;
for(var i = 1; i<= n ;i++){
total_secs = total_secs + (i*10) + 50;
}
for n= 1, total_secs = 0 + 10 + 50 = 60
for n= 2, total_secs = 60 + 20 + 50 = 130
and so on...
For a single equation:
var n = level_number;
total_secs = 60 * n + 5* ((n-1) * n);
Hope this helps.
It seems as though you're justing looking for the equation
60 + ((levelN - 1) * 10)
Where levelN is the current level, starting at 1. If you make the first level 0, you can get rid of the - 1 part and make it just
60 + (levelN * 10)
Thought process:
What's the base/first number? What's the lowest it can ever be? 60. That means your equation will start with
60 + ...
Every time you increase the level, you add 10, so at some point you'll need something like levelN * 10. Then, it's just some fiddling. In those case, since you don't add any on the first left, and the first level is level 1, you just need to subtract 1 from the level number to fix that.
You can solve this with a really simple mathematical phrase (with factorial).
((n-1)! * 10) + (60 * n)
n is the level ofcourse.
please i'm having problems resolving a function about a program in Visual Fox Pro.
I need to make a rounding down every 20 minutes in decimal.
For example: if i recive 19 minutes (0.316) y need return 0 minutes.
if i get between 0-19 minutes, return 0 minutes
if i get between 20-39 minutes, return 20 minutes
if i get between 40-59 minutes, return 40 minutes
if i get between 60-79 minutes, return 60 minutes
i was thinking use ROUND() but i don't know how because "Round" Approaches to the nearest decimal.
thanks in advance.
roundedMinutes = Floor( m.myMinute / 20 ) * 20
For example:
Create Cursor SampleMin (minutes i, rounded i)
Local ix
For ix = 1 To 600
Insert Into SampleMin ;
(minutes, rounded) ;
values ;
(m.ix, Floor(m.ix/20) * 20)
Endfor
Locate
Browse
Here's a small prg I made that will hopefully help you or at least start you in the right direction.
lnStart = 0
lnEnd = 20
lnReceivedMinutes = 500
llNotDone = .T.
IF lnReceivedMinutes > 0
DO WHILE llNotDone
IF BETWEEN(lnReceivedMinutes, lnStart, lnEnd)
llNotDone = .F.
MESSAGEBOX(ALLTRIM(STR(lnStart)) + " Minutes")
ELSE
lnStart = lnStart + 20
lnEnd = lnEnd + 20
ENDIF
ENDDO
ENDIF
I check to see if the value received is between my lnStart and lnEnd. If it is not then I check for the next 20 minutes.
Cetin Basoz gave correct answer
roundedMinutes = Floor( m.myMinute / 20 ) * 20
I have checked myself
Basically I am trying to solve a 2nd order differential equation with the forward euler method. I have some for loops inside my code, which take considerable time to solve and I would like to speed things up a bit. Does anyone have any suggestions how could I do this?
And also when looking at the time it takes, I notice that my end at line 14 takes 45 % of my total time. What is end actually doing and why is it taking so much time?
Here is my simplified code:
t = 0:0.01:100;
dt = t(2)-t(1);
B = 3.5 * t;
F0 = 2 * t;
BB=zeros(1,length(t)); % Preallocation
x = 2; % Initial value
u = 0; % Initial value
for ii = 1:length(t)
for kk = 1:ii
BB(ii) = BB(ii) + B(kk) * u(ii-kk+1)*dt; % This line takes the most time
end % This end takes 45% of the other time
x(ii+1) = x(ii) + dt*u(ii);
u(ii+1) = u(ii) + dt * (F0(ii) - BB(ii));
end
Running the code it takes me 8.552 sec.
You can remove the inner loop, I think:
for ii = 1:length(t)
for kk = 1:ii
BB(ii) = BB(ii) + B(kk) * u(ii-kk+1)*dt; % This line takes the most time
end % This end takes 45% of the other time
x(ii+1) = x(ii) + dt*u(ii);
u(ii+1) = u(ii) + dt * (F0(ii) - BB(ii));
end
So BB(ii) = BB(ii) (zero at initalisation) + sum for 1 to ii of BB(kk)* u(ii-kk+1).dt
but kk = 1:ii, so for a given ii, ii-kk+1 → ii-(1:ii) + 1 → ii:-1:1
So I think this is equivalent to:
for ii = 1:length(t)
BB(ii) = sum(B(1:ii).*u(ii:-1:1)*dt);
x(ii+1) = x(ii) + dt*u(ii);
u(ii+1) = u(ii) + dt * (F0(ii) - BB(ii));
end
It doesn't take as long as 8 seconds for me using either method, but the version with only one loop is about 2x as fast (the output of BB appears to be the same).
Is the sum loop of B(kk) * u(ii-kk+1) just conv(B(1:ii),u(1:ii),'same')
The best way to speed up loops in matlab is to try to avoid them. Try if you are able to perform a matrix operation instead of the inner loop. For example try to break the calculation you do there in small parts, then decide, if there are parts you can perform in advance without knowing the results of the next iteration of the loop.
to your secound part of the question, my guess:: The end contains the check if the loop runs for another round and this check by it self is not that long but called 50.015.001 times!