Skip to content

Commit 56efce9

Browse files
committed
Keys. Now. Work. Correctly!
1 parent 71a60c6 commit 56efce9

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

assets/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"targets":[{"isStage":true,"name":"Stage","variables":{"`jEk@4|i[#Fk?(8x)AV.-my variable":["variabila mea",0]},"lists":{},"broadcasts":{},"blocks":{},"comments":{},"currentCostume":0,"costumes":[{"assetId":"cd21514d0531fdffb22204e0ec5ed84a","name":"decor1","md5ext":"cd21514d0531fdffb22204e0ec5ed84a.svg","dataFormat":"svg","rotationCenterX":240,"rotationCenterY":180}],"sounds":[{"assetId":"83a9787d4cb6f3b7632b4ddfebf74367","name":"pop","dataFormat":"wav","format":"","rate":44100,"sampleCount":1032,"md5ext":"83a9787d4cb6f3b7632b4ddfebf74367.wav"}],"volume":100,"layerOrder":0,"tempo":60,"videoTransparency":50,"videoState":"on","textToSpeechLanguage":null},{"isStage":false,"name":"Personaj1","variables":{},"lists":{},"broadcasts":{},"blocks":{"[_TPYe1f0U#J6fC3Z-H9":{"opcode":"event_whenflagclicked","next":"TrO*p@+rW#b|n.cRboA$","parent":null,"inputs":{},"fields":{},"shadow":false,"topLevel":true,"x":227,"y":227},"TrO*p@+rW#b|n.cRboA$":{"opcode":"looks_switchcostumeto","next":null,"parent":"[_TPYe1f0U#J6fC3Z-H9","inputs":{"COSTUME":[1,"qC-grBho7d-C}[D}D5+c"]},"fields":{},"shadow":false,"topLevel":false},"qC-grBho7d-C}[D}D5+c":{"opcode":"looks_costume","next":null,"parent":"TrO*p@+rW#b|n.cRboA$","inputs":{},"fields":{"COSTUME":["costum1",null]},"shadow":true,"topLevel":false},"SXNG`gV~`q%fX8J{m_fO":{"opcode":"event_whenkeypressed","next":"$xg78~48L;;5Q).$!n)T","parent":null,"inputs":{},"fields":{"KEY_OPTION":["right arrow",null]},"shadow":false,"topLevel":true,"x":267,"y":543},"$xg78~48L;;5Q).$!n)T":{"opcode":"looks_nextcostume","next":null,"parent":"SXNG`gV~`q%fX8J{m_fO","inputs":{},"fields":{},"shadow":false,"topLevel":false}},"comments":{},"currentCostume":0,"costumes":[{"assetId":"b7853f557e4426412e64bb3da6531a99","name":"costum1","bitmapResolution":1,"md5ext":"b7853f557e4426412e64bb3da6531a99.svg","dataFormat":"svg","rotationCenterX":48,"rotationCenterY":50},{"assetId":"e6ddc55a6ddd9cc9d84fe0b4c21e016f","name":"costum2","bitmapResolution":1,"md5ext":"e6ddc55a6ddd9cc9d84fe0b4c21e016f.svg","dataFormat":"svg","rotationCenterX":46,"rotationCenterY":53}],"sounds":[{"assetId":"83c36d806dc92327b9e7049a565c6bff","name":"Miau","dataFormat":"wav","format":"","rate":44100,"sampleCount":37376,"md5ext":"83c36d806dc92327b9e7049a565c6bff.wav"}],"volume":100,"layerOrder":1,"visible":true,"x":0,"y":0,"size":100,"direction":90,"draggable":false,"rotationStyle":"all around"}],"monitors":[],"extensions":[],"meta":{"semver":"3.0.0","vm":"0.2.0-prerelease.20201016122132","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Scratch/3.18.1 Chrome/80.0.3987.165 Electron/8.2.5 Safari/537.36"}}
1+
{"targets":[{"isStage":true,"name":"Stage","variables":{"`jEk@4|i[#Fk?(8x)AV.-my variable":["variabila mea",0]},"lists":{},"broadcasts":{},"blocks":{},"comments":{},"currentCostume":0,"costumes":[{"assetId":"cd21514d0531fdffb22204e0ec5ed84a","name":"decor1","md5ext":"cd21514d0531fdffb22204e0ec5ed84a.svg","dataFormat":"svg","rotationCenterX":240,"rotationCenterY":180}],"sounds":[{"assetId":"83a9787d4cb6f3b7632b4ddfebf74367","name":"pop","dataFormat":"wav","format":"","rate":44100,"sampleCount":1032,"md5ext":"83a9787d4cb6f3b7632b4ddfebf74367.wav"}],"volume":100,"layerOrder":0,"tempo":60,"videoTransparency":50,"videoState":"on","textToSpeechLanguage":null},{"isStage":false,"name":"Personaj1","variables":{},"lists":{},"broadcasts":{},"blocks":{"2gXe00(^=5?jAV4GO65H":{"opcode":"event_whenkeypressed","next":"[6l:dI8V6DP/46FRvqWF","parent":null,"inputs":{},"fields":{"KEY_OPTION":["up arrow",null]},"shadow":false,"topLevel":true,"x":221,"y":482},"[6l:dI8V6DP/46FRvqWF":{"opcode":"control_repeat","next":"66+5V9OT]aVmQ~an[lHS","parent":"2gXe00(^=5?jAV4GO65H","inputs":{"TIMES":[1,[6,"10"]],"SUBSTACK":[2,"!5m_j*V4@%-:ZJk_u8BM"]},"fields":{},"shadow":false,"topLevel":false},"66+5V9OT]aVmQ~an[lHS":{"opcode":"control_repeat","next":null,"parent":"[6l:dI8V6DP/46FRvqWF","inputs":{"TIMES":[1,[6,"10"]],"SUBSTACK":[2,"0w,.oaq;Me*YJldkC8F."]},"fields":{},"shadow":false,"topLevel":false},"!5m_j*V4@%-:ZJk_u8BM":{"opcode":"motion_changeyby","next":null,"parent":"[6l:dI8V6DP/46FRvqWF","inputs":{"DY":[1,[4,"10"]]},"fields":{},"shadow":false,"topLevel":false},"0w,.oaq;Me*YJldkC8F.":{"opcode":"motion_changeyby","next":null,"parent":"66+5V9OT]aVmQ~an[lHS","inputs":{"DY":[1,[4,"-10"]]},"fields":{},"shadow":false,"topLevel":false}},"comments":{},"currentCostume":0,"costumes":[{"assetId":"b7853f557e4426412e64bb3da6531a99","name":"costum1","bitmapResolution":1,"md5ext":"b7853f557e4426412e64bb3da6531a99.svg","dataFormat":"svg","rotationCenterX":48,"rotationCenterY":50},{"assetId":"e6ddc55a6ddd9cc9d84fe0b4c21e016f","name":"costum2","bitmapResolution":1,"md5ext":"e6ddc55a6ddd9cc9d84fe0b4c21e016f.svg","dataFormat":"svg","rotationCenterX":46,"rotationCenterY":53}],"sounds":[{"assetId":"83c36d806dc92327b9e7049a565c6bff","name":"Miau","dataFormat":"wav","format":"","rate":44100,"sampleCount":37376,"md5ext":"83c36d806dc92327b9e7049a565c6bff.wav"}],"volume":100,"layerOrder":1,"visible":true,"x":0,"y":0,"size":100,"direction":90,"draggable":false,"rotationStyle":"all around"}],"monitors":[],"extensions":[],"meta":{"semver":"3.0.0","vm":"0.2.0-prerelease.20201016122132","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Scratch/3.18.1 Chrome/80.0.3987.165 Electron/8.2.5 Safari/537.36"}}

