- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我很可能没有正确初始化矩形,因此玩家不断地“墙跳”,这不是有意的。我很可能没有正确初始化矩形,因此玩家不断地“跳墙”,这不是有意的。我很可能没有正确初始化矩形,因此玩家不断地“墙跳”,这不是有意的。
这是主要内容:
import pygame
import wall
import Player
import math
pygame.init()
pygame.display.set_caption("Tanki")
pygame.display.set_icon(pygame.image.load("mainIcon.png"))
running = True
screen = pygame.display.set_mode((300, 500))
WALLS = pygame.sprite.Group()
fence = \
[wall.Wall(0, 0, 300, 10, pygame.Color(255, 255, 255), screen),
wall.Wall(290, 0, 10, 500, pygame.Color(255, 255, 255), screen),
wall.Wall(0, 0, 10, 500, pygame.Color(255, 255, 255), screen),
wall.Wall(0, 490, 300, 10, pygame.Color(255, 255, 255), screen),
# boundry
wall.Wall(10 + 60, 235, 50, 10, pygame.Color(255, 255, 255), screen),
wall.Wall(10 + 60 + 50 + 60, 235, 50, 10, pygame.Color(255, 255, 255), screen),
# middle blocks
wall.Wall(100, ((245 + 500 - 60) / 2) - 5, 100, 10, pygame.Color(255, 255, 255), screen),
wall.Wall(100, ((235 + 60) / 2) - 5, 100, 10, pygame.Color(255, 255, 255), screen),
wall.Wall(10 + 66.666, 440, 66.666 + 80, 10, pygame.Color(255, 255, 255), screen),
wall.Wall(10 + 66.666, 430, 10, 10, pygame.Color(255, 255, 255), screen),
wall.Wall(10 + 66.666 * 2 + 80 - 11, 430, 10, 10, pygame.Color(255, 255, 255), screen),
# everythin around the goal
wall.Wall(10 + 66.666 + 10, 430, 66.666 + 80 - 11 - 9, 10, pygame.Color(0, 0, 200), screen),
# the bottom goal MAKE A CLASS FOR THIS
wall.Wall(10 + 66.666, 50, 66.666 + 80, 10, pygame.Color(255, 255, 255), screen),
wall.Wall(10 + 66.666, 60, 10, 10, pygame.Color(255, 255, 255), screen),
wall.Wall(10 + 66.666 * 2 + 80 - 11, 60, 10, 10, pygame.Color(255, 255, 255), screen),
# everythin around the goal
wall.Wall(10 + 66.666 + 10, 60, 66.666 + 80 - 11 - 9, 10, pygame.Color(200, 0, 0), screen)
# the top goal
]
tmp = Player.Player(pygame.Color(0, 255, 0), 20, 20, 140, 455, 0.05, 180, 0)
pro = Player.Player(pygame.Color(0, 255, 0), 20, 20, 140, 455, 0.05, 180, tmp)
for eachwall in fence:
WALLS.add(eachwall)
count = 1
while running:
count += 1
print(pro.getAngle() % 360)
if (count % 10 == 0):
tmp.updatetmp(pro)
screen.fill(pygame.Color(0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pro.setAngle(pro.getAngle() - 10)
if event.key == pygame.K_RIGHT:
pro.setAngle(pro.getAngle() + 10)
if event.key == pygame.K_UP:
pro.setVel(0.05)
if event.key == pygame.K_DOWN:
pro.setVel(-0.05)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
pro.setVel(0)
b = True
for block in WALLS:
if (pygame.sprite.collide_rect(pro, block)):
b = False
print("A")
a = pro.update(WALLS, screen,b)
for awall in WALLS:
awall.draw()
pygame.display.update()
这是玩家
import pygame
import math
class Player(pygame.sprite.Sprite):
direction = 'right'
def __init__(self, color, width, height, x, y, velocity, angle, past):
# Pygame constructor
pygame.sprite.Sprite.__init__(self)
self.past = past
# Init. variables
self.angle = angle
self.velocity = velocity
self.color = color
self.width = width
self.height = height
self.x = x
self.y = y
# Create sprite
self.image = pygame.Surface([width, height])
self.image.fill(self.color)
self.rect = self.image.get_rect(topleft = (self.x, self.y))
#############################
def setColor(self, color):
self.color = color
def setX(self, X):
self.x = X
def setY(self, Y):
self.y = Y
def setVel(self, vel):
self.velocity = vel
def setAngle(self, ANGLE):
self.angle = ANGLE
##############################
def getColor(self):
return self.color
def getX(self):
return self.x
def getY(self):
return self.y
def getVel(self):
return self.velocity
def getAngle(self):
return self.angle
#################################
def draw(self, screen):
# Draw player on the screen
pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height], 0)
def deg_to_rad(self):
return int(self.angle * math.pi) / 180
def rad_to_offset(self, offset): # insert better func name.
radians = self.deg_to_rad()
x = math.cos(radians) * offset
y = math.sin(radians) * offset
return [x, y]
def move(self, walls, go):
if go:
forward = self.rad_to_offset(self.velocity)
self.x += forward[0]
self.y += forward[1]
else:
print("collided")
def update(self, walls, screen, go):
if(self.angle>=360):
self.angle = self.angle%360
hit = self.move(walls, go)
self.draw(screen)
return hit
def updatetmp(self, Player):
self.angle = Player.getAngle()
self.velocity = Player.getVel()
self.color = Player.getColor()
self.x = Player.getX()
self.y = Player.getY()
这是 Wall 类
import pygame
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color, screen):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.screen = screen
self.image = pygame.Surface((width, height))
self.image.fill(color)
self.rect = self.image.get_rect(topleft = (self.x, self.y))
def get_rectangle(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
def draw(self):
pygame.draw.rect(self.screen, self.color, (self.x, self.y, self.width, self.height), 0)
最佳答案
类似 pygame.sprite.collide_rect()
的操作或pygame.sprite.spritecollide()
使用 Sprite
的 .rect
属性对象执行碰撞测试。
对象播放器由浮点属性 .x
和 .y
定位。您必须将此位置同步到更新中的(整体).rect
属性,以使碰撞测试正常工作:
class Player(pygame.sprite.Sprite):
# [...]
def update(self, walls, screen, go):
if(self.angle>=360):
self.angle = self.angle%360
hit = self.move(walls, go)
self.rect.topleft = (round(self.x), round(self.y)) # <-----
self.draw(screen)
return hit
此外,没有必要迭代所有WALLS
:
for block in WALLS:
if pygame.sprite.collide_rect(pro, block):
b = False
print("A")
使用pygame.sprite.spritecollide()
而不是``pygame.sprite.collide_rect()`
if pygame.sprite.spritecollide(pro, WALLS, False):
b = False
print("A")
<小时/>
请注意,没有必要在 Player
和 Wall
中实现 draw()
方法。您可以使用 draw()
Group
的方法。这个方便的方法使用所包含的 Sprite 对象的 .rect
和 .image
属性来绘制它们。
删除Player
和Wall
的draw
方法。将玩家和墙添加到组中并在组上调用 draw()
:
fence = \
[wall.Wall(0, 0, 300, 10, pygame.Color(255, 255, 255), screen),
# [...]
]
tmp = Player.Player(pygame.Color(0, 255, 0), 20, 20, 140, 455, 0.05, 180, 0)
pro = Player.Player(pygame.Color(0, 255, 0), 20, 20, 140, 455, 0.05, 180, tmp)
WALLS = pygame.sprite.Group(fence)
players = pygame.sprite.Group([tmp, pro])
count = 1
while running:
count += 1
# [...]
WALLS.draw(screen)
players.draw(screen)
pygame.display.update()
关于python - pygame 中的碰撞帮助以及当您使用角度和速度移动时如何阻止它撞到墙壁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60305870/
我正在用 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 个物体(墙壁)碰撞的好方法。是的,不仅仅是检测,还有进一步
我是一名优秀的程序员,十分优秀!