sam
sam

Reputation: 21

Python pygame.mouse.get_pos() coordinates printing in IDE terminal, but object in game window isn't moving when the mouse is moved

I'm following the programarcadegames website to try and move an object along with mouse movement. When I run the program, the coordinates of the moving mouse print out as it goes which is good, but the issue is that the item itself is constantly stuck in its starting position. I've tried making changes and running them to see what went wrong but the same thing keeps happening. Here is my code so far:

import pygame
import random 

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GRAY = (128, 128, 128)

pygame.init()

star_list = []

for i in range(50):
    x = random.randrange(0, 700)
    y = random.randrange(0, 700)
    star_list.append([x, y])

# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Space Game")

# Loop until the user clicks the close button.
done = False

# Draw spaceship
def draw_spaceship(screen, x ,y):
    #body
    pygame.draw.rect(screen, GRAY, [350,375,20,40], 0)
    #wing1  pygame.draw.polygon(screen, BLUE, [[350,375], [330,375], [350,400]], 0)
    pygame.draw.polygon(screen, GRAY, [[390,375], [365,375], [365,400]], 0)
    #wing2
    pygame.draw.polygon(screen, GRAY, [[350,375], [330,375], [350,400]], 0)

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    
# Game logic

# Mouse movement
    pos = pygame.mouse.get_pos()
    print (pos)
    x=pos[0]
    y=pos[1]

# Background
    screen.fill(BLACK)

        # Process each star in the list
    for i in range(len(star_list)):
        pygame.draw.circle(screen, WHITE, star_list[i], 2)
        star_list[i][1] += 1
        if star_list[i][1] > 700:
            y = random.randrange(-50, -10)
            star_list[i][1] = y
            x = random.randrange(0, 700)
            star_list[i][0] = x

    #call draw_spaceship
    draw_spaceship(screen, 0, 0)

# --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # --- Limit to 60 frames per second
    clock.tick(60)
 
# Close the window and quit.
pygame.quit()

Upvotes: 2

Views: 196

Answers (2)

MegaEmailman
MegaEmailman

Reputation: 545

draw_spaceship is drawing your ship at a constant position, (0,0). You should change the call for that to something more like

draw_spaceship(screen, pos[0], pos[1])

Edit: It looks like your draw_spaceship() function has hardcoded values for where to draw the spaceship, without even using the x and y arguments given.

Upvotes: 1

Rabbid76
Rabbid76

Reputation: 210946

Your draw_spaceship function draws the ship at a constant position. Draw the ship relative to the x and y coordiante:

def draw_spaceship(screen, x, y):
    #body
    pygame.draw.rect(screen, GRAY, [x-10, y-20, 20, 40], 0)
    #wing1
    pygame.draw.polygon(screen, GRAY, [[x+30,y-20], [x+10,y-20], [x+10,y+5]], 0)
    #wing2
    pygame.draw.polygon(screen, GRAY, [[x-10,y-20], [x-30,y-20], [x-10,y+5]], 0)

Call draw_spaceship with the current mouse position, instead of (0, 0):

while not done:
    # [...]

    #draw_spaceship(screen, 0, 0)     
    draw_spaceship(screen, pos[0], pos[1])

Upvotes: 2

Related Questions