你如何使用pygame.sprite.Sprite?

时间:2016-01-03 19:44:42

标签: pygame sprite

我目前正在使用单独的文件编写一个小游戏,但我遇到pygame.sprite.Sprite的问题。每次我去运行播放器类时,我都会收到此错误AttrbuteError: "module" object has no attribute "sprite"。我在这个网站上做了很多搜索,并且还在pygame网站上查找了答案,但我发现其他一些代码和答案并没有解决我当前的问题。如果有人能帮助我解决我的问题,那就太棒了。 这是我的玩家课程:

import pygame
import pygame.sprite as Sprite
class player(Sprite.sprite):
   def _init_(self,plyrmovement,plyrhealth,plyrdirection):
      Sprite.Sprite._init_(self)
      def plyrmovement(left,right):
          position=[100,100]
          direct_left = 'player left'
          direct_right = 'player right'
          def right():
              if event.key == ord('d'):
                  position[0] += 5
                  plyrdirection = direct_right
                  sprite.sprite='playerleft'
        def left():
            if event.key == ord('a'):
                position[0] -= 5
                plyrdirection = direct_left
                sprite.sprite = 'playerright'
        def shoot(self,bullet):
            if event.key == K_SPACE:
                bullet.bulletmove
        def plyrhealth(self):
            self.health = 100


class bullet(pygame.Sprite.Sprite):
    def _init_(self,bulletmove,bulletdamage):
        pygame.image.load('bullet sprite.png')

1 个答案:

答案 0 :(得分:0)

全名是<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script> <script type="text/javascript"> var angApp = angular.module("app", []); angApp.controller("mainCtrl", function($scope){ $scope.p1 = "Hello1"; $scope.p2 = "Hello2"; }) angular.element(document).ready(function() { angular.bootstrap(document, ['app']); }); function add(){ var tpl = "<div>{{p1}}</div><div>{{p2}}</div>"; $("#container").html(""); $("#container").html(tpl); angular.element(document).injector().invoke(function($compile){ var obj=$('#angApp'); var scope = angular.element($("#angApp")).scope(); $compile(obj.contents())(scope); scope.$digest(); }); var angControllerScope = angular.element($("#angApp")).scope(); angControllerScope.$apply(function() { angControllerScope.p1 = "New Hello"; }); } </script> <body> <button type="button" onclick="add()">Add</button> <div id="angApp" ng-controller="mainCtrl"> <div id="container"> <div>{{p1}}</div> <div>{{p2}}</div> </div> </div> </body>

如果您使用pygame.sprite.Sprite,则正确的名称为import pygame.sprite as Sprite而不是player(Sprite.Sprite) - 请参阅player(Sprite.sprite)中的上方S

至于我,最好在第一个Sprite中使用class player(pygame.sprite.Sprite)bullet(pygame.sprite.Sprite) - 更低s,在第二个sprite中使用更高S - 没有Sprite

import pygame.sprite as Sprite

BTW:

  • import pygame class Player(pygame.sprite.Sprite): def __init__(self,plyrmovement, plyrhealth, plyrdirection): pygame.sprite.Sprite.__init__(self) # ...rest... class Bullet(pygame.sprite.Sprite): def __init__(self, bulletmove, bulletdamage): pygame.sprite.Sprite.__init__(self) # ...rest...
  • 之前和之后必须是两个_
  • 您可以将CamelCase名称用于类 - Player,Bullet - 类似于Sprite,Rect。请参阅PEP8中的建议(PEP 0008 -- Style Guide for Python Code)(PEP8 - Class Names
  • initclass之前使用空行以使代码更具可读性(PEP8 - Blank Lines)。