2017-12-05 15 views
-3
import pygame, assetloader 
from pygame.locals import * 
import random, time, math 

GRAD = math.pi/180 
blue = (0, 0, 255) 

assetloader.set_asset_path("assets/") 

class Player(pygame.sprite.Sprite): 
    def __init__(self, x, y): 
     pygame.sprite.Sprite.__init__(self) 

     self.image, self.rect = assetloader.load_image("Tank.png", -1) 
     self.rect.x = x 
     self.rect.y = y 
     self.rect.clamp_ip(screen.get_rect()) 
     self.dir = 0 
     self.vel_y = 0 
     self.vel_x = 0 

    def draw(self, screen): 
     image = pygame.transform.rotate(self.image, self.dir) 
     screen.blit(image, self.rect) 

    def update(self): 
     oldCenter = self.rect.center 

     self.rect = self.image.get_rect() 
     self.rect.center = oldCenter 
     screen_rect = screen.get_rect() 
     keys = pygame.key.get_pressed() 


     if keys[K_UP]: 
      if (0 < self.dir and self.dir < 90) or (-360 < self.dir and self.dir < -270): 
       self.vel_x = -1 
       self.vel_y = -1 
      elif (270 < self.dir and self.dir < 360) or (-90 < self.dir and self.dir < 0): 
       self.vel_x = 1 
       self.vel_y = -1 
      if (90 < self.dir and self.dir < 180) or (-270 < self.dir and self.dir < -180): 
       self.vel_x = -1 
       self.vel_y = 1 

      elif (180 < self.dir and self.dir < 270) or (-180 < self.dir and self.dir < -90): 
       self.vel_x = 1 
       self.vel_y = 1 

      if self.dir == 0 : 
       self.vel_x = 0 
       self.vel_y = -1 
      if self.dir == 90 : 
       self.vel_x = -1 
       self.vel_y = 0 
      if self.dir == 180: 
       self.vel_x = 0 
       self.vel_y = 1 
      if self.dir == 270: 
       self.vel_x = 1 
       self.vel_y = 0 
      self.rect.move_ip(self.vel_x, self.vel_y) 

     elif keys[K_DOWN]: 

      if (0 < self.dir and self.dir < 90) or (-360 < self.dir and self.dir < -270): 
       self.vel_x = 1 

       self.vel_y = 1 
      elif (270 < self.dir and self.dir < 360) or (-90 < self.dir and self.dir < 0): 
       self.vel_x = -1 
       self.vel_y = 1 
      if (90 < self.dir and self.dir < 180) or (-270 < self.dir and self.dir < -180): 
       self.vel_x = 1 
       self.vel_y = -1 

      elif (180 < self.dir and self.dir < 270) or (-180 < self.dir and self.dir < -90): 
       self.vel_x = -1 
       self.vel_y = -1 

      if self.dir == 0 : 
       self.vel_x = 0 
       self.vel_y = 1 
      if self.dir == 90 : 
       self.vel_x = 1 
       self.vel_y = 0 
      if self.dir == 180: 
       self.vel_x = 0 
       self.vel_y = -1 
      if self.dir == 270: 
       self.vel_x = -1 
       self.vel_y = 0 

      self.rect.move_ip(self.vel_x, self.vel_y) 


     if keys[K_LEFT]: 
      self.dir += 5 
      if self.dir > 360: 
       self.dir = 0 
     elif keys[K_RIGHT]: 
      self.dir -= 5 
      if self.dir < -360: 
       self.dir = 0 

     if not screen_rect.contains(self.rect): 
      self.rect.clamp_ip(screen_rect) 

