我剛剛學習Python和Pygame,並且一直在研究一個非常簡單的遊戲,您需要玩一個鼠標,以避免每隔8秒產生一次並在屏幕上漫遊的蛇。現在我試圖讓我的鼠標精靈面對它正在移動的方向。圖像讓鼠標朝下,我已經嘗試了一些關於如何讓它面對正確的方向的想法,但沒有任何工作,希望有人能幫助我。2D精靈面對它在Pygame中移動的方向
這是代碼,沒有碰撞檢測,但鼠標的基本代碼仍然存在。
#!/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()