我只是在学习Python和Pygame,并且一直致力于一个非常简单的游戏,你可以在这里玩鼠标,避免每隔8秒产生一次并在屏幕上漫游的蛇。现在我正试图让我的鼠标精灵面对它正在移动的方向。图像让鼠标朝下,我已经尝试了几个关于如何让它面向正确方向的想法,但没有任何效果,希望有人可以帮助我。
这是代码,还没有碰撞检测,但鼠标的基本代码仍在那里。
#!/usr/bin/python
#Imports
import os, sys, math
import pygame, random
from pygame.locals import *
#Sets path for game images
main_dir = os.path.split(os.path.abspath(__file__))[0]
data_dir = os.path.join(main_dir, 'data')
pygame.init()
#Main screen and background
width, height = 1000, 800
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Don't Get Eaten")
background = pygame.image.load(os.path.join(data_dir, 'liquid.bmp'))
background = pygame.transform.scale(background, (width, height))
screen.blit(background, (0,0))
#Game time setup
clock = pygame.time.Clock()
pygame.time.set_timer(USEREVENT+1, 8000)
#Functions to assist in image loading
def load_image(file):
file = os.path.join(data_dir, file)
surface = pygame.image.load(file)
return surface.convert_alpha()
def load_images(*files):
imgs = []
for file in files:
imgs.append(load_image(file))
return imgs
#Class for player mouse
class Mouse(pygame.sprite.Sprite):
speed = 6
def __init__(self):
image = load_image('mouse.png')
image = pygame.transform.scale(image, (60,60))
pygame.sprite.Sprite.__init__(self,self.groups)
self.image = image
self.rect = self.image.get_rect()
surface = pygame.display.get_surface()
self.rect.center = (width/2, height/2)
self.area = surface.get_rect()
def movex(self, directionx):
self.rect.move_ip(directionx*self.speed, 0)
def movey(self, directiony):
self.rect.move_ip(0, -directiony*self.speed)
def update(self):
pass
#Class for the snake enemy
class Snake(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self,self.groups)
image = load_image(random.choice(['snake1.png','snake2.png','snake3.png']))
image = pygame.transform.scale(image, (85,85))
self.image = image
self.rect = self.image.get_rect()
surface = pygame.display.get_surface()
self.area = surface.get_rect()
self.movex = random.randint(1,5)
self.movey = random.randint(1,5)
def update(self):
self.rect = self.rect.move(self.movex,self.movey)
if self.rect.left < self.area.left or self.rect.right > self.area.right:
self.movex = -self.movex
newpos = self.rect.move((self.movex, 0))
self.image = pygame.transform.flip(self.image, 1, 0)
self.rect = newpos
if self.rect.top < self.area.top or self.rect.bottom > self.area.bottom:
self.movey = -self.movey
newpos = self.rect.move((0, self.movey))
self.rect = newpos
#Pregame setup
allsprites = pygame.sprite.Group()
snakesprites = pygame.sprite.Group()
mousesprites = pygame.sprite.Group()
Snake.groups = allsprites, snakesprites
Mouse.groups = allsprites, mousesprites
mouse = Mouse()
snake = Snake()
def main():
#Main game loop
while 1:
#Setup and blits
seconds = clock.tick()/1000.0
screen.blit(background, (0,0))
#Game events
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == USEREVENT+1:
Snake()
keystate = pygame.key.get_pressed()
directionx = keystate[K_RIGHT] - keystate[K_LEFT]
directiony = keystate[K_UP] - keystate[K_DOWN]
mouse.movex(directionx)
mouse.movey(directiony)
allsprites.update()
allsprites.draw(screen)
pygame.display.update()
pygame.display.flip
clock.tick(60)
if __name__ == '__main__':
main()
答案 0 :(得分:1)
要让鼠标朝正确方向看,我会尝试pygame.transform.rotate并使旋转度取决于directionx
和directiony
变量大于或小于零。
答案 1 :(得分:0)
这是我的一个项目的一些示例代码,它使用三角函数来旋转图像。
self.deltaX = pos[0] - des[0]
self.deltaY = pos[1] - des[1]
self.angle = math.atan2(self.deltaX, self.deltaY) *180 / math.pi
self.image = pygame.transform.rotate(self.image, self.angle)
pos是(在你的情况下是鼠标)的位置,'des'是它的目的地的元组。