import pygame,sys
pygame.init()
screen = pygame.display.set_mode((1280,720))
image = pygame.Surface((32,64))
image.fill('green')
rect = image.get_rect(center = (640,360))
direction = pygame.math.Vector2()
pos = pygame.math.Vector2(rect.center)
speed = 2
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
direction.y = -1
if keys[pygame.K_DOWN]:
direction.y = 1
if not keys[pygame.K_UP] and not keys[pygame.K_DOWN]:
direction.y = 0
if keys[pygame.K_LEFT]:
direction.x = -1
if keys[pygame.K_RIGHT]:
direction.x = 1
if not keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT]:
direction.x = 0
if keys[pygame.K_q]:
print('这边是重点')
if direction.magnitude() > 0:
direction = direction.normalize()
pos.x += direction.x * speed
rect.centerx = pos.x
pos.y += direction.y * speed
rect.centery = pos.y
screen.fill((0,0,0))
screen.blit(image,rect)
pygame.display.flip()
clock.tick(60)