我正在尝试学习Python,并希望使用Pygame进行练习。我目前正在使用Python 3.6.5并在Atom 1.27.2中运行代码并安装了atom-runner软件包。我一直在Youtube上关注Pygame的KidsCanCode tutorial。
我想改变代码,所以我不只是复制教程。目前一切正常,直到我试图从我的船上生成一颗子弹。子弹应该从左到右,从屏幕左侧的船上产生。一旦程序加载所有船只按预期移动并且碰撞正常,但是当我按空格键时游戏崩溃并出现以下错误:
TypeError: add() argument after * must be an iterable, not int
我认为这与我的player.shoot功能有关,但如果我删除该功能并尝试在...下生成子弹...
if event.key == pg.K_SPACE:
它仍然无法运作。
我的代码类似于视频中的代码,但它似乎仍然无效。也许这与子弹的正确位置有关,但此时我并不确定。任何帮助将不胜感激。
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()
答案 0 :(得分:2)
该问题是由拼写错误引起的。构造函数的名称是 __init__
而不是 __int__
def __int__(self, x, y):
def __init__(self, x, y):