简体   繁体   English

Python pygame.mouse.get_pos() 在IDE终端中坐标打印,但是当鼠标移动时游戏窗口中的对象没有移动

[英]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.我正在关注 programarcadegames 网站来尝试移动对象以及鼠标移动。 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()

Your draw_spaceship function draws the ship at a constant position.您的draw_spaceship函数将船绘制在一个恒定的位置。 Draw the ship relative to the x and y coordiante:绘制相对于xy坐标的船:

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):使用当前鼠标位置而不是 (0, 0) 调用draw_spaceship

while not done:
    # [...]

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

draw_spaceship is drawing your ship at a constant position, (0,0). draw_spaceship将您的船绘制在一个恒定的位置 (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.编辑:看起来你的draw_spaceship()函数有硬编码值来绘制宇宙飞船,甚至没有使用给定的 x 和 y 参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM