我让敌人随机移动并看着玩家,现在我想让敌人射击玩家,我不知道为什么,但射击完全随机,正如你在下面的gif中看到的(你按左ctrl射击)。
无论如何,这是玩家类:
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))
这是我的子弹课:
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()
如果你想测试代码,我已经删除了不必要的东西:< br > https://pastebin.com/HT93hUzt
您可以在此处下载替换图像来测试代码(记得更改图像加载字符串!):
https://www.flaticon.com/free-icon/spaceship_1702089?term=spaceship
https://www.flaticon.com/free-icon/spaceship_1114531
https://www.flaticon.com/search?word=laserbeam
我自己找到了答案,对于那些感兴趣的人,我把代码放在这里:
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
我希望这能对某人有所帮助!
当玩家或敌人被击中时,我希望他们的精灵变成红色。从本质上讲,我想要在精灵上显示透明的红色效果。皮游戏是否提供了一种方法来做到这一点?(我使用多个图像,因为角色是动画的,所以创建一个红色的精灵,只是闪烁而不是正常的子画面是行不通的)
没多久,绝影干脆在学校外面租了房子自己搬了出去。 超薄早在上上学期就在外面租了房子,本来超薄话不多,看起来又热爱学习,大家都以为他是个老实人,根本没想到他居然是寝室第一个谈恋爱的,更没想到他居然会租房子同居。上学期王江也出去租了房子,他有足够的理由:要搞音乐,搞乐队,还要搞平面设计,比如搞音乐的搞设计的标志是什么?当然是有一家属于自己的工作室。――所以租间房子作工作室是很让人信服的。 绝影也想出去
在我创建的游戏中,我只希望僵尸能够每分钟击中玩家2次,而不是拿走洞健康条,因为它会让玩家快速受伤。 这是检查玩家和僵尸碰撞的代码。我这样做是为了让玩家只受到10点伤害,但这样玩家就再也不会受到伤害了。我尝试使用if语句来检查玩家是否无敌,并且在if语句中有一个for循环,当int达到30000时,该循环会使玩家死亡,但僵尸仍然会以如此快的速度伤害玩家,以至于洞健康条被拿走。
我试图创建一个游戏,敌人将随机产卵,并向屏幕上的一个点移动。我的问题是程序等待它们产卵,然后它们开始移动它们。我希望它们在产卵时开始移动,并在每个产卵之间有一个短暂的Rest。 下面是让它们移动的两个主要方法: Spek是一个简单的类,它扩展了ImageView,setRandomRegion()在屏幕边界上选择一个随机的x,y坐标。为了简单起见,我在while循环中有一个计数器,但我希望最终将其
跟一些比较牛X的程序员交流,经常听到他们嘴里冒出一个不标准的英文单词,而loop、iterate、traversal和recursion如果不在其内,总觉得他还不够牛X。当让,真正牛X的绝对不会这么说的,他们只是说“循环、迭代、遍历、递归”,然后再问“这个你懂吗?”。哦,这就是真正牛X的程序员。不过,他也仅仅是牛X罢了,还不是大神。大神程序员是什么样儿呢?他是扫地僧,大隐隐于市。 先搞清楚这些名词
我正在创建一个应用程序,实现谷歌的turn为基础的多人api,但我是相对新的Android。我需要做的是,当用户开始与对手比赛时,他们需要等待第二个用户开始,以便在布局可以填充之前将某些数据传递给他们。基本上,我有什么是你从谷歌的回合为基础的多人游戏的例子。它开始一场比赛,并让开始比赛的人进行第一轮。有人对此有经验吗?我不知道如何让首发球员等到另一个球员轮到他们。 我让开始的球员传递他们的信息,他