我的代码有问题,我分配了空间来创建子弹然后发射它并将其更新到屏幕上。有什么问题?
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 手动更新它。