我正在做一个小游戏。我有一个主菜单,有两个按钮,如果有人按下教程按钮,屏幕将填充黑色,屏幕上会出现一个图像。我希望移动这个图像,尽管我放在键盘指令和它应该移动多少它只是不移动。没有移动hi_image的图像。
# Import pygame
import pygame
# Colors
RED = ( 255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = (0, 0, 255)
PUPLE = ( 217, 0, 255)
BROWN = ( 105, 84, 62)
YELLOW = ( 255, 255, 0)
ORANGE = ( 255, 115, 0)
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
pygame.init()
clock = pygame.time.Clock()
# Screen Dynamics
screen = pygame.display.set_mode([1000,700])
#Title of window
pygame.display.set_caption("Space Victory")
#Variables before main loop
color_white = False
tutorial_white = False
x = 500
speed = (5, 5) # Amount of pixels to move every frame (loop).
moving_up = False
moving_right = False
moving_down = False
moving_left = False
#Positions of Graphics
background_position = [0,0]
start_position = [100,600]
tutorial_position = [700, 600]
hi_position = [x,x]
#The graphics
background_image = pygame.image.load("spacebackground.png").convert()
start_image = pygame.image.load("start.png").convert()
tutorial_image = pygame.image.load("tutorial.png").convert()
hi_image = pygame.image.load("izzat.png").convert()
hi_image.set_colorkey(BLACK)
position = izzat_image.get_rect()
# Sounds
# Main loop ___________________________________________________________
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#The main menu
#The buttons and background image
screen.blit(background_image, background_position)
screen.blit(start_image, start_position)
screen.blit(tutorial_image,tutorial_position)
#If buttons get pressed screen does this.
mouse_pos = pygame.mouse.get_pos()
white_rect = pygame.Rect(100,600,177,44)
tutorial_rect = pygame.Rect(700,600,177,44)
if pygame.mouse.get_pressed()[0] and white_rect.collidepoint(mouse_pos):
color_white = True
if pygame.mouse.get_pressed()[2] and white_rect.collidepoint(mouse_pos):
color_white = False
if color_white:
screen.fill(WHITE)
if pygame.mouse.get_pressed()[0] and tutorial_rect.collidepoint(mouse_pos):
tutorial_white = True
if pygame.mouse.get_pressed()[2] and tutorial_rect.collidepoint(mouse_pos):
tutorial_white = False
if tutorial_white:
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
x -= 10
if event.key == pygame.K_DOWN:
x += 10
if event.key == pygame.K_RIGHT:
x += 10
if event.key == pygame.K_LEFT:
x -= 10
screen.blit(hi_image,hi_position)
#Flip the display
pygame.display.flip()
pygame.time.delay(100)
clock.tick(60)
#Quitting the game
pygame.quit()
答案 0 :(得分:0)
您没有更新菜单位置,只是x。你需要更新实际的菜单位置
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
hi_position[1] -= 10
if event.key == pygame.K_DOWN:
hi_position[1] += 10
if event.key == pygame.K_RIGHT:
hi_position[0] += 10
if event.key == pygame.K_LEFT:
hi_position[0] -= 10