pygame 尝试开火时子弹没有出现
Bullets not showing up when trying to fire in pygame
我的代码有问题,我分配了 space 创建子弹然后发射它并更新到屏幕上。有什么问题?
import pygame, random, os, sys
pygame.init()
pygame.mixer.pre_init(441100, -16, 2, 2048)
pygame.mixer.init()
width = 640
height = 480
black = ( 0, 0, 0)
white = (255,255,255)
playerimg = pygame.image.load("images/shipup.png")
class player: # Class for player ship
def __init__(self, x, y, xc, yc, w, h, angle, img, lives, direc):
self.x = x
self.y = y
self.xc = xc
self.yc = yc
self.w = w
self.h = h
self.img = img
self.lives = lives
self.angle = angle
self.direc = direc
def update(self):
gameDisplay.blit(self.img, (self.x, self.y))
self.x += self.xc
self.y += self.yc
playership = player(10, 10, 0, 0, 30, 50, 0, playerimg, 3, "up")
bulletxc = 0
bulletyc = 0
class bullet: # Class for shooting bullets
def __init__(self, x, y, xc, yc, w, h, img):
self.x = x
self.y = y
self.xc = xc
self.yc = yc
self.w = w
self.h = h
self.img = img
def update(self):
gameDisplay.blit(self.img, (self.x, self.y))
self.x += self.xc
self.y += self.yc
if playership.direc == "right": # Determines what direction to shoot
bulletxc = 6
bulletyc = 0
if playership.direc == "left":
bulletxc = -6
bulletyc = 0
if playership.direc == "up":
bulletyc = -6
bulletxc = 0
if playership.direc == "down":
bulletyc = 6
bulletxc = 0
bulletlist = ()
gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Asteroids", "Ast...")
clock = pygame.time.Clock()
def mainloop():
global bulletlist
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Basic movements V
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
playership.yc = -3
playership.img = pygame.image.load("images/shipup.png")
if event.key == pygame.K_s:
playership.yc = 3
playership.img = pygame.image.load("images/shipdown.png")
if event.key == pygame.K_d:
playership.xc = 3
playership.img = pygame.image.load("images/shipright.png")
if event.key == pygame.K_a:
playership.xc = -3
playership.img = pygame.image.load("images/shipleft.png")
if event.key == pygame.K_SPACE: # Here is where the bullet fires.
for i in bulletlist:
bulletlist.append(bullet)
bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w:
playership.yc = 0
if event.key == pygame.K_s:
playership.yc = 0
if event.key == pygame.K_d:
playership.xc = 0
if event.key == pygame.K_a:
playership.xc = 0
gameDisplay.fill(black)
playership.update()
for i in bulletlist:
i.update()
if playership.x < 0: # Collisions against the sides of the screen.
playership.x = -1
if playership.y < 0:
playership.y = -1
if playership.x + playership.h > width:
playership.x = width - playership.h
if playership.y + playership.h > height:
playership.y = height - playership.h
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
mainloop()
pygame.quit()
quit()
问题:项目符号未显示,但没有错误。
已尝试:在 K_SPACE if 语句中使用 gameDisplay.blit 手动更新它。
问题出在这段代码中:
if event.key == pygame.K_SPACE: # Here is where the bullet fires.
for i in bulletlist:
bulletlist.append(bullet)
bulletlist = bullet(playership.x + 25, playership.y + 25,
bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))
问题是您之前定义了 bulletlist
:
bulletlist = ()
作为一个空元组。但是,在上面的代码中,for
循环不会做任何事情,因为 bulletlist
的长度是 0
,所以 i
永远不会有值。此代码在 Python 中不会出错,但它也不会执行任何操作。
但是,即使它有长度,您也将 bulletlist
标识符重新分配给 bullet
在 for
循环中的一个实例 ,这是个坏主意。我建议你考虑一下你要在这里完成什么,也许想出另一种方法。
if event.key == pygame.K_SPACE: # Here is where the bullet fires.
for i in bulletlist:
bulletlist.append(bullet)
bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))
在这里;
bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))
这部分是错误的,你的 bulletlist
不再是元组了。看到了,你的 bullet
class 存储了它,你应该修改那个语句。
另外,我认为你不能用这种方法生火,查看更多关于Pygame的信息。 Here 教程非常棒。
import pygame, random, os, sys
pygame.init()
pygame.mixer.pre_init(441100, -16, 2, 2048)
pygame.mixer.init()
width = 640
height = 480
black = ( 0, 0, 0)
white = (255,255,255)
playerimg = pygame.image.load("images/shipup.png")
class player: # Class for player ship
def __init__(self, x, y, xc, yc, w, h, angle, img, lives, direc):
self.x = x
self.y = y
self.xc = xc
self.yc = yc
self.w = w
self.h = h
self.img = img
self.lives = lives
self.angle = angle
self.direc = direc
def update(self):
gameDisplay.blit(self.img, (self.x, self.y))
self.x += self.xc
self.y += self.yc
playership = player(10, 10, 0, 0, 30, 50, 0, playerimg, 3, "up")
bulletxc = 0
bulletyc = 0
class bullet: # Class for shooting bullets
def __init__(self, x, y, xc, yc, w, h, img):
self.x = x
self.y = y
self.xc = xc
self.yc = yc
self.w = w
self.h = h
self.img = img
def update(self):
gameDisplay.blit(self.img, (self.x, self.y))
self.x += self.xc
self.y += self.yc
if playership.direc == "right": # Determines what direction to shoot
bulletxc = 6
bulletyc = 0
if playership.direc == "left":
bulletxc = -6
bulletyc = 0
if playership.direc == "up":
bulletyc = -6
bulletxc = 0
if playership.direc == "down":
bulletyc = 6
bulletxc = 0
bulletlist = bullet(-33, -33, bulletxc, bulletyc, 5, 5, pygame.image.load("images/bullet.png"))
gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Asteroids", "Ast...")
clock = pygame.time.Clock()
def mainloop():
global bulletlist
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Basic movements V
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
playership.yc = -3
playership.img = pygame.image.load("images/shipup.png")
bulletyc = -6
bulletxc = 0
if event.key == pygame.K_s:
playership.yc = 3
playership.img = pygame.image.load("images/shipdown.png")
bulletyc = 6
bulletxc = 0
if event.key == pygame.K_d:
playership.xc = 3
playership.img = pygame.image.load("images/shipright.png")
bulletxc = 6
bulletyc = 0
if event.key == pygame.K_a:
playership.xc = -3
playership.img = pygame.image.load("images/shipleft.png")
bulletxc = -6
bulletyc = 0
if event.key == pygame.K_SPACE: # Here is where the bullet fires.
bulletlist = bullet(playership.x + 15, playership.y + 15, bulletxc, bulletyc, 5, 5, pygame.image.load("images/bullet.png"))
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w:
playership.yc = 0
if event.key == pygame.K_s:
playership.yc = 0
if event.key == pygame.K_d:
playership.xc = 0
if event.key == pygame.K_a:
playership.xc = 0
gameDisplay.fill(black)
playership.update()
bulletlist.update()
if playership.x < 0: # Collisions against the sides of the screen.
playership.x = -1
if playership.y < 0:
playership.y = -1
if playership.x + playership.h > width:
playership.x = width - playership.h
if playership.y + playership.h > height:
playership.y = height - playership.h
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
mainloop()
pygame.quit()
quit()
摆脱了元组,它工作正常。
我的代码有问题,我分配了 space 创建子弹然后发射它并更新到屏幕上。有什么问题?
import pygame, random, os, sys
pygame.init()
pygame.mixer.pre_init(441100, -16, 2, 2048)
pygame.mixer.init()
width = 640
height = 480
black = ( 0, 0, 0)
white = (255,255,255)
playerimg = pygame.image.load("images/shipup.png")
class player: # Class for player ship
def __init__(self, x, y, xc, yc, w, h, angle, img, lives, direc):
self.x = x
self.y = y
self.xc = xc
self.yc = yc
self.w = w
self.h = h
self.img = img
self.lives = lives
self.angle = angle
self.direc = direc
def update(self):
gameDisplay.blit(self.img, (self.x, self.y))
self.x += self.xc
self.y += self.yc
playership = player(10, 10, 0, 0, 30, 50, 0, playerimg, 3, "up")
bulletxc = 0
bulletyc = 0
class bullet: # Class for shooting bullets
def __init__(self, x, y, xc, yc, w, h, img):
self.x = x
self.y = y
self.xc = xc
self.yc = yc
self.w = w
self.h = h
self.img = img
def update(self):
gameDisplay.blit(self.img, (self.x, self.y))
self.x += self.xc
self.y += self.yc
if playership.direc == "right": # Determines what direction to shoot
bulletxc = 6
bulletyc = 0
if playership.direc == "left":
bulletxc = -6
bulletyc = 0
if playership.direc == "up":
bulletyc = -6
bulletxc = 0
if playership.direc == "down":
bulletyc = 6
bulletxc = 0
bulletlist = ()
gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Asteroids", "Ast...")
clock = pygame.time.Clock()
def mainloop():
global bulletlist
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Basic movements V
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
playership.yc = -3
playership.img = pygame.image.load("images/shipup.png")
if event.key == pygame.K_s:
playership.yc = 3
playership.img = pygame.image.load("images/shipdown.png")
if event.key == pygame.K_d:
playership.xc = 3
playership.img = pygame.image.load("images/shipright.png")
if event.key == pygame.K_a:
playership.xc = -3
playership.img = pygame.image.load("images/shipleft.png")
if event.key == pygame.K_SPACE: # Here is where the bullet fires.
for i in bulletlist:
bulletlist.append(bullet)
bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w:
playership.yc = 0
if event.key == pygame.K_s:
playership.yc = 0
if event.key == pygame.K_d:
playership.xc = 0
if event.key == pygame.K_a:
playership.xc = 0
gameDisplay.fill(black)
playership.update()
for i in bulletlist:
i.update()
if playership.x < 0: # Collisions against the sides of the screen.
playership.x = -1
if playership.y < 0:
playership.y = -1
if playership.x + playership.h > width:
playership.x = width - playership.h
if playership.y + playership.h > height:
playership.y = height - playership.h
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
mainloop()
pygame.quit()
quit()
问题:项目符号未显示,但没有错误。
已尝试:在 K_SPACE if 语句中使用 gameDisplay.blit 手动更新它。
问题出在这段代码中:
if event.key == pygame.K_SPACE: # Here is where the bullet fires.
for i in bulletlist:
bulletlist.append(bullet)
bulletlist = bullet(playership.x + 25, playership.y + 25,
bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))
问题是您之前定义了 bulletlist
:
bulletlist = ()
作为一个空元组。但是,在上面的代码中,for
循环不会做任何事情,因为 bulletlist
的长度是 0
,所以 i
永远不会有值。此代码在 Python 中不会出错,但它也不会执行任何操作。
但是,即使它有长度,您也将 bulletlist
标识符重新分配给 bullet
在 for
循环中的一个实例 ,这是个坏主意。我建议你考虑一下你要在这里完成什么,也许想出另一种方法。
if event.key == pygame.K_SPACE: # Here is where the bullet fires.
for i in bulletlist:
bulletlist.append(bullet)
bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))
在这里;
bulletlist = bullet(playership.x + 25, playership.y + 25, bulletxc, bulletyc, 5, 10, pygame.image.load("images/bullet.png"))
这部分是错误的,你的 bulletlist
不再是元组了。看到了,你的 bullet
class 存储了它,你应该修改那个语句。
另外,我认为你不能用这种方法生火,查看更多关于Pygame的信息。 Here 教程非常棒。
import pygame, random, os, sys
pygame.init()
pygame.mixer.pre_init(441100, -16, 2, 2048)
pygame.mixer.init()
width = 640
height = 480
black = ( 0, 0, 0)
white = (255,255,255)
playerimg = pygame.image.load("images/shipup.png")
class player: # Class for player ship
def __init__(self, x, y, xc, yc, w, h, angle, img, lives, direc):
self.x = x
self.y = y
self.xc = xc
self.yc = yc
self.w = w
self.h = h
self.img = img
self.lives = lives
self.angle = angle
self.direc = direc
def update(self):
gameDisplay.blit(self.img, (self.x, self.y))
self.x += self.xc
self.y += self.yc
playership = player(10, 10, 0, 0, 30, 50, 0, playerimg, 3, "up")
bulletxc = 0
bulletyc = 0
class bullet: # Class for shooting bullets
def __init__(self, x, y, xc, yc, w, h, img):
self.x = x
self.y = y
self.xc = xc
self.yc = yc
self.w = w
self.h = h
self.img = img
def update(self):
gameDisplay.blit(self.img, (self.x, self.y))
self.x += self.xc
self.y += self.yc
if playership.direc == "right": # Determines what direction to shoot
bulletxc = 6
bulletyc = 0
if playership.direc == "left":
bulletxc = -6
bulletyc = 0
if playership.direc == "up":
bulletyc = -6
bulletxc = 0
if playership.direc == "down":
bulletyc = 6
bulletxc = 0
bulletlist = bullet(-33, -33, bulletxc, bulletyc, 5, 5, pygame.image.load("images/bullet.png"))
gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Asteroids", "Ast...")
clock = pygame.time.Clock()
def mainloop():
global bulletlist
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Basic movements V
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
playership.yc = -3
playership.img = pygame.image.load("images/shipup.png")
bulletyc = -6
bulletxc = 0
if event.key == pygame.K_s:
playership.yc = 3
playership.img = pygame.image.load("images/shipdown.png")
bulletyc = 6
bulletxc = 0
if event.key == pygame.K_d:
playership.xc = 3
playership.img = pygame.image.load("images/shipright.png")
bulletxc = 6
bulletyc = 0
if event.key == pygame.K_a:
playership.xc = -3
playership.img = pygame.image.load("images/shipleft.png")
bulletxc = -6
bulletyc = 0
if event.key == pygame.K_SPACE: # Here is where the bullet fires.
bulletlist = bullet(playership.x + 15, playership.y + 15, bulletxc, bulletyc, 5, 5, pygame.image.load("images/bullet.png"))
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w:
playership.yc = 0
if event.key == pygame.K_s:
playership.yc = 0
if event.key == pygame.K_d:
playership.xc = 0
if event.key == pygame.K_a:
playership.xc = 0
gameDisplay.fill(black)
playership.update()
bulletlist.update()
if playership.x < 0: # Collisions against the sides of the screen.
playership.x = -1
if playership.y < 0:
playership.y = -1
if playership.x + playership.h > width:
playership.x = width - playership.h
if playership.y + playership.h > height:
playership.y = height - playership.h
pygame.display.update()
clock.tick(60)
if __name__ == "__main__":
mainloop()
pygame.quit()
quit()
摆脱了元组,它工作正常。