0

The WASD Controls for an image in my game aren't working, but in the same project there is a part of the code where it makes the sprite move to the mouse pointer. Why does WASD controls not work?

Help would be much appreciated, Antoni

I tried figuring out why their were errors with the modules i imported, and i tried to arrange the code differently [PLEASE NOTE THIS IS AN UPDATED QUESTION AND CODE IS NEW]

Code below

import sys, pygame, math
from pygame.locals import *

spaceship = ('sprite.png')
mouse_c = ('crosshair.png')
backg = ('background.jpg')
pygame.init()
screen = pygame.display.set_mode((800, 600))
bk = pygame.image.load(backg).convert_alpha()
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)
while True:
  for event in pygame.event.get():
    if event.type == QUIT:
      pygame.quit()
      sys.exit()
    elif event.type == MOUSEBUTTONDOWN and event.button == 1:
      print("test1")
    elif event.type == MOUSEBUTTONDOWN and event.button == 3:
      print("test3")
    screen.blit(bk, (0, 0))
    pos = pygame.mouse.get_pos()
    screen.blit(mousec, (pos))
    angle = 360-math.atan2(pos[1]-300,pos[0]-400)*180/math.pi
    rotimage = pygame.transform.rotate(space_ship,angle)
    rect = rotimage.get_rect(center=(400,300))
    screen.blit(rotimage,rect) 
  #I need space_ship to rotate towards my cursor
    pygame.display.update()



while True:
# initiate pygame and give permission
# to use pygame's functionality.
  pygame.init()
  
# create the display surface object
# of specific dimension.
  window = pygame.display.set_mode((600, 600))
  
# Add caption in the window
  pygame.display.set_caption('Player Movement')
  
# Initializing the clock
# Clocks are used to track and
# control the frame-rate of a game
  clock = pygame.time.Clock()
  
# Add player sprite
  
  
# Store the initial
# coordinates of the player in
# two variables i.e. x and y.
  x = 100
  y = 100
  
# Create a variable to store the
# velocity of player's movement
  velocity = 12

#rotate image def:
  
    # Set the frame rates to 60 fps
  clock.tick(20)
  
    # Filling the background with
    # white color
  window.fill((255, 255, 255))
  
    # Display the player sprite at x
    # and y coordinates
  window.blit(spaceship, (x, y))


  
    # iterate over the list of Event objects
    # that was returned by pygame.event.get() method.
  for event in pygame.event.get():
  
        # Closing the window and program if the
        # type of the event is QUIT
    if event.type == pygame.QUIT:
      run = False
      pygame.quit()
      quit()
  
    # Storing the key pressed in a
    # new variable using key.get_pressed()
    # method

while True:
  key_pressed_is = pygame.key.get_pressed()
  
    # Changing the coordinates
    # of the player
  if key_pressed_is[K_A]:
      x -= 3
  if key_pressed_is[K_D]:
      x += 3
  if key_pressed_is[K_W]:
      y -= 3
  if key_pressed_is[K_S]:
      y += 3
  
    # Draws the surface object to the screen.
  pygame.display.update()
Antoni
  • 34
  • 3
  • there was another post with the same question but it was a different style of code(pygame) – Antoni May 12 '23 at 00:02
  • Code after line 31 is not run because the while loop ends there. All of the keyboard code is never run and is not part of your game – M Virts May 12 '23 at 00:04
  • thank u M Virts, but after those changes, there are new errors. – Antoni May 12 '23 at 01:32
  • code is here would you be able to see whats wrong it still wont work afetr adjustments, i am only a beginner and 13 yrs old – Antoni May 12 '23 at 01:34
  • https://replit.com/@AntoniDruzynski/Test#main.py – Antoni May 12 '23 at 01:34
  • You've done a great job so far! I've been programming since I was young and it has served me well. I recommend reading through this code line-by-line and pretending you're the computer executing it. The first problem is that you have the complete code for 2 games in the same file. Try to work on combining both of the `while` loops into one, thinking about what each line does and why it appears. If you see two places that have the same or similar code, think about if they both need to be there or can be combined. – M Virts May 12 '23 at 05:53

0 Answers0