Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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 如何使用pygame在屏幕上拖动图像_Python - Fatal编程技术网

Python 如何使用pygame在屏幕上拖动图像

Python 如何使用pygame在屏幕上拖动图像,python,Python,我希望能够抓住一个图像点击它与鼠标,然后将它拖到屏幕上的一个位置。释放鼠标按钮时,它将被放下。这张图片是一艘我想用作精灵的宇宙飞船,有多艘宇宙飞船。这是一个战列舰游戏,这是游戏的一部分,你可以把你的战舰拖到棋盘上想要的位置。我正在使用python 3.3.2、pygame和windows 10。谢谢你的帮助!以下是我到目前为止的情况: def instructions_screen(): intro = True while intro: for event

我希望能够抓住一个图像点击它与鼠标,然后将它拖到屏幕上的一个位置。释放鼠标按钮时,它将被放下。这张图片是一艘我想用作精灵的宇宙飞船,有多艘宇宙飞船。这是一个战列舰游戏,这是游戏的一部分,你可以把你的战舰拖到棋盘上想要的位置。我正在使用python 3.3.2、pygame和windows 10。谢谢你的帮助!以下是我到目前为止的情况:

def instructions_screen():

    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type ==QUIT:
                pygame.screen.quit()
                exit()

        screen.blit(background, (0, 0))
        largeText = pygame.font.Font('SF Distant Galaxy.ttf',36)
        TextSurf, TextRect = text_objects("Instructions:", largeText)
        TextRect.center = ((display_width/2), (75))

        screen.blit(instruction_text, (280, 120))

        button("Back",0,670,200,50,light_grey,white,"Back_instructions")


        screen.blit(TextSurf, TextRect)
        pygame.display.update()
        clock.tick(30)


def ship_select_screen():
    top_margin = 120
    left_margin = 390

    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.screen.quit()
                exit()

        screen.blit(background, (0, 0))
        largeText = pygame.font.Font('SF Distant Galaxy.ttf',36)
        TextSurf, TextRect = text_objects("Ship selection:", largeText)
        TextRect.center = ((display_width/2), (75))
        screen.blit(TextSurf, TextRect)        
        largeText = pygame.font.Font('SF Distant Galaxy.ttf',36)
        TextSurf, TextRect = text_objects("Ships to place:", largeText)
        TextRect.center = ((200), (150))

        button("Back",0,670,200,50,light_grey,white,"Back_instructions")

    #width and height of each grid location
        WIDTH = 40
        HEIGHT = 40
        #margin between each cell
        MARGIN = 10
        #colours:
        BLACK = (0, 0, 0)
        WHITE = (255, 255, 255)
        GREEN = (0, 255, 0)
        RED = (255, 0, 0)
        # Create a 2 dimensional array.
        grid = []
        for row in range(10):
            # Add an empty array that will hold each cell
            # in this row
            grid.append([])
            for column in range(10):
                grid[row].append(0)  # Append a cell

        while True:
            #draw the grid
            for row in range(10):
                for column in range(10):
                    colour = WHITE
                    if grid[row][column] == 1:
                        colour = GREEN
                    pygame.draw.rect(screen,
                                     colour,
                                     [(((MARGIN + WIDTH) * column) + left_margin),
                                      (((MARGIN + HEIGHT) * row) + top_margin),
                                      WIDTH,
                                      HEIGHT])

            for event in pygame.event.get():
                pos = pygame.mouse.get_pos()
                if event.type == QUIT:
                    exit()
                elif event.type == pygame.MOUSEBUTTONDOWN and pos[0] > 390 and pos[0] < 890 and pos[1] > 120 and pos[1] < 620:
                    column = (pos[0]-left_margin) // (WIDTH + MARGIN)
                    row = (pos[1]-top_margin) // (HEIGHT + MARGIN)
                    # Set that location to zero
                    grid[row][column] = 1
                    print("Click ", pos, "Grid coordinates: ", row, column)
                    print(grid)

            global mouse_button_down
            mouse_button_down = pygame.mouse.get_pressed()


            global mouse_pos
            mouse_pos = pygame.mouse.get_pos()

            global aircraft_x
            aircraft_x = 70

            global aircraft_y
            aircraft_y = 185


            aircraft_carrier()



            screen.blit(TextSurf, TextRect)
            pygame.display.update()
            clock.tick(15)




    button("Back",0,670,200,50,light_grey,white,"Back_game")


def aircraft_carrier():
    global aircraft_x
    global aircraft_y
    global mouse_button_down
    global mouse_pos

    running = True

    while running:
        if mouse_button_down[0] == True and mouse_pos[0] > abs(aircraft_x - aircraft_x) + 250 and mouse_pos[1] > aircraft_y and mouse_pos[1] < aircraft_y + 40:
            print("Running!")
            aircraft_x = (aircraft_x + abs(aircraft_x - mouse_pos[0]))
            aircraft_y = (aircraft_y + abs(aircraft_y - mouse_pos[1]))
            screen.blit(aircraft_carrier_sprite, (aircraft_x, aircraft_y))

        elif mouse_button_down[0] != True:
            running = False

        pygame.display.update()
        clock.tick(30)      
def指令_屏幕():
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.screen.quit()
退出()
屏幕光点(背景,(0,0))
largeText=pygame.font.font('SF distance Galaxy.ttf',36)
TextSurf,TextRect=text_对象(“说明:”,largeText)
TextRect.center=((显示宽度/2),(75))
屏幕blit(说明文字,(280,120))
按钮(“后退”,0670200,50,浅灰色,白色,“后退指示”)
screen.blit(TextSurf,TextRect)
pygame.display.update()
时钟滴答(30)
def ship_选择_屏幕():
上边距=120
左边距=390
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.screen.quit()
退出()
屏幕光点(背景,(0,0))
largeText=pygame.font.font('SF distance Galaxy.ttf',36)
TextSurf,TextRect=text_对象(“装运选择:”,largeText)
TextRect.center=((显示宽度/2),(75))
screen.blit(TextSurf,TextRect)
largeText=pygame.font.font('SF distance Galaxy.ttf',36)
TextSurf,TextRect=text_对象(“发货地点:”,largeText)
TextRect.center=(200),(150))
按钮(“后退”,0670200,50,浅灰色,白色,“后退指示”)
#每个网格位置的宽度和高度
宽度=40
高度=40
#每个单元格之间的边距
保证金=10
#颜色:
黑色=(0,0,0)
白色=(255,255,255)
绿色=(0,255,0)
红色=(255,0,0)
#创建一个二维数组。
网格=[]
对于范围(10)中的行:
#添加将容纳每个单元格的空数组
#在这一排
grid.append([])
对于范围(10)中的列:
网格[行].追加(0)#追加单元格
尽管如此:
#画网格
对于范围(10)中的行:
对于范围(10)中的列:
颜色=白色
如果网格[行][列]==1:
颜色=绿色
pygame.draw.rect(屏幕,
颜色,
[((边距+宽度)*列)+左_边距),
((边距+高度)*行)+顶边距),
宽度,
高度])
对于pygame.event.get()中的事件:
pos=pygame.mouse.get_pos()
如果event.type==退出:
退出()
elif event.type==pygame.MOUSEBUTTONDOWN和pos[0]>390和pos[0]<890和pos[1]>120和pos[1]<620:
列=(位置[0]-左边距)/(宽度+边距)
行=(位置[1]-顶部页边)/(高度+页边)
#将该位置设置为零
网格[行][列]=1
打印(“单击”,位置,“网格坐标:”,行,列)
打印(网格)
全局鼠标按钮按下
mouse\u button\u down=pygame.mouse.get\u pressed()
全局鼠标位置
mouse\u pos=pygame.mouse.get\u pos()
全球航空公司
飞机x=70
全球航空公司
飞机y=185
航空母舰()
screen.blit(TextSurf,TextRect)
pygame.display.update()
时钟滴答(15)
按钮(“后退”,0670200,50,浅灰色,白色,“后退游戏”)
def航空母舰():
全球航空公司
全球航空公司
全局鼠标按钮按下
全局鼠标位置
运行=真
运行时:
如果鼠标按钮按下[0]==True,鼠标位置[0]>abs(飞机x-飞机x)+250,鼠标位置[1]>aircraft y和鼠标位置[1]