Skip to content

Commit

Permalink
Keys. Now. Work. Correctly!
Browse files Browse the repository at this point in the history
  • Loading branch information
Secret-chest committed Nov 2, 2022
1 parent 71a60c6 commit 56efce9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
2 changes: 1 addition & 1 deletion assets/project.json
Original file line number Diff line number Diff line change
@@ -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"}}
{"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"}}
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

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

# Download cache size
# Number of recent downloaded projects stored. 0 means infinity.
Expand Down Expand Up @@ -63,7 +63,7 @@

# Key delay
# Set the delay before key events start repeating. (in milliseconds)
keyDelay: int = 5000
keyDelay: int = 250

# Screen width/height
# Stage size. You can change that, but most projects won't work with it.
Expand Down
20 changes: 9 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ def buttonbox(self):
keyEventContainer = eventContainer.EventContainer()

# Mainloop
counterValue = 0
lastTime = time.time_ns()
while projectRunning:
keyEventContainer.keyEvents = set()
# Process Pygame events
for event in pygame.event.get():
# Window quit (ALT-F4 / X button / etc.)
Expand All @@ -254,7 +255,8 @@ def buttonbox(self):
keyEventContainer.keyEvents = set()
if event.type == pygame.KEYDOWN:
keyEventContainer.keyEvents.add(event.key)
print("new key event", time.time_ns())
# print(event.key, "+" + str((time.time_ns() - lastTime) // 1000000))
lastTime = time.time_ns()
keysRaw = pygame.key.get_pressed()
keyEventContainer.keys = set(k for k in scratch.KEY_MAPPING.values() if keysRaw[k])

Expand Down Expand Up @@ -308,21 +310,21 @@ def buttonbox(self):

display.fill((255, 255, 255))
if not isPaused:
print("Starting frame at", time.time_ns())
# print("starting new frame", keyEventContainer.keyEvents)
for e in eventHandlers:
# TODO why does it run so many times???
if e.opcode == "event_whenkeypressed" and keyEventContainer.keyEvents and not e.blockRan:
print("running", e.blockID)
# print("running", e.blockID)

e.blockRan = True
nextBlock = scratch.execute(e, e.target.sprite, keyEventContainer)
if nextBlock and isinstance(nextBlock, list):
toExecute.extend(nextBlock)
print("next:", (b.blockID for b in nextBlock))
# print("next:", (b.blockID for b in nextBlock))
elif nextBlock:
toExecute.append(nextBlock)
print("next:", nextBlock.blockID)
print(keyEventContainer.keyEvents, "in main.py", "(" + str(len(eventHandlers)) + ")")
# print("next:", nextBlock.blockID)
# print(keyEventContainer.keyEvents, "in main.py", "(" + str(len(eventHandlers)) + ")")

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

counter = font.render(str(counterValue), True, (0, 0, 128))
display.blit(counter, (WIDTH // 2 - pausedWidth // 2, WIDTH // 2 - pausedHeight // 2))
pygame.display.flip()
mainWindow.update()
doScreenRefresh = False
Expand Down
4 changes: 2 additions & 2 deletions scratch.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def execute(block, s, events=eventContainer.EventContainer()):
pass

elif opcode == "event_whenkeypressed":
print(time.time_ns(), "in whenkeypressed")
# print(time.time_ns(), "in whenkeypressed")

# print("Handling key event")
# if not block.waiting:
Expand Down Expand Up @@ -264,7 +264,7 @@ def execute(block, s, events=eventContainer.EventContainer()):
nextBlock = s.target.blocks[block.next]
return nextBlock
elif KEY_MAPPING[key] in keyEvents and block.next: # when key [. . . v] pressed
print(keyEvents, "received in execute()")
# print(keyEvents, "received in execute()")
if key == "left arrow":
keyName = _("key-left")
elif key == "right arrow":
Expand Down
18 changes: 18 additions & 0 deletions testPygameKeyEvents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import time
import pygame

pygame.init()
pygame.key.set_repeat(2000, 1000 // 30)

pygame.display.set_mode((240, 180))

lastTime = time.time_ns()

if __name__ == "__main__":
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
print(event.key, "+" + str((time.time_ns() - lastTime) // 1000000))
lastTime = time.time_ns()

0 comments on commit 56efce9

Please sign in to comment.