Pygame - Crashing when trying to shoot [closed]

我们两清 提交于 2021-01-20 08:52:19

问题


I am trying to learn Python and wanted to practice with Pygame. I am currently using Python 3.6.5 and running the code in Atom 1.27.2 and installed the atom-runner package. I have been following the KidsCanCode tutorial for Pygame on Youtube.

I wanted to change the code around so I am not just copying the tutorial. At the moment everything works until I try to spawn a bullet from my ship. The bullet is supposed to go from the left to right, spawning from the ship on the left side of the screen. Once the program loads all ships are moving as expected with collisions working, however when I hit space bar the game crashes with the following error:

TypeError: add() argument after * must be an iterable, not int

I thought it has to do with my player.shoot function but if I remove the function and try to spawn the bullet under...

if event.key == pg.K_SPACE:

It still doesn't work.

I have the code similar to the code on the video but it still doesn't seem to work. Maybe it has to do with my .rect positions for the bullet, but at this point I am not really sure. Any help would be appreciated.

import pygame as pg
import random
import os

WIDTH = 1200
HEIGHT = 700
FPS = 60

#-----define colors-----
WHITE = (255,255,255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# set up assets folders
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, 'graphics')

pg.init()
pg.mixer.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption('First Game')
clock = pg.time.Clock()

# ------Load graphics-------------
player_img = pg.image.load(os.path.join(img_folder, 'blueship1.png')).convert()
enemy1_img = pg.image.load(os.path.join(img_folder, 'enemy1.png')).convert()


# ------------ Sprite for the player---------
class Player(pg.sprite.Sprite):
    def __init__(self):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.transform.scale(player_img, (80, 60))
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
        self.rect.midleft =  (0, HEIGHT / 2)
        self.speedx = 0
        self.speedy = 0

    def update(self):
        self.rect.x += self.speedx
        self.speedy = 0
        keystate = pg.key.get_pressed()
        if keystate[pg.K_UP]:
            self.speedy = -5
        if keystate[pg.K_DOWN]:
            self.speedy = 5
        self.rect.y += self.speedy
        if self.rect.bottom > HEIGHT:
            self.rect.bottom = HEIGHT
        if self.rect.top < 0:
            self.rect.top = 0

    def shoot(self):
        laser = laser1(self.rect.midright, self.rect.centery)
        all_sprites.add(laser)
        lasers.add(laser)

class Enemy1(pg.sprite.Sprite):
    def __init__(self):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.transform.scale(enemy1_img, (80, 60))
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()
        self.rect.y = random.randrange(HEIGHT - self.rect.height)
        self.rect.x = random.randrange(WIDTH + 40, WIDTH + 100)
        self.speedx = random.randrange(-8, -1)
    def update(self):
        self.rect.x += self.speedx
        if self.rect.right < 0:
            self.rect.y = random.randrange(HEIGHT - self.rect.height)
            self.rect.x = random.randrange(WIDTH + 40, WIDTH + 100)
            self.speedx = random.randrange(-8, -1)

class laser1(pg.sprite.Sprite):
    def __int__(self, x, y):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.Surface((10, 5))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.centery = y
        self.rect.left = x
        self.speedx = 15

    def update(self):
        self.rect.x += self.speedx
        #kill at right side of screen
        if self.rect.left > WIDTH:
            self.kill()

#  ---initializes pygame and creates window---
all_sprites = pg.sprite.Group()
mobs = pg.sprite.Group()
lasers = pg.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
    m = Enemy1()
    all_sprites.add(m)
    mobs.add(m)

# --------------Game loop-----------------

running = True
while running:
    # ------keep loop running at right speed-----
    clock.tick(FPS)
    # ------Process input (events)--------
    for event in pg.event.get():
        #check for closing window
        if event.type == pg.QUIT:
            running = False
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_SPACE:
                player.shoot()

    # ----------Update---------------
    all_sprites.update()

    # --------------Checking for Collisions----------------
    hits = pg.sprite.spritecollide(player, mobs, False)
    if hits:
        running = False

    # ---------Draw / render----------------
    screen.fill(BLACK)
    all_sprites.draw(screen)
    # flip after drawing
    pg.display.flip()

回答1:


The issue is caused by a typo. The name of the constructor is __init__ rather than __int__

def __int__(self, x, y):

def __init__(self, x, y):


来源:https://stackoverflow.com/questions/50677749/pygame-crashing-when-trying-to-shoot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!