gpt4 book ai didi

python - 无法让我的大炮在pygame中发射子弹

转载 作者:行者123 更新时间:2023-12-04 09:42:09 25 4
gpt4 key购买 nike

在我的游戏中,我可以将大炮放置在屏幕上的任何位置。我希望我的大炮能够发射子弹,子弹行进 100 像素后应该重置。下面给出的是我的大炮类(class),我对 OOP 还是新手,所以我需要一些帮助,但是我也无法使用列表来完成这项任务。感谢您的帮助。

class Cannon():
global cannon_list
global bullet_list
def __init__(self, x, y, track, old_x):
self.x = x
self.y = y
self.track = track
self.old_x = old_x

def spawnBullet(self):
for j in bullet_list:
self.old_x = self.x
self.track = j[2]
screen.blit(bullet, (j[0], j[1]))

def moveBullet(self):
if self.x <= self.track:
self.x += 3

def resetBullet(self):
if self.x >= self.track:
self.x = self.old_x

def spawnCannon(self):
for i in cannon_list:
screen.blit(cannon, i)

使用大炮类。这是在redrawGamewindow下。
for j in bullet_list:
cannonsAndBullets = Cannon(j[0], j[1], j[2], j[0])
cannonsAndBullets.spawnCannon()
cannonsAndBullets.spawnBullet()
cannonsAndBullets.moveBullet()
cannonsAndBullets.resetBullet()

以下是我附加到 bullet_list 中的内容和 cannon_list . x an y 是我的球员的位置
            cannon_list.append([x,y])
bullet_list.append([x,(y+25),100, x])

在我的类里面编辑
class Cannon():
global cannon_list
global bullet_list
def __init__(self, x, y, track, old_x):
self.x = x
self.y = y
self.track = track
self.old_x = old_x

def spawnBullet(self):
# for j in bullet_list:
# self.x, self.y, self.track, self.old_x = j
screen.blit(bullet, (self.x, self.y))

def moveBullet(self):
# for j in bullet_list:
# self.x, self.y, self.track, self.old_x = j
if self.track <= 100:
print(self.track)
self.track += 3
self.x += 3

def resetBullet(self):
# for j in bullet_list:
# self.x, self.y, self.track, self.old_x = j
if self.track >= 100:
print(self.track)
self.x = self.old_x

def spawnCannon(self):
for i in cannon_list:
screen.blit(cannon, i)

最佳答案

让我们放弃一切,从头开始,利用 Sprite 和矢量数学等 pygame 功能。

我们从一个 pygame 游戏的基本骨架开始,一个简单的窗口:

import pygame

def main():
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
screen.fill(pygame.Color('grey'))
pygame.display.flip()
clock.tick(60)

if __name__ == '__main__':
main()

enter image description here

现在,我们要放置一些 Sprite。让我们创建一个 Sprite代表大炮的类,让我们用鼠标放置它们:
import pygame

class Cannon(pygame.sprite.Sprite):
def __init__(self, pos, *grps):
super().__init__(*grps)
self.image = pygame.Surface((32, 32))
self.image.fill(pygame.Color('darkred'))
self.rect = self.image.get_rect(center=pos)

def main():

all_sprites = pygame.sprite.Group()

screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
if e.type == pygame.MOUSEBUTTONDOWN:
Cannon(e.pos, all_sprites)


all_sprites.update()

screen.fill(pygame.Color('grey'))
all_sprites.draw(screen)
pygame.display.flip()

clock.tick(60)

if __name__ == '__main__':
main()

enter code here

现在我们想让大炮发射子弹。为此,我们使用了一些 OOP 特性,例如多态性。子弹和大炮是不同的类型,但它们提供相同的界面: updatedraw .就是这样。请注意我们的主循环如何不需要知道我们的 Sprite 到底是什么类型。

我们还确保项目符号的所有逻辑都在 Bullet 中。类,大炮的所有逻辑都在 Cannon类(class):
import pygame

class Bullet(pygame.sprite.Sprite):
def __init__(self, pos, *grps):
super().__init__(*grps)
self.image = pygame.Surface((4, 4))
self.image.fill(pygame.Color('black'))
self.rect = self.image.get_rect(center=pos)
self.pos = pygame.Vector2(pos)
self.travelled = pygame.Vector2(0, 0)
direction = pygame.Vector2(pygame.mouse.get_pos()) - self.pos
if direction.length() > 0:
self.direction = direction.normalize()
else:
self.kill()

def update(self, dt):
v = self.direction * dt / 5
self.pos += v
self.travelled += v
self.rect.center = self.pos
if self.travelled.length() > 100:
self.kill()

class Cannon(pygame.sprite.Sprite):
def __init__(self, pos, *grps):
super().__init__(*grps)
self.image = pygame.Surface((32, 32))
self.image.fill(pygame.Color('darkred'))
self.rect = self.image.get_rect(center=pos)
self.timer = 200

def update(self, dt):
self.timer = max(self.timer - dt, 0)
if self.timer == 0:
self.timer = 200
Bullet(self.rect.center, self.groups()[0])

def main():

all_sprites = pygame.sprite.Group()

screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
dt = 1
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
if e.type == pygame.MOUSEBUTTONDOWN:
Cannon(e.pos, all_sprites)


all_sprites.update(dt)

screen.fill(pygame.Color('grey'))
all_sprites.draw(screen)
pygame.display.flip()

dt = clock.tick(60)

if __name__ == '__main__':
main()

enter image description here

使用向量并简单地添加每帧经过的距离可以轻松检查每个 Bullet如果它已经行进了 100 像素。如果为真,只需调用 kill将删除它。

关于python - 无法让我的大炮在pygame中发射子弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62276788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com