我正在尝试使用python和pygame制作游戏,我需要一个精灵能够从其中心旋转,然后以其面对的任何方向向前移动,并以相同的方式向后移动。 Sprite旨在模仿机器人及其运动。
目前,它可以很好地上下移动,但是每次单击旋转按钮时图像都会失真。
我还是python和pygame的新手,因此非常感谢您能通过一种简单的方法来解决我的问题! 谢谢。
import pygame
pygame.init()
pygame.mixer.init() # this will allow game to have sound
#basic settings
grey = (227,221,220)
black = (0,0,0) # defining colour scheme of the game
displayWidth = 1000
displayLength = 600
lead_x = displayWidth/2 - 50 # 100 is sprite length and width, so 50 is half of the sprite
lead_y = displayLength - 100 # starting position of robot
vel = 2 # vel = velocity: the number of pixels it will move with each movement
rotSpeed = 15 # rotation speed
rot = 0
gameDisplay = pygame.display.set_mode((displayWidth,displayLength)) # game screen size
pygame.display.set_caption('Robot Race') # name of window
robot = pygame.image.load('robotproto.png')
gameExit = False
while gameExit == False: # defining main loop
pygame.time.delay(15)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
keys = pygame.key.get_pressed() # variable affecting keys pressed
# keys for up and down movement
if keys[pygame.K_q] and keys[pygame.K_p] and lead_y > -20:
lead_y -= vel
if keys[pygame.K_a] and keys[pygame.K_l] and lead_y < displayLength - 80:
lead_y += vel
# keys for rotation
rotate = pygame.transform.rotate
if keys[pygame.K_w]:
robot = rotate(robot, 1)
if keys[pygame.K_o]:
robot = rotate(robot, -1)
gameDisplay.fill(grey)
gameDisplay.blit(robot, (lead_x, lead_y))
pygame.display.update()
pygame.quit()
quit()