Moving Sprites Right and Downwards
I am working on this project to move a sprite, but I can't seem to figure out how to move a sprite to the right as well as move it downwards. Any thoughts? Here is my program:
import pygame import time import sys pygame.init() # Set up window screen = pygame.display.set_mode((320, 240)) # Load an image rocket = pygame.image.load("rocket.png") rocketrect = rocket.get_rect() x_coord = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill((0,0,0)) screen.blit(rocket, rocketrect) pygame.display.flip() x_coord += 1 rocketrect.centerx = x_coord
In your method of mving the sprite, you change the coordinatses (x) and then assign it to the centerx of the images rectangle. If you want to keep this kind of method (changing and assigning), and also move the image down, you will need to give a y value. For example
# Define y variable y_coord = 0 # Your code … y_coords += 1 rocketrect.centery = y_coord
This works similarly to how you moved your x_coords, but in total, the program is a bit basic and is not how programmers (or at least me) usually code. Another person might used a tuple for the location, along with making a rocket class. There are also some other ways to move the sprite, such as the .move() or .move_ip() that I would suggest. But it's up to you.