目前,我正在尝试让我的坦克在我在代码中编写的指定位置进行绘制。
代码:
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()
我知道我用于类Player的init函数包含四个(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的内容和原因吗?
答案 0 :(得分:1)
您可以从Player类的__init__()
方法中看到pos
是您在创建类实例时需要指定的参数。
它应该是一个2元素的整数元组,其条目指定创建的播放器对象的列和行属性。您可以通过__init__()
方法的其他地方告诉您pos[0]
和pos[1]
。
例如,
p = Player(width/2, height/2, (3,4))
将创建一个名为p
的玩家对象,使p.rows
= 3且p.columns
= 4。