Код дает мне несколько синтаксических ошибок. И я даже не уверен, действительно ли это работает в первую очередь. Моя цель - попытаться сделать так, чтобы персонаж игрока стрелял горизонтально. Если бы кто-нибудь мог помочь мне осуществить стрельбу в классе снарядов или в основном игровом цикле, я был бы очень признателен
import pygame
pygame.init()
win = pygame.display.set_mode((600,600))#initialises the game window
pygame.display.set_caption("Hello world")
bg = pygame.image.load('bg.jpg')
k = pygame.key.get_pressed()
thunderball = pygame.image.load('thunderball.png')
Класс снаряда, где объект инициализирован и нарисован
class projectile():
def __init__(self,x,y,facing):
self.x = x
self.y = y
self.facing = facing
self.vel = vel*facing
def draw(self,win):
win.blit(thunderball,(self.x,self.y))
Playerкласс для главного героя
class player:
def __init__(self,x,y):
self.x = x
self.y = y
self.width = 64
self.height = 64
self.standing = True
self.left = False
self.right = True
self.vel = 15
self.jumping = False
self.jumpCount = 10
self.attack = False
def move(self,x,y):
if not(self.standing):
if k[pygame.K_LEFT] and self.x > 0 - 150:
self.left = True
self.right = False
self.x -= self.vel
elif k[pygame.K_RIGHT] and self.x < 500 - 150 :
self.right = True
self.left = False
self.x += self.vel
else:
self.standing = True
if k[pygame.K_b]:
if len(self.bullets) < 5:
if self.left:
projectile.facing = -1
else:
projectile.facing = 1
self.bullets.append(projectile(self.x + 20,self.y
+64,projectile.facing))
def jump(self,y):
if not(self.jumping): #checks if user's jumping intiating jump
if k[pygame.K_SPACE]:
self.jumping = True
else:
if self.jumpCount >= -10:
neg = 1
if self.jumpCount < 0:
neg = -1
self.y -= (self.jumpCount ** 2) * 0.5 * neg
self.jumpCount -= 1
else:
self.jumping = False
self.jumpCount = 10
def draw(self,win):
wLeft = pygame.image.load('runningleft.png')
wRight = pygame.image.load('running.png')
char = [pygame.image.load('idleright.png'),pygame.image.load('idleleft.png')]
if not(self.standing):
if self.left:
win.blit(wLeft,(self.x,self.y))
elif self.right:
win.blit(wRight,(self.x,self.y))
else:
if self.right:
win.blit(char[0],(self.x,self.y))
if self.left:
win.blit(char[1],(self.x,self.y))
Основной цикл игры, в котором я пишу код для стрельбы снарядами. Я пытался добавить функцию стрельбы в класс снарядов, но столкнулся с некоторыми проблемами на этом пути
bullets = []
run = True
wizard = (25,420)
while run:#main game loop
pygame.time.delay(15)
for event in pygame.event.get():#loops through a list of keyboard or mouse events
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
wizard.jumping = True
for bullet in bullets:
if bullet.x > 0 and bullet.x:
bullet.x += bullet.vel
else:
bullets.remove(bullet)
if k[pygame.K_b]:
if len(bullets) < 5:
if left:
facing = -1
else:
facing = 1
bullets.append(ball)
wizard.move(wizard.x,wizard.y)
win.blit(bg,(0,0))
wizard.jump(wizard.y)
wizard.draw(win)
for bullet in bullets:
projectile.draw(win)
pygame.display.update()
pygame.quit()