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()