[英]How do I get my character to move while also getting bullets to spawn above them?
I've been trying to make a bullet hell (dankamu) game in Python, and when I tried to animate the movements of the character with the bullets, I encountered some issues.我一直在尝试在 Python 中制作子弹地狱(dankamu)游戏,当我尝试用子弹动画角色的动作时,我遇到了一些问题。 Depending on where I put my
redrawGameWindow()
function, either the bullets fail to spawn or my game character cannot move at all.根据我将
redrawGameWindow()
function 放在哪里,子弹无法生成或我的游戏角色根本无法移动。 I think it might be because of the fact that my movement function is separate from the for loop I used for the bullets?我认为这可能是因为我的运动 function 与我用于子弹的 for 循环是分开的? If that's the case, I don't know how to amalgamate them together in order to run both simultaneously.
如果是这种情况,我不知道如何将它们合并在一起以便同时运行。
class mc(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.hitbox = (self.x, self.y, 30, 30)
def draw(self, win):
win.blit(char, (self.x, self.y))
self.hitbox = (self.x, self.y, 30, 30)
pygame.draw.rect(win, (255,0,0), self.hitbox,2)
def hit(self):
print("Hit")
def redrawGameWindow(mainchar, bullet, bullets):
win.blit(bg, (0,0))
mainchar.draw(win)
pygame.display.update()
for bullet in bullets:
bullet.draw(win)
class projectile(object):
def __init__(self,x,y,radius,color,facing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing = facing
self.vel = 8 * facing
def draw(self,win):
pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)
def dankamu(enemy):
mainchar = mc(200, 410, 64, 64)
run = True
bullets = []
while run:
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and mainchar.x > 0:
mainchar.x -= mainchar.vel
if keys[pygame.K_RIGHT] and mainchar.x < 470:
mainchar.x += mainchar.vel
if keys[pygame.K_DOWN] and mainchar.y < 470:
mainchar.y += mainchar.vel
if keys[pygame.K_UP] and mainchar.y > 0:
mainchar.y -= mainchar.vel
if keys[pygame.K_LSHIFT] and mainchar.vel > 0:
mainchar.vel = 3
if keys[pygame.K_RSHIFT]:
mainchar.vel = 5
win.fill((255, 255, 255))
if enemy == "skeleton":
for i in range (30):
bullets.append(projectile(250, 250, 6, (0, 0, 0), 1))
for bullet in bullets:
if bullet.y - bullet.radius < mainchar.hitbox[1] + mainchar.hitbox[3] and bullet.y + bullet.radius > mainchar.hitbox[1]:
if bullet.x + bullet.radius > mainchar.hitbox[0] and bullet.x - bullet.radius < mainchar.hitbox[0] + mainchar.hitbox[2]:
mainchar.hit()
bullets.pop(bullets.index(bullet))
elif bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
redrawGameWindow(mainchar, bullet, bullets)
run = False
pygame.quit()
#Actual Running
dankamu("skeleton")
print("Dankamu success")
When you spawn a bullet, you must spawn the bullet above the palyer's location:当你生成子弹时,你必须在玩家的位置上方生成子弹:
bullets.append(projectile(250, 250, 6, (0, 0, 0), 1))
bullets.append(projectile(mainchar.x, 0, 6, (0, 0, 0), 1))
Since the bullet has to move from the bottom to the top, you have to increment the y coordinate of the bullet:由于子弹必须从底部移动到顶部,因此您必须增加子弹的 y 坐标:
bullet.x += bullet.vel
bullet.y += bullet.vel
Draw the bullets before you update the display:在更新显示之前绘制项目符号:
def redrawGameWindow(mainchar, bullet, bullets):
win.blit(bg, (0,0))
mainchar.draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
Do not add all the bullets at once:不要一次添加所有项目符号:
if bullet_frame_count == 0:
bullet_frame_count = 10
if len(bullets) < 30:
bullets.append(projectile(mainchar.x, 0, 6, (0, 0, 0), 1))
else:
bullet_frame_count -= 1
Use pygame.Rect.colliderect
for the collision test:使用
pygame.Rect.colliderect
进行碰撞测试:
bullet_rect = pygame.Rect(bullet.x-bullet.radius, bullet.y-bullet.radius, bullet.radius*2,bullet.radius*2)
if bullet_rect.colliderect(mainchar.hitbox):
mainchar.hit()
bullets.pop(bullets.index(bullet))
Please read How to remove items from a list while iterating?请阅读如何在迭代时从列表中删除项目?
Complete dankamu
function:完整
dankamu
function:
def dankamu(enemy):
mainchar = mc(200, 410, 64, 64)
run = True
bullets = []
count = 0
while run:
pygame.time.delay(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
mainchar.x = mainchar.x + (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * mainchar.vel
mainchar.y = mainchar.y + (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * mainchar.vel
mainchar.x = max(0, min(470, mainchar.x))
mainchar.y = max(0, min(470, mainchar.y))
if keys[pygame.K_LSHIFT] and mainchar.vel > 0:
mainchar.vel = 3
if keys[pygame.K_RSHIFT]:
mainchar.vel = 5
if enemy == "skeleton":
if count == 0:
count = 10
if len(bullets) < 30:
bullets.append(projectile(mainchar.x, 0, 6, (0, 0, 0), 1))
else:
count -= 1
for bullet in bullets[:]:
bullet_rect = pygame.Rect(bullet.x-bullet.radius, bullet.y-bullet.radius, bullet.radius*2,bullet.radius*2)
if bullet_rect.colliderect(mainchar.hitbox):
mainchar.hit()
bullets.pop(bullets.index(bullet))
elif bullet.y < 500:
bullet.y += bullet.vel
else:
bullets.pop(bullets.index(bullet))
redrawGameWindow(mainchar, bullet, bullets)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.