From 0680b5453fd598ef7269fa10164902d392a48c9d Mon Sep 17 00:00:00 2001 From: FilsonTyler Date: Mon, 9 Jun 2025 16:00:57 -0400 Subject: [PATCH 1/2] testing commits --- main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.py b/main.py index f3df86ee..15ed4ffe 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,5 @@ +# Testing Commits as Tfilson + import math import random From 1fab5b2aae81aeea80be6e454812956a04ea2f8b Mon Sep 17 00:00:00 2001 From: FilsonTyler Date: Mon, 9 Jun 2025 16:30:41 -0400 Subject: [PATCH 2/2] Rewrote movement key handling to prevent letting go of a key from unconditionally halting the player's momentum --- main.py | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 15ed4ffe..c86c4ab0 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,3 @@ -# Testing Commits as Tfilson - import math import random @@ -101,10 +99,12 @@ def isCollision(enemyX, enemyY, bulletX, bulletY): return False +# default to going left +lastKeyL = True + # Game Loop running = True while running: - # RGB = Red, Green, Blue screen.fill((0, 0, 0)) # Background Image @@ -113,23 +113,45 @@ def isCollision(enemyX, enemyY, bulletX, bulletY): if event.type == pygame.QUIT: running = False - # if keystroke is pressed check whether its right or left + # if keystroke is pressed mark it as the most recent if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: - playerX_change = -5 + lastKeyL = True if event.key == pygame.K_RIGHT: - playerX_change = 5 + lastKeyL = False if event.key == pygame.K_SPACE: if bullet_state is "ready": bulletSound = mixer.Sound("laser.wav") bulletSound.play() - # Get the current x cordinate of the spaceship + # Get the current x coordinate of the spaceship bulletX = playerX fire_bullet(bulletX, bulletY) - if event.type == pygame.KEYUP: - if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: - playerX_change = 0 + # determine which movement keys are held + keys = pygame.key.get_pressed() + if keys[pygame.K_LEFT]: + left = True + else: + left = False + if keys[pygame.K_RIGHT]: + right = True + else: + right = False + + # determine velocity based on which keys are held in which order + if (not right) and (not left): + playerX_change = 0 + else: + if right and left: + if lastKeyL: + playerX_change = -5 + else: + playerX_change = 5 + else: + if left: + playerX_change = -5 + else: + playerX_change = 5 # 5 = 5 + -0.1 -> 5 = 5 - 0.1 # 5 = 5 + 0.1