0

我只是在学习 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()
4

2 回答 2

1

为了让鼠标看向正确的方向,我会尝试pygame.transform.rotate并使旋转度数取决于directionxanddirectiony变量大于或小于零。

于 2011-09-07T07:32:58.497 回答
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' 它是它的目的地的元组。

于 2013-08-19T06:03:25.133 回答