- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经让敌人随机移动并看着玩家,现在我想让敌人射击玩家,我不知道为什么,但射击完全随机,正如你在下面的 gif 中看到的那样(按左 ctrl 进行拍摄)。
无论如何,这是 Player 类:
class Player:
# inizialize the player
def __init__(self, x, y, player):
self.x = x
self.y = y
self.image = first_player_spaceship_image
self.life = 6
self.original_player_image = first_player_spaceship_image
self.angle = 0
self.rect = self.image.get_rect()
# move the player
def movePlayer(self):
if key[0]:
self.x -= 10
elif key[1]:
self.x += 10
if key[2]:
self.y -= 10
elif key[3]:
self.y += 10
# check borders
if self.x <= 0:
self.x = 0
if self.x + shooting_enemy_image.get_width() >= display_width:
self.x = display_width - first_player_spaceship_image.get_width()
if self.y <= 0:
self.y = 0
if self.y + first_player_spaceship_image.get_height() >= display_height:
self.y = display_height - first_player_spaceship_image.get_height()
# rotate the player where the mouse is aiming
def rotate(self):
mouse_x, mouse_y = pygame.mouse.get_pos()
rel_x, rel_y = mouse_x - self.x, mouse_y - self.y
self.angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 90
self.image = pygame.transform.rotate(self.original_player_image, int(self.angle))
orig_center = self.original_player_image.get_rect(topleft=(self.x, self.y)).center
self.rect = self.image.get_rect(center=orig_center)
# draw the player
def drawPlayer(self):
screen.blit(self.image, self.rect.topleft)
这是我的敌人类别:
class ShootingEnemy:
# inizialize the enemy
def __init__(self):
self.image = shooting_enemy_image
self.new_shooting_enemy_image = shooting_enemy_image
self.x = display_width // 2
self.y = 0
self.begin_down = True
self.choices = 0
self.timer = 51
self.left_again = False
self.right_again = False
self.up_again = False
self.down_again = False
self.rect = shooting_enemy_image.get_rect()
self.angle = 0
# draw the shooting enemy
def continueMoveShootingEnemy(self):
if self.y < 30:
self.y += 1
if self.y == 30:
self.begin_down = False
self.y += 1
else:
if not self.begin_down and self.timer > 20:
self.choices = random.choice([1, 2, 3, 4])
self.timer = 0
self.timer += 1
if self.left_again:
self.x += 1
self.left_again = False
if self.right_again:
self.x -= 1
self.right_again = False
if self.up_again:
self.y += 0
self.up_again = False
if self.down_again:
self.y -= 1
self.down_again = False
else:
if self.choices == 1:
if self.x >= display_width:
self.timer = 21
self.right_again = True
else:
if self.y < 140 and self.y > 10:
self.y += 1
self.x += 4
else:
self.x += 4
if self.choices == 2:
if self.x <= 0:
self.timer = 21
self.left_again = True
else:
if self.y < 140 and self.y > 10:
self.y += 1
self.x -= 4
else:
self.x -= 4
if self.choices == 3:
if self.y >= 150:
self.timer = 21
self.down_again = True
else:
self.y += 2
if self.choices == 4:
if self.y <= 0:
self.timer = 21
self.up_again = True
else:
self.y -= 2
# rotate the shooting enemy where the player is
def rotate(self):
temp_x, temp_y = player_one_istance.x - self.x, player_one_istance.y - self.y
self.angle = (180 / math.pi) * -math.atan2(temp_y, temp_x) - 90
self.image = pygame.transform.rotate(self.new_shooting_enemy_image, int(self.angle))
orig_center = self.new_shooting_enemy_image.get_rect(topleft=(self.x, self.y)).center
self.rect = self.image.get_rect(center=orig_center)
def drawShootingEnemies(self):
screen.blit(self.image, (self.x, self.y))
这是我的 Bullet 类:
class BulletEnemyShooting:
# initialize the bullet for the enemy shooting
def __init__(self, shooting_enemy):
self.x = shooting_enemy.x
self.y = shooting_enemy.y
self.image = laser_bullet
self.original_image = laser_bullet
self.angle = shooting_enemy.angle
self.vel_x = math.cos(self.angle) * 45
self.vel_y = math.sin(self.angle) * 45
self.rect = self.image.get_rect()
# draw the bullet
def MoveBulletEnemyShooting(self):
self.x += self.vel_x
self.y += self.vel_y
if self.x < -laser_bullet.get_width() or self.x >= display_width + laser_bullet.get_width() \
or self.y < -laser_bullet.get_height() or self.y >= display_height + laser_bullet.get_height():
enemy_shooting_bullets_list.pop(enemy_shooting_bullets_list.index(self))
def drawBulletEnemyShooting(self):
screen.blit(self.image, (self.x, self.y))
# rotate the bullet to the new angle
def rotate(self):
self.image = pygame.transform.rotate(self.original_image,
((180 / math.pi) * -math.atan2(self.vel_y, self.vel_x) - 90))
self.rect = self.image.get_rect()
如果你想测试代码,我已经删除了不必要的东西:
https://pastebin.com/HT93hUzt
您可以在此处下载替换图像来测试代码(请记住更改图像加载字符串!):
https://www.flaticon.com/free-icon/spaceship_1702089?term=spaceship&page=1&position=12
https://www.flaticon.com/free-icon/spaceship_1114531
最佳答案
我自己找到了答案,对于有兴趣的人,我将代码放在这里:
class BulletEnemyShooting:
def __init__(self, temp_angle, pos, target):
self.image = enemy_laser_bullet_image
self.original_image = enemy_laser_bullet_image
# The `pos` parameter is the center of the bullet.rect.
self.rect = self.image.get_rect(center=pos)
self.position = Vector2(pos) # The position of the bullet.
# This vector points from the shooting_enemy position to target position
direction = target - pos
# The polar coordinates of the direction vector.
radius, angle = direction.as_polar()
# Rotate the image by the negative angle (because the y-axis is flipped).
self.image = pygame.transform.rotozoom(self.image, -angle, 1)
# The velocity is the normalized direction vector scaled to the desired length.
self.velocity = direction.normalize() * 11
def update(self):
#move the bullet
self.position += self.velocity # Update the position vector.
self.rect.center = self.position # And the rect.
def drawBulletEnemyShooting(self):
screen.blit(self.image, self.rect)
#check if the enemy shooting bullet collides with player and in that case decrement the life
def collisionBulletShootingEnemy(self, shooting_enemy, shooting_enemy_image):
global score
shooting_enemy_rect = pygame.Rect(shooting_enemy.x, shooting_enemy.y, shooting_enemy_image.get_width(), shooting_enemy_image.get_height() )
bullet_rect = pygame.Rect(self.x, self.y, laser_bullet.get_width(), laser_bullet.get_height() )
# check collision
if shooting_enemy_rect.colliderect(bullet_rect):
bullets_list.pop(bullets_list.index(self))
shooting_enemy.x = 5000
score += 1
这必须在游戏的主循环中完成,因此每次都必须重新计算目标:
if enemy_shooting_bool:
for shooting_enemy in shooting_enemies_list:
target = Vector2(player_one_istance.x, player_one_istance.y)
bullet = BulletEnemyShooting(shooting_enemy.angle, (shooting_enemy.x, shooting_enemy.y), target)
enemy_bullets_list.append(bullet)
enemy_shooting_bool = False
我希望这可以帮助别人!
关于python - 让敌人射击玩家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610856/
好的,这就是我的错误:'Enemy' 未在此范围内声明。错误在 map.h 文件中,即使 map.h 包含 enemy.h,如图所示 #ifndef MAP_H_INCLUDED #define MA
这个问题已经有答案了: How to make enemy follow player in pygame? (2 个回答) Enemy doesn't follow player (pygame)
我试图让我的 Canvas 游戏中的 Sprite 不断向玩家移动,直到它发生碰撞。执行此操作的相关函数是 update() 函数: Enemy.prototype.update = function
我在无休止的运行游戏中遇到了麻烦,主要是我创建的两种敌人的产卵,当我们调用函数“startDifficultyTimer”时,有时会产生两个敌人。 基本上,当我调用该函数时,游戏会在同一行生成 2 个
从性能角度来看,什么是最正确的 - 敌人和武器之间的交互算法(这里的子弹可能更正确)? 每个 Sprite 的每一个子弹都应该通过“collidesWith”方法检查碰撞情况,并在完整的敌人列表上进行
遇到了一个问题,即敌人会向玩家开火,但似乎总是开到高处或向玩家的一侧,即使玩家是静止的并且没有移动。我是不是在我的代码中做错了什么导致了这个疯狂的问题,或者它只是一个随机的烦人的错误? 为播放器使用相
我是新来的快速编程。我想使用类节点将敌人生成到随机位置。我试图搜索随机生成敌人的代码,但它似乎与我的代码无关。 这是我搜索随机生成的代码。 import SpriteKit class GameSce
我最近遇到了一些问题,只是在我正在制作的游戏中传递对对象/敌人的引用,我想知道我是否使用了错误的方法。 我的主要问题是处理敌人和物体,而其他敌人或玩家可能仍然与它们有联系。 例如,如果你有一只兔子和一
在我的 2d 游戏中,我有一个金币和铜币的概念如下: when normal enemies die ..they drop a random gold or copper coin. Now whe
我是新来的,所以我希望你能帮助我。 我正在学习制作简单的 cocos2d 游戏的教程 Ray Wenderlich's Tutorial 我在另一个游戏中实现了它,一个像涂鸦跳跃这样的跳跃游戏。 在上
我是一名优秀的程序员,十分优秀!