config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
# Project file name
2626
# If in test mode, set the Scratch project file to load.
27-
projectFileName: str = "projects/RightArrowKey.sb3"
27+
projectFileName: str = "projects/KeyJump.sb3"
2828

2929
# Download cache size
3030
# Number of recent downloaded projects stored. 0 means infinity.
@@ -63,7 +63,7 @@
6363

6464
# Key delay
6565
# Set the delay before key events start repeating. (in milliseconds)
66-
keyDelay: int = 5000
66+
keyDelay: int = 250
6767

6868
# Screen width/height
6969
# Stage size. You can change that, but most projects won't work with it.

main.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,9 @@ def buttonbox(self):
240240
keyEventContainer = eventContainer.EventContainer()
241241

242242
# Mainloop
243-
counterValue = 0
243+
lastTime = time.time_ns()
244244
while projectRunning:
245+
keyEventContainer.keyEvents = set()
245246
# Process Pygame events
246247
for event in pygame.event.get():
247248
# Window quit (ALT-F4 / X button / etc.)
@@ -254,7 +255,8 @@ def buttonbox(self):
254255
keyEventContainer.keyEvents = set()
255256
if event.type == pygame.KEYDOWN:
256257
keyEventContainer.keyEvents.add(event.key)
257-
print("new key event", time.time_ns())
258+
# print(event.key, "+" + str((time.time_ns() - lastTime) // 1000000))
259+
lastTime = time.time_ns()
258260
keysRaw = pygame.key.get_pressed()
259261
keyEventContainer.keys = set(k for k in scratch.KEY_MAPPING.values() if keysRaw[k])
260262

