简体   繁体   English

为什么添加 def 时语法无效?

[英]Why am i getting invalid syntax when adding a def?

import pygame
import math

#initialise pygame
pygame.init()

#game window
WINDOW_WIDTH = 1200
WINDOW_HEIGHT = 750

#create game window
win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Castle Defender')

clock = pygame.time.Clock()
FPS = 60


#load images
background_img = pygame.image.load('Animations/BG.png').convert_alpha()
background = pygame.transform.scale(background_img,(1200,750))

castle_img = pygame.image.load('Animations/Castle.png').convert_alpha()

bullet_img = pygame.image.load('Animations/SmallBullet.png').convert_alpha()
b_w = bullet_img.get_width()
b_h = bullet_img.get_height()
bullet_img = pygame.transform.scale(bullet_img, (int(b_w * 0.075), int(b_h * 0.075)))

WHITE =(255,255,255)



            
#Castle class
class Castle():
    def __init__(self, image, x, y, scale):
        self.health = 1000
        self.max_health = self.health

        width = image.get_width()
        height = image.get_height()

        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def shoot(self):
        pos = pygame.mouse.get_pos()
        x_dist = pos[0] - self.rect.midleft[0]
        y_dist = -(pos[1] - self.rect.midleft[1])
        self.angle = math.degrees(math.atan2(y_dist, x_dist))
        #mouseclick
        if pygame.mouse.get_pressed()[0] and self.fired == False:
            self.fired = True
            bullet = Bullet(bullet_img, self.rect.right[0], self.rect.right[1], self.angle)
            bullet_group.add(bullet)
        #reset mouseclick
        if pygame.mouse.get_pressed()[0] == False:
            self.fired = False
                         
#Method
    def draw(self):
        self.image = self.image

        win.blit(self.image, self.rect)

class Bullet(pygame.sprite.Sprite):
    def __init__(self, image, x, y, angle):
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.angle = math.radians(angle)
        self.speed = 10
        self.dx = math.cos(self.angle) * self.speed
        self.dy = -(math.sin(self.angle) * self.speed)

    def update(self):
        #check if bullet has gone off the screen
        if self.rect.right < 0 or self.rect.left > SCREEN_WIDTH or self.rect.bottom < 0 or self.rect.top > SCREEN_HEIGHT:
            self.kill()         

        #move bullet
        self.rect.x += self.dx
        self.rect.y += self.dy


castle = Castle(castle_img, WINDOW_WIDTH - 1350, WINDOW_HEIGHT - 400, 0.7)


bullet_group = pygame.sprite.Group()


run = True
while run:

    clock.tick(FPS)

    win.blit(background, (0, 0))
    
    castle.draw()
    castle.shoot()

    #bullet drawing
    bullet_group.update()
    bullet_group.draw(win)
    print(len(bullet_group))

    
    #event handler
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    #update display window
    pygame.display.update()

pygame.quit()

I am trying to see the line from my base and my mouse.我试图从我的基地和鼠标中看到这条线。 However I get an error of the following但是我收到以下错误

bullet = Bullet(bullet_img, self.rect.right[0], self.rect.right[1], self.angle)

(My end goal is for bullets to come out of the castle and i am relatively new to coding and this will be my first project). (我的最终目标是让子弹从城堡里出来,我对编码比较陌生,这将是我的第一个项目)。 Also i keep getting an error when trying to submit this saying it is mostly code so the parenthisis was me rambling on另外,我在尝试提交此内容时不断收到错误消息,说它主要是代码,所以括号是我漫无边际的

The issue the code is trying to use self.rect.right as if it was a python list.代码尝试使用self.rect.right的问题,就好像它是 python 列表一样。 But self.rect.right is an integer.但是self.rect.right是 integer。

self.rect.right[0]
self.rect.right[1]

Probably this should be self.rect.right and self.rect.top (since it deals with the y -dimension).可能这应该是self.rect.rightself.rect.top (因为它处理y维度)。

Then you also need to correct references to SCREEN_WIDTH and SCREEN_HEIGHT .然后,您还需要更正对SCREEN_WIDTHSCREEN_HEIGHT的引用。 It looks like these should be WINDOW_ etc.看起来这些应该是WINDOW_等。

After these changes are made, your program does not exit with an error when the mouse is clicked.进行这些更改后,单击鼠标时您的程序不会因错误而退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM