- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个 roguelike 游戏,但在编码方面我是初学者。我已经让我的角色移动了,我的墙壁和地板 Sprite ,但我的代码中有一些错误,允许角色穿过墙壁。
我用了 block_path
在地板和墙砖之间进行选择,然后我尝试使用它来识别墙,但它并没有真正起作用。
接下来,你可以看到我的代码:
screenWidth = 800
screenHeight = 600
mapHeight = 30
mapWidth = 30
cellWidth = 32
cellHeight = 32
screen = pygame.display.set_mode((screenWidth, screenHeight))
walkRight = [pygame.image.load('model/r1.png'), pygame.image.load('model/r2.png'), pygame.image.load('model/r3.png'), pygame.image.load('model/r4.png'), pygame.image.load('model/r5.png'), pygame.image.load('model/r6.png')]
walkLeft = [pygame.image.load('model/l1.png'), pygame.image.load('model/l2.png'), pygame.image.load('model/l3.png'), pygame.image.load('model/l4.png'), pygame.image.load('model/l5.png'), pygame.image.load('model/l6.png')]
walkUp = [pygame.image.load('model/u1.png'), pygame.image.load('model/u2.png'), pygame.image.load('model/u3.png'), pygame.image.load('model/u4.png'), pygame.image.load('model/u5.png'), pygame.image.load('model/u6.png')]
Floor = pygame.image.load("map/floor.jpg")
wallRight = pygame.image.load("map/rightwall.png")
class struc_Tile():
def __init__(self,block_path):
self.block_path = block_path`
class player(object):
def __init__(self,x,y,width,height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.up = False
self.down = False
self.walkCount = 0
def draw(self,screen):
if self.walkCount + 1 >= 18:
self.walkCount = 0
elif self.left:
screen.blit(walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
screen.blit(walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.up:
screen.blit(walkUp[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.down:
screen.blit(walkDown[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
else:
screen.blit(Standing[self.walkCount//3], (self.x,self.y))
self.walkCount = 0
def move(self,dx,dy):
if gamemap[self.x + dx][self.y + dy].block_path == False:
self.x += dx
self.y += dy
def createmap():
newmap = [[struc_Tile(False) for y in range(0,mapHeight)] for x in range (0,mapWidth) ]
newmap[10][10].block_path = True
newmap[10][15].block_path = True
return newmap
def drawmap(maptodraw):
for x in range(0,mapWidth):
for y in range(0,mapHeight):
if maptodraw[x][y].block_path == True:
screen.blit(wallRight, (x*cellWidth, y*cellHeight))
else:
screen.blit(Floor, (x*cellWidth, y*cellHeight)
def redrawgamewindow():
screen.blit(bg, (0, 0))
drawmap(gamemap)
character.draw(screen)
pygame.display.update()
pygame.init()
gamemap = createmap()
clock = pygame.time.Clock()
character = player(0, 0, 32,32)
run = True
while run:
clock.tick(18)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and character.x > character.vel:
character.x -= character.vel
character.left = True
character.right = False
character.up = False
character.down = False
character.standing = False
elif keys[pygame.K_RIGHT] and character.x < 800 -character.width - character.vel:
character.x += character.vel
character.left = False
character.right = True
character.up = False
character.down = False
character.standing = False
elif keys[pygame.K_UP] and character.y > character.vel:
character.y -= character.vel
character.left = False
character.right = False
character.up = True
character.down = False
character.standing = False
elif keys[pygame.K_DOWN] and character.y < 600 - character.height - character.vel:
character.y += character.vel
character.left = False
character.right = False
character.up = False
character.down = True
character.standing = False
else:
character.right = False
character.left = False
character.up = False
character.down = False
character.standing = True
redrawgamewindow()
最佳答案
更改您的 createmap()
像这样的函数将在 map 的无聊处创建一个 5 像素的缓冲区。我把它设为 5 像素的原因是因为这就是你的角色运动。
我建议进行一些实际的角色碰撞,以测试您是否超出范围而不管速度如何。
编辑:我已经包含了完整的代码,因为如果我展示它们,我所做的更改会更容易理解。
我已将您的图像列表更改为列表字典,因为这样调用起来更容易。您要求的网格是 30x30。由于您显示的是 32x32 单元格宽度,因此我将 map 更改为 960 x 960。
我已经扩展了您的移动方法来检查碰撞以查看它是否可以在移动之前移动。我也删除了你的 redraw()
功能,因为将重绘向下移动更容易。如果你愿意,你可以重新添加它,但在这个例子中,我删除了它。
import pygame
# Based on the paths in your image
# This should work for your image paths
image = pygame.image.load('model/standing1.png')
Standing = pygame.transform.scale(image, (32, 32))
image = pygame.image.load('map/floor.jpg')
Floor = pygame.transform.scale(image, (32, 32))
image = pygame.image.load('map/rightwall.png')
wallRight = pygame.transform.scale(image, (32, 32))
walkLeft = []
walkRight = []
walkUp = []
walkDown = []
for i in range(1, 19):
image = pygame.image.load('model/r' + str(i) + '.png')
walkRight.append(pygame.transform.scale(image, (32, 32)))
image = pygame.image.load('model/l' + str(i) + '.png')
walkLeft.append(pygame.transform.scale(image, (32, 32)))
image = pygame.image.load('model/u' + str(i) + '.png')
walkUp.append(pygame.transform.scale(image, (32, 32)))
image = pygame.image.load('model/d' + str(i) + '.png')
walkDown.append(pygame.transform.scale(image, (32, 32)))
class struc_Tile():
def __init__(self, block_path):
self.block_path = block_path
class player(object):
def __init__(self,x,y,width,height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.walkCount = 0
# do something like this with your images
# if you keep your lists in a dict you can avoid
# all your boolean direction variables and
# the lengthy if else statement
self.images = {}
self.images['walkleft'] = walkLeft[:]
self.images['walkright'] = walkRight[:]
self.images['walkup'] = walkUp[:]
self.images['walkdown'] = walkDown[:]
def draw(self, screen, direction):
if self.walkCount + 1 >= 18:
self.walkCount = 0
# since standing is not in your dict check for that first
if direction == 'standing':
screen.blit(Standing, (self.x,self.y))
self.walkCount = 0
else:
screen.blit(self.images[direction][self.walkCount], (self.x,self.y))
self.walkCount += 1
def can_move(self, dx, dy):
# with the buffer created around the border of the map
# you shouldn't have issues with
# index out of bounds exceptions
# EDIT: added better collision
new_x = self.x + dx
new_y = self.y + dy
if gamemap[new_x][new_y].block_path == False:
if gamemap[new_x + cellWidth][new_y].block_path == False:
if gamemap[new_x][new_y + cellHeight].block_path == False:
if gamemap[new_x + cellWidth][new_y + cellHeight].block_path == False:
self.x += dx
self.y += dy
return True
def createmap():
newmap = [[struc_Tile(False) for y in range(0, mapHeight)] for x in range (0,mapWidth)]
# give our upper/left borders a cell width buffer
# and our bottom/right borders a 2 cell width buffer
# since blit uses the upper left corner this should account
# for the sprite width
# EDIT: Fixed this to accommodate the better collision
for x in range(0, mapWidth):
for y in range (0, mapHeight):
if y < 32 or y + cellWidth >= mapHeight:
newmap[x][y].block_path = True
elif x < 32 or x + cellWidth >= mapWidth:
newmap[x][y].block_path = True
return newmap
def drawmap(maptodraw):
# only blit at cellwidth and height intervals
for x in range(0, mapWidth, cellWidth):
for y in range(0, mapHeight, cellHeight):
if maptodraw[x][y].block_path == True:
screen.blit(wallRight, (x, y))
else:
screen.blit(Floor, (x, y))
# Added this function which lets you block or unblock a cell
# simply call like gamemap = block_element(5, 10, gamemap)
# this will block the 6th cell on the x axis and 11th on the y axis
# to unblock a cell call it with block=False
def block_element(x, y, maptoblock, block=True):
x_cells = int(mapWidth / cellWidth)
y_cells = int(mapHeight / cellHeight)
start_x = int(x * cellWidth)
start_y = int(y * cellHeight)
end_x = start_x + cellWidth
end_y = start_y + cellHeight
print(start_x, end_x)
if x >= 0 and x < x_cells:
if y >= 0 and y < y_cells:
for x in range(start_x, end_x):
for y in range(start_y, end_y):
maptoblock[x][y].block_path = block
return maptoblock
pygame.init()
mapHeight = 960
mapWidth = 960
cellWidth = 32
cellHeight = 32
screen = pygame.display.set_mode((mapWidth, mapHeight))
gamemap = createmap()
# blocking/unblocking example
gamemap = block_element(5, 10, gamemap)
gamemap = block_element(0, 8, gamemap, block=False)
gamemap = block_element(0, 9, gamemap, block=False)
clock = pygame.time.Clock()
character = player(64, 64, 32, 32)
run = True
while run:
clock.tick(18)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# I just used fill since I didn't have a bg image handy
screen.fill((0, 0, 0))
drawmap(gamemap)
# handle the keypresses
# first check if the character can move that much
# then draw the movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and character.can_move(0 - character.vel, 0):
character.draw(screen, 'walkleft')
elif keys[pygame.K_RIGHT] and character.can_move(character.vel, 0):
character.draw(screen, 'walkright')
elif keys[pygame.K_UP] and character.can_move(0, 0 - character.vel):
character.draw(screen, 'walkup')
elif keys[pygame.K_DOWN] and character.can_move(0, character.vel):
character.draw(screen, 'walkdown')
else:
character.draw(screen, 'standing')
关于python - Pygame 墙壁 Sprite 碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60266066/
我正在用 jQuery Collision 编写这个游戏,它使用键盘按键来移动 div,当一个 div 接触另一个 div 时,它应该防止重叠。 我到底该怎么做? HTML ----
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Java 2D Collision? 嘿,大家好,我有另一篇关于这个问题的帖子刚刚消失了,所以我想我会尝试得到一些关
嘿伙计们,我正在制作一个 2D java 游戏,我正在尝试找出如何制作一个好的碰撞代码。我目前正在使用以下代码: public void checkCollision() { Rect
我的意思是,当我与实体的侧面碰撞并想要跳跃时,我无法向右/向左移动,因为当我与右侧/左侧的实体碰撞时,我有一个标志可以防止这种情况发生,例如所以: 这是我用来检测碰撞的代码: public void
所以我正在运行 collide_mask 检查,以删除与玩家 Sprite 碰撞时的生物实例。它工作得很好。 pygame.sprite.spritecollide(player, mobs, Tru
我正在研究我的砖 block splinter 机,并制作一个适当的碰撞系统,以便使球逻辑地切换方向,我必须检测球与砖 block 的哪一侧碰撞。这是我当前的脚本: int sprite_collid
我做了一个类似颜色切换的游戏。唯一的问题是玩家与每种颜色发生碰撞...... 这是我从github上获取的代码: https://github.com/prometheon/MLNimbleNinja
测试我的游戏,当用户和怪物发生碰撞时,我希望弹出警报但没有成功: function die() { for (var i = 0; i < monster.length; i++) { i
我对 vector 很陌生,这是我第一次真正使用它们进行碰撞检查。这是我的项目,我对如何实现碰撞感到困惑。我目前的碰撞检查和响应代码似乎是……糟糕的设计。 这是我的代码: for(auto it =
我是 javascript 的新手,正在尝试找出如何与球和木板发生碰撞,这将停止游戏并提醒玩家“你输了”。但我只想让红球击中木板,让蓝球不接触地继续前进。这是我正在处理的代码。 (我不介意你是否可以帮
很抱歉提出奇怪的问题,我还是 Android 编程的新手。 我有以下代码: import android.content.DialogInterface.OnClickListener; import
我有 6 个 UIImageView,每个都连接到 UIPanGestureRecognizer,它们都连接到相同的方法。方法是: - (IBAction)handlePan:(UIPanGestur
我想根据某些对象的轴对齐边界框检查视锥体,以粗略检查这些对象是否在视野中。速度不是什么大问题。 最佳答案 我发现构建视锥体的世界空间模型并检查与它的 bbox 碰撞是错误的方法。 一个更简单的方法是以
我项目中的所有这些代码都运行良好,但我遇到了一些问题。当飞机接触到屏幕的边界时,它会在接触后开始旋转。我不知道如何让它在碰到屏幕边界时不旋转。只有在我使用时才会出现这个问题: plane.physic
在应用程序启动时,我在后台线程中删除旧的 CoreData 行,下面是我的代码。我的问题类似于城市街道问题。所以,我有两个实体,Street 和 City,我有一个关系 City > Street,因
我试图不接触穴居人和其他带有碰撞位掩码的图像,但我的穴居人击中了一切。 func addCaveManBitMasks(){ caveManNode.physicsBody?.category
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
如何在 PyGame 中找到字符和图像之间的碰撞?我已经从图像中绘制了一个玩家,并从瓷砖中绘制了墙壁,那么我如何检测这些碰撞? 最佳答案 如果你使用pygame Rect类来表示对象的边界,您可以使用
我正在使用 ftok() 为 C 应用程序使用的共享内存段生成标识符。我有问题,在一个盒子上我与 root 使用的标识符发生冲突。在这种情况下,我可以通过破解代码来修复它,但我想要一个更强大的解决方案
这个问题在这里已经有了答案: JavaScript: Collision detection (10 个回答) 10 个月前关闭。 检测 2 个物体(墙壁)碰撞的好方法。是的,不仅仅是检测,还有进一步
我是一名优秀的程序员,十分优秀!