@@ -308,21 +310,21 @@ def buttonbox(self):
308310

309311
display.fill((255, 255, 255))
310312
if not isPaused:
311-
print("Starting frame at", time.time_ns())
313+
# print("starting new frame", keyEventContainer.keyEvents)
312314
for e in eventHandlers:
313315
# TODO why does it run so many times???
314316
if e.opcode == "event_whenkeypressed" and keyEventContainer.keyEvents and not e.blockRan:
315-
print("running", e.blockID)
317+
# print("running", e.blockID)
316318

317319
e.blockRan = True
318320
nextBlock = scratch.execute(e, e.target.sprite, keyEventContainer)
319321
if nextBlock and isinstance(nextBlock, list):
320322
toExecute.extend(nextBlock)
321-
print("next:", (b.blockID for b in nextBlock))
323+
# print("next:", (b.blockID for b in nextBlock))
322324
elif nextBlock:
323325
toExecute.append(nextBlock)
324-
print("next:", nextBlock.blockID)
325-
print(keyEventContainer.keyEvents, "in main.py", "(" + str(len(eventHandlers)) + ")")
326+
# print("next:", nextBlock.blockID)
327+
# print(keyEventContainer.keyEvents, "in main.py", "(" + str(len(eventHandlers)) + ")")
326328

327329
if e.opcode == "event_whenkeypressed":
328330
# print(s.target.blocks, e.script)
@@ -343,8 +345,6 @@ def buttonbox(self):
343345
block.executionTime, block.timeDelay = 0, 0
344346
if not block.blockRan and not block.opcode.startswith("event"): # TODO add broadcast blocks
345347
nextBlock = scratch.execute(block, block.target.sprite, keyEventContainer)
346-
if block.opcode == "looks_nextcostume":
347-
counterValue += 1
348348
if not block.next \
349349
and block.top \
350350
and block.top.opcode.startswith("event") \
@@ -370,8 +370,6 @@ def buttonbox(self):
370370
else:
371371
display.blit(paused, (WIDTH // 2 - pausedWidth // 2, WIDTH // 2 - pausedHeight // 2))
372372

373-
counter = font.render(str(counterValue), True, (0, 0, 128))
374-
display.blit(counter, (WIDTH // 2 - pausedWidth // 2, WIDTH // 2 - pausedHeight // 2))
375373
pygame.display.flip()
376374
mainWindow.update()
377375
doScreenRefresh = False

scratch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def execute(block, s, events=eventContainer.EventContainer()):
225225
pass
226226

227227
elif opcode == "event_whenkeypressed":
228-
print(time.time_ns(), "in whenkeypressed")
228+
# print(time.time_ns(), "in whenkeypressed")
229229

230230
# print("Handling key event")
231231
# if not block.waiting:
@@ -264,7 +264,7 @@ def execute(block, s, events=eventContainer.EventContainer()):
264264
nextBlock = s.target.blocks[block.next]
265265
return nextBlock
266266
elif KEY_MAPPING[key] in keyEvents and block.next: # when key [. . . v] pressed
267-
print(keyEvents, "received in execute()")
267+
# print(keyEvents, "received in execute()")
268268
if key == "left arrow":
269269
keyName = _("key-left")
270270
elif key == "right arrow":

testPygameKeyEvents.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import time
2+
import pygame
3+
4+
pygame.init()
5+
pygame.key.set_repeat(2000, 1000 // 30)
6+
7+
pygame.display.set_mode((240, 180))
8+
9+
lastTime = time.time_ns()
10+
11+
if __name__ == "__main__":
12+
while True:
13+
for event in pygame.event.get():
14+
if event.type == pygame.QUIT:
15+
exit()
16+
if event.type == pygame.KEYDOWN:
17+
print(event.key, "+" + str((time.time_ns() - lastTime) // 1000000))
18+
lastTime = time.time_ns()

0 commit comments

Comments
 (0)