2017-12-07 12 views
1

現在、自分のコードで書いた指定された場所にタンクを描こうとしています。特定の場所にタンクを描く

コード:

import pygame, assetloader 
from pygame.locals import * 
import random, time, math 
import pygame 

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

wallRects = [] 

maze = [[] for i in range(25)] 

assetloader.set_asset_path("assets/") 

def generateMaze(): 
    global grid 
    width = 12 
    height = 12 
    seed = time.time() 

    random.seed(seed) 

    grid = [[0 for j in range(width)] for i in range(height)] 

    N, S, E, W = 1, 2, 4, 8 
    DX   = { E: 1, W: -1, N: 0, S: 0 } 
    DY   = { E: 0, W: 0, N: -1, S: 1 } 
    OPPOSITE = { E: W, W: E, N: S, S: N } 

    def carve_passages_from(cx, cy, grid): 
     directions = random.sample([N, S, E, W], 4) 

     for direction in directions: 
      nx, ny = cx + DX[direction], cy + DY[direction] 

      if (ny >= 0 and ny <= len(grid) - 1) and (nx >= 0 and nx <= len(grid[0]) - 1) and grid[ny][nx] == 0: 
       grid[cy][cx] += direction 
       grid[ny][nx] += OPPOSITE[direction] 
       carve_passages_from(nx, ny, grid) 

    carve_passages_from(0, 0, grid) 

    global maze 
    maze[0] = [2 for i in range(25)] 
    mrow = 1 

    # 0 = path 
    # 1 = vertical wall 
    # 2 = horizontal wall 
    for y in range(height): 
     row1 = maze[mrow] 
     row2 = maze[mrow+1] 
     row1.append(1) 
     row2.append(1) 
     for x in range(width): 
      if grid[y][x] & S != 0: 
       row1.append(0) 
       row2.append(0) 
      else: 
       row1.append(0) 
       row2.append(2) 
      if grid[y][x] & E != 0: 
       if (grid[y][x] | grid[y][x+1]) & S != 0: 
        row1.append(0) 
        row2.append(0) 
       else: 
        row1.append(0) 
        row2.append(2) 
      else: 
       row1.append(1) 
       if y < height - 1: 
        row2.append(1) 
       else: 
        row2.append(2) 
     mrow += 2 

def printMaze(): 
    for row in range(len(maze)): 
     for col in range(len(maze[row])): 
      print maze[row][col], 
     print 

def CreateWallRects(): 
    for row in range(len(maze)): 
     for col in range(len(maze[row])): 
      if maze[row][col] == 1 and row < 23: 
       x = (start_x + col * gsize) + (0.5 * gsize) - 1 
       y = (start_y + (row - 1) * gsize) + (0.5 * gsize) 
       wallRects.append(pygame.Rect(x, y, 3, gsize)) 
      elif maze[row][col] == 1 and row == 23: 
       x = (start_x + col * gsize) + (0.5 * gsize) - 1 
       y = (start_y + (row - 1) * gsize) + (0.5 * gsize) 
       wallRects.append(pygame.Rect(x, y, 3, gsize * 2)) 
      elif maze[row][col] == 2: 
       x = (start_x + (col-1) * gsize) + (0.5 * gsize) 
       y = (start_y + row * gsize) + (0.5 * gsize) - 1 
       wallRects.append(pygame.Rect(x, y, gsize * 2, 3)) 

def drawMaze(): 

    lineColor = 255, 255, 255 

    for x in range(0, width, gsize): 
     pygame.draw.line(screen, lineColor, (x, 0), (x, height), 3) #line(Surface, color, start_pos, end_pos, width=1) 

    for y in range(0, height, gsize): 
     pygame.draw.line(screen, lineColor, (0, y), (width, y), 3) 

    wallColor = 0, 0, 0 
    for wr in wallRects: 
     pygame.draw.rect(screen, wallColor, wr)  

class Player(pygame.sprite.Sprite): 
    def __init__(self, x, y, pos): 
     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 
     self.rows = pos[0] 
     self.cols = pos[1] 
     self.x = self.cols * gsize 
     self.y = self.rows * gsize 
#  self.orig_image, self.orig_rect = assetloader.load_image(img_name, -1) 
     self.orig_rect.x = self.x 
     self.orig_rect.y = self.y 
     self.orig_gun_pos = self.orig_rect.midtop 
     self.ammo = 5 
     self.vel = [0, 0] 
     self.dead = False 

    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) 

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 = Player(width/2, height/4, pos) 

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() 

     screen.fill(bgColor) 

     p.draw(screen) 

     pygame.display.flip() 

pygame.quit() 

私はクラスのプレーヤーのために使用されるinit関数は、4つの物事(self, x, y, pos)が含まれていることを知っている、と私はそれが今置か取得する場所を定義するPlayer(width/2, height/4, pos)を持っている - しかし、それはposが未定義であると言います。ですから、どこかで "pos"を代入したり定義したりするにはどうすればよいでしょうか?

今、私は取得しています:

Traceback (most recent call last): 
    File "tinytanks3.py", line 375, in <module> 
    p = Player(width/2, height/4, pos) 
NameError: name 'pos' is not defined 

を誰かがposと理由のために置くために何を説明してくださいことはできますか?

答えて

1

Playerクラスの__init__()メソッドから、posがクラスのインスタンスの作成時に指定する必要がある引数であることがわかります。

この要素は、作成されたプレーヤーオブジェクトの列と行の属性を指定するエントリの2要素タプルである必要があります。これは、__init__()メソッドの別の場所で、pos[0]pos[1]という参照があるという事実から判断できます。

例えば、

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

は、= 3 p.rowsp.columns = 4

こと、 p名前プレイヤオブジェクトを作成するには
関連する問題