class Enemy(pygame.sprite.Sprite): 
    def __init__(self, x, y): 
     pygame.sprite.Sprite.__init__(self) 

     self.image, self.rect = assetloader.load_image("New Piskel.png", -1) 
     self.rect.x = x 
     self.rect.y = y 
     self.dir = 0 
     self.rect.clamp_ip(screen.get_rect()) 

    def draw(self, screen): 
     image = pygame.transform.rotate(self.image, self.dir) 
     screen.blit(image, self.rect) 

    def update(self): 
     screen_rect = screen.get_rect() 
     keys = pygame.key.get_pressed() 
     if keys[K_w]: 

      if (0 < self.dir and self.dir < 90) or (-360 < self.dir and self.dir < -270): 
       self.vel_x = -1 

       self.vel_y = -1 
      elif (270 < self.dir and self.dir < 360) or (-90 < self.dir and self.dir < 0): 
       self.vel_x = 1 
       self.vel_y = -1 
      if (90 < self.dir and self.dir < 180) or (-270 < self.dir and self.dir < -180): 
       self.vel_x = -1 
       self.vel_y = 1 

      elif (180 < self.dir and self.dir < 270) or (-180 < self.dir and self.dir < -90): 
       self.vel_x = 1 
       self.vel_y = 1 

      if self.dir == 0 : 
       self.vel_x = 0 
       self.vel_y = -1 
      if self.dir == 90 : 
       self.vel_x = -1 
       self.vel_y = 0 
      if self.dir == 180: 
       self.vel_x = 0 
       self.vel_y = 1 
      if self.dir == 270: 
       self.vel_x = 1 
       self.vel_y = 0 
       self.rect.move_ip(self.vel_x, self.vel_y) 

     elif keys[K_s]: 

      if (0 < self.dir and self.dir < 90) or (-360 < self.dir and self.dir < -270): 
       self.vel_x = 1 

       self.vel_y = 1 
      elif (270 < self.dir and self.dir < 360) or (-90 < self.dir and self.dir < 0): 
       self.vel_x = -1 
       self.vel_y = 1 
      if (90 < self.dir and self.dir < 180) or (-270 < self.dir and self.dir < -180): 
       self.vel_x = 1 
       self.vel_y = -1 

      elif (180 < self.dir and self.dir < 270) or (-180 < self.dir and self.dir < -90): 
       self.vel_x = -1 
       self.vel_y = -1 

      if self.dir == 0 : 
       self.vel_x = 0 
       self.vel_y = 1 
      if self.dir == 90 : 
       self.vel_x = 1 
       self.vel_y = 0 
      if self.dir == 180: 
       self.vel_x = 0 
       self.vel_y = -1 
      if self.dir == 270: 
       self.vel_x = -1 
       self.vel_y = 0 

      self.rect.move_ip(self.vel_x, self.vel_y) 


     if keys[K_a]: 
      self.dir += 5 
      if self.dir > 360: 
       self.dir = 0 
     elif keys[K_d]: 
      self.dir -= 5 
      if self.dir < -360: 
       self.dir = 0 

     if not screen_rect.contains(self.rect): 
      self.rect.clamp_ip(screen_rect) 

size = width, height = 500, 400 
gsize = 25 
start_x, start_y = 0, 0 
bgColor = 255, 255, 255 

pygame.init() 
screen = pygame.display.set_mode(size)#, pygame.FULLSCREEN) 
pygame.display.set_caption("Sample Sprite") 
clock = pygame.time.Clock() 

p = (width/2, height/4) 
e = (width/2, height/4) 


coll_font = pygame.font.Font(None, 30) 

going = True 
while going: 
    clock.tick(60) 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      going = False 
     elif event.type == KEYDOWN: 
      if event.key == K_ESCAPE: 
       going = False 

     p.update() 
     e.update() 

     screen.fill(bgColor) 

     p.draw(screen) 
     e.draw(screen) 

     pygame.display.flip() 

pygame.quit() 

コードは私のイメージを画面に描画する前に動作していましたが、今は突然動作しません。
p.update()で何かが間違っていると思います(コードの最後の定義を参照してください)。私はpeの値を(width/2, height/4)と指定しましたが、これは意味をなさないものの、これまでは動作していましたが、現在はありません。誰でも説明できますか?Pygame - 'Tuple'オブジェクトに 'update'属性がありません

+1

エラーに関する質問を投稿する場合は、*正確で完全な*エラーメッセージを含めてください。再入力しないでください。 "'Tuple'オブジェクトに 'update'属性がありません"が歪んでスタックトレースがありません。 – user2357112

+1

また、免疫タブの定義を探すために、[mcve]も提供してください。 – Shadow

答えて

1

ここでの問題は、pがタプルであることです。 pをプレーヤークラスのインスタンスにしたいと思っています。あなたは、以下の記述する必要があります。私はあなたが敵クラスのインスタンスになりたいと仮定eため同様に

p = Player(width/2, height/4)

。それがそのままで、pはタプルであり、メソッドの更新はありません。

関連する問題