我希望能够通过鼠标单击图像来抓取图像,然后将其拖动到屏幕上的某个位置。当鼠标按钮被释放时,它将被丢弃。图像是我想用作精灵的太空飞船,并且有多个宇宙飞船。这是一个战舰游戏,这是游戏的一部分,你将你的船拖到板上所需的位置。我正在使用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)