Python 在Pygame中射击多发子弹
我正在使用pygame制作一个简单的空间入侵者变体。以下是一些工作代码:Python 在Pygame中射击多发子弹,python,pygame,Python,Pygame,我正在使用pygame制作一个简单的空间入侵者变体。以下是一些工作代码: import pygame, random, time, tkinter pygame.init() def main(): X = 672 Y = 500 win = pygame.display.set_mode((X,Y)) pygame.display.set_caption('Space Invaders') class player(object): def __init__(
import pygame, random, time, tkinter
pygame.init()
def main():
X = 672
Y = 500
win = pygame.display.set_mode((X,Y))
pygame.display.set_caption('Space Invaders')
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.lives = 3
self.vel = 5
self.points = 0
self.explosionYN = False
self.explosionCount = 0
self.highScore = int(text)
def explosion(self, win):
if self.explosionYN:
win.blit(explosion[self.explosionCount], (self.x-20, self.y-30, 100, 100))
self.explosionCount += 1
if not self.explosionYN:
self.explosionCount = 0
if self.explosionCount + 1 > 6:
self.explosionYN = False
self.explosionCount = 0
def move(self, win):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.x > -1:
self.x -= self.vel
if keys[pygame.K_RIGHT] and self.x < 623:
self.x += self.vel
if keys[pygame.K_q]:
if self.x >= 623:
self.x = 0
if self.x < 0:
self.x = 623
class invader(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 1
def reset(self, win):
self.y = 0
self.x = random.randint(5, (X-35))
class bullet(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 0
def shoot(self, win):
laser.play(0)
self.x = ship.x + 22
self.y = ship.y
self.vel = 15
def reset(self, win):
self.vel = 0
self.y = 510
ship = player(X//2 - (0.5*52), 435, 52, 52)
alien = invader(random.randint(0,707),0,31,25)
bullet = bullet(750, 510, 5, 7)
while run:
pygame.time.delay(25)
alien.y += alien.vel
bullet.y -= bullet.vel
if alien.y > 500:
ship.explosionYN = True
ship.explosion(win)
loseLife.play(0)
if ship.explosionCount+ 1 >= 6:
win.fill((0,0,0))
pause()
ship.lives -= 1
alien.reset(win)
elif ship.lives <= 1:
test()
if bullet.y <= 0:
bullet.reset(win)
alien1 = pygame.draw.rect(win, (0,0,0), (alien.x,alien.y,31,25))
bullet1 = pygame.draw.rect(win, (0,100,255), (bullet.x, bullet.y, bullet.width, bullet.height))
if pygame.Rect.colliderect(alien1, bullet1):
ship.points += 1
if ship.highScore < ship.points:
ship.highScore += 1
bullet.reset(win)
kill.play(0)
alien.y = 0
alien.reset(win)
alien.vel += 0.5
ship.vel += 0.25
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keys = pygame.key.get_pressed()
ship.move(win)
if keys[pygame.K_SPACE] or keys[pygame.K_UP]:
bullet.shoot(win)
if keys[pygame.K_p]:
pause()
drawGame()
main()
但现在子弹一点也没露出来。
实际上什么也没发生。
我看过其他一些帖子,但由于我对pygame还不熟悉,我对它们一无所知
在pygame上显示多个子弹的简单有效方法是什么?
谢谢。完成此操作的典型方法是创建项目符号列表
bullets = []
然后当你发射一颗子弹时,你就把它添加到列表中
bullets.append(Bullet(750, 510, 5, 7))
在while循环中,您将使用for循环迭代每个项目符号,依次更新和重画每个项目符号
for bullet in bullets:
bullet.y -= bullet.vel # Move Bullet
# Draw bullet
# Check for collision with player/enemy
这显然不是一个完整的代码清单,但希望它足以让您开始
如果你想创建一个太空入侵者克隆,你还必须创建一个敌人列表
你可以从RasBuriPi基金会上找到最近发布的“代码经典”,它有助于解释如何使用PyPoGAME创建一些经典游戏。这是一个免费下载(或者你可以购买硬拷贝)
编辑:考虑Python样式指南PEP-8,并将您的类改名为标题案例。比如说
class bullet():
应该是
class Bullet():
这将有助于消除变量
bullet
和同名类之间的任何混淆。我尝试过这个方法,但当我按空格键时,它会给我一条错误消息:主项目符号中的第218行。追加(项目符号(750、510、5、7))TypeError:“bullet”对象不可调用。我建议遵循PEP-8指南,将bullet
类重命名为bullet
。这样就不会混淆变量bullet
和类bullet
。我会更新我的答案。
class Bullet():