Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使一个对象在python中具有设置的y位置时用鼠标水平移动_Python_Pygame - Fatal编程技术网

如何使一个对象在python中具有设置的y位置时用鼠标水平移动

如何使一个对象在python中具有设置的y位置时用鼠标水平移动,python,pygame,Python,Pygame,我想在街机游戏galaga中复制宇宙飞船。宇宙飞船需要比鼠标移动得慢,但仍然跟随它。到目前为止,这就是我所拥有的,我不知道从这里可以走到哪里。请帮忙 请记住,我是编程新手,所以我相信这很难理解 import pygame import math pygame.display.init() pygame.mixer.init() screen = pygame.display.set_mode((800, 600)) #fireSnd = pygame.mixer.Sound() #pygame

我想在街机游戏galaga中复制宇宙飞船。宇宙飞船需要比鼠标移动得慢,但仍然跟随它。到目前为止,这就是我所拥有的,我不知道从这里可以走到哪里。请帮忙 请记住,我是编程新手,所以我相信这很难理解

import pygame
import math
pygame.display.init()
pygame.mixer.init()
screen = pygame.display.set_mode((800, 600))

#fireSnd = pygame.mixer.Sound()
#pygame.mixer.music.load()
#pygame.mixer.music.play(-1)
x1 = 400
x2 = 405
x3 = 395
point1 = (x1,585)
point2 =(x2,600)
point3 = (x3,600)
SpaceShip = (point1,point2,point3)
Hdir = 0

done = False

while not done:
    # Step1. Erase the screen
    screen.fill((0, 0, 0))
    # Step2. Update

    # Step3. Process variables
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        done = True
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            done = True
    elif event.type == pygame.MOUSEMOTION:
        x,y = event.pos
    # Step4. Drawing
    pygame.draw.polygon(screen, (255,0,0) ,(SpaceShip), 0)
    pygame.display.flip()



pygame.display.quit()    

只要让你的飞船以x速率向左移动(如果鼠标在它的左边),如果鼠标在它的右边,就以x速率向右移动

要获取鼠标位置,请执行以下操作:

mousePos = pygame.mouse.get_pos()
你可能想改变你画宇宙飞船的方式。现在,您已经指定了一个具有点1、点2和点3的三角形。它不会改变。让我们这样做:

spaceshipPos = [400]

Spaceship = ((spaceshipPos[0],585),(spaceshipPos[0] + 5,600),(spaceshipPos[0] - 5,600))
这只是获取宇宙飞船的位置,并给出每个三角形点的位置

while True: # game loop

    if spaceshipPos[0] > mousePos[0]:  # if the mouse is to the left, move left
        spaceshipPos[0] -= 5
    elif spaceshipPos[0] < mousePos[1]:  # if the mouse is to the right, move right
        spaceshipPos[0] += 5

    pygame.draw.polygon(screen, (255,0,0), Spaceship, 0)
为True时:#游戏循环
如果spaceshipPos[0]>mousePos[0]:#如果鼠标在左侧,请向左移动
太空船位置[0]-=5
elif spaceshipPos[0]
因此,您的完整代码是:

import pygame
import math
pygame.display.init()
pygame.mixer.init()
screen = pygame.display.set_mode((800, 600))

#fireSnd = pygame.mixer.Sound()
#pygame.mixer.music.load()
#pygame.mixer.music.play(-1)

Hdir = 0

moveRate = 1

spaceshipPos = [400]

done = False

while not done:

    screen.fill((0, 0, 0))

    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        done = True
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            done = True

    mousePos = pygame.mouse.get_pos()

    if spaceshipPos[0] > mousePos[0]:  # if the mouse is to the left, move left
        spaceshipPos[0] -= moveRate

    elif spaceshipPos[0] < mousePos[0]:  # if the mouse is to the right, move right
        spaceshipPos[0] += moveRate

    elif spaceshipPos[0] == mousePos[0]:
        pass

    Spaceship = ((spaceshipPos[0],585),(spaceshipPos[0] + 5,600),(spaceshipPos[0] - 5,600))
    pygame.draw.polygon(screen, (255,0,0) ,(Spaceship), 0)

    pygame.display.flip()



pygame.display.quit()    
导入pygame
输入数学
pygame.display.init()
pygame.mixer.init()
screen=pygame.display.set_模式((800600))
#fireSnd=pygame.mixer.Sound()
#pygame.mixer.music.load()文件
#pygame.mixer.music.play(-1)
Hdir=0
移动速率=1
太空船位置=[400]
完成=错误
虽然没有这样做:
屏幕填充((0,0,0))
event=pygame.event.poll()
如果event.type==pygame.QUIT:
完成=正确
elif event.type==pygame.KEYDOWN:
如果event.key==pygame.K_退出:
完成=正确
mousePos=pygame.mouse.get_pos()
如果spaceshipPos[0]>mousePos[0]:#如果鼠标在左侧,请向左移动
spaceshipPos[0]-=移动速率
elif spaceshipPos[0]