- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
目前,我正在尝试让我的坦克在我在代码中编写的指定位置绘制。
代码:
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 添加什么内容以及为什么吗?
最佳答案
您可以从 Player 类的 __init__()
方法中看到,pos
是您在创建该类的实例时需要指定的参数。
它应该是一个 2 元素整数元组,其条目指定所创建的玩家对象的列和行属性。您可以从以下事实看出这一点:在 __init__()
方法的其他地方,您引用了 pos[0]
和 pos[1]
.
例如,
p = Player(width/2, height/2, (3,4))
将创建一个名为p
的播放器对象,使得p.rows
= 3且p.columns
= 4。
关于python - 在某个地方抽水箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47686889/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
操作无法完成。 Places API 库中发生内部错误。如果您认为此错误代表错误,请使用我们社区和支持页面 (https://developers.google.com/places/support)
我正在尝试在我的项目中使用 google places,我将其设置在 fragment 中而不是 Activity 中,我的自动完成 fragment 在 fragment 中。但是,当我尝试搜索它时
我的目的是使用R来查询google api。 我有一个地址和名称列表(属于商店、餐馆等),我需要为每个地址和名称存储: “纬度”、“经度”、“业务类型” 我的想法是使用 google place ap
我正在寻找设置一个自动完成的谷歌地方小部件。 我有一个带有“searchFieldText”id 的输入类型文本。 这是我的 JS 代码: var inputsec = document.getEle
是否可以使用图形 API(或地址/ zip )按纬度/经度和半径获取地点?我在文档中的任何地方都看不到它 最佳答案 搜索 URL 的以下格式将返回某个位置附近的地点列表: https://graph.
我正在探索 Google API,主要是 Places API。由于对 Google Places API 的请求数限制为 100,000,因此我正在寻找方法来最大限度地减少发送到 API 的请求数。
伙计们,我在我的应用程序中有一个功能,可以使用 GetFiles 在特定目录中搜索特定文件。方法 System.IO.Directory.GetFiles(string path, string
我已经在 Laravel 5.3 上使用 where 查询成功创建了许多函数,但是这次发生了一些奇怪的事情。 public function show($id){ $artikel = Art
我正在为我的 iPhone 应用程序使用 Facebook 图形 API 来获取附近地点的列表,我使用带有一些参数的“搜索”请求。我得到的响应是一个包含以下信息的地点列表:“纬度”、“经度”、“名称”
我有一个 Android 应用程序,我在其中使用 Google map 显示附近的地方,如加油站、药店等。我正在使用 map 和地点 API。 https://maps.googleapis.com/
我是一名优秀的程序员,十分优秀!