Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我会得到TypeError:'<';在';int';和';元组';?_Python_Loops_Variables_Pygame_Runtime Error - Fatal编程技术网

Python 为什么我会得到TypeError:'<';在';int';和';元组';?

Python 为什么我会得到TypeError:'<';在';int';和';元组';?,python,loops,variables,pygame,runtime-error,Python,Loops,Variables,Pygame,Runtime Error,我需要一个TypeError:'问题是由它引起的,因为敌军的构造函数的第一个参数必须是一个y坐标: class敌人: 定义初始值(自身、y、宽度、高度) # [...] self.y=y 但是当你将敌人对象装箱时,第一个参数是一个元组((x,rock.y)): 小行星在屏幕上。附加(敌人((x,rock.y),rock.width,rock.height)) 因此,a.y是一个元组,而不是单个值。可能是: 小行星在屏幕上。追加(敌人(岩石y,岩石宽度,岩石高度)) 敌人初始值设定项:def\

我需要一个
TypeError:'问题是由它引起的,因为
敌军的构造函数的第一个参数必须是一个y坐标:

class敌人:
定义初始值(自身、y、宽度、高度)
# [...]
self.y=y
但是当你将
敌人
对象装箱时,第一个参数是一个元组(
(x,rock.y)
):

小行星在屏幕上。附加(敌人((x,rock.y),rock.width,rock.height))
因此,
a.y
是一个元组,而不是单个值。可能是:

小行星在屏幕上。追加(敌人(岩石y,岩石宽度,岩石高度))

敌人
初始值设定项:
def\uuuu init\uuuu(self,y,width,height)

你通过:
敌人((x,rock.y),rock.width,rock.height))

所以你的
敌人.y
实际上是
(x,rock.y)
的元组。当然,您不能将其与
int
进行比较


下次,打印出您正在比较的内容,这样的错误会很明显。

请分享哪一行代码产生了错误。另外,看看这是否有帮助:@ItamarMushkin错误发生在第158行
while run:
    last = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == my_event_id:
            x = random.randrange(screen_width - 64 * 2)
            index = random.choice(number)
            asteroids_on_screen.append(Enemy((x, rock.y), rock.width, rock.height))

    for a in asteroids_on_screen:
        if 0 < a.y < 500: # right here is the error
            a.y -= a.vel
        else:
            asteroids_on_screen.pop(asteroids_on_screen.index(a))
import pygame

import random

pygame.init()

screen_width = 500
screen_height = 500
win = pygame.display.set_mode((screen_width, screen_height))
walk_left = [pygame.image.load('sprite_5.png'), pygame.image.load('sprite_6.png')]
walk_right = [pygame.image.load('sprite_3.png'), pygame.image.load('sprite_4.png')]
standing = [pygame.image.load('sprite_0.png'), pygame.image.load('sprite_1.png'), pygame.image.load('sprite_2.png')]


class Player:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 4
        self.left = False
        self.right = False
        self.standing = True
        self.walk_count = 0
        self.hitbox = (self.x + 2, self.y + 26, 123, 45)

    def draw(self, win):
        if self.walk_count + 1 >= 12:
            self.walk_count = 0

        if not self.standing:
            if self.left:
                win.blit(walk_left[self.walk_count // 6], (self.x, self.y))
                self.walk_count += 1
            elif self.right:
                win.blit(walk_right[self.walk_count // 6], (self.x, self.y))
                self.walk_count += 1
        else:
            win.blit(standing[self.walk_count // 4], (self.x, self.y))
            self.walk_count += 1
        self.hitbox = (self.x + 2, self.y + 31, 123, 40)
        pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)


def move():
    if keys[pygame.K_LEFT] and man.x > man.vel or keys[pygame.K_a] and man.x > man.vel:
        man.x -= man.vel
        man.left = True
        man.right = False
        man.standing = False

    elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
        man.x += man.vel
        man.left = False
        man.right = True
        man.standing = False

    else:
        man.standing = True


class Enemy:
    def __init__(self, y, width, height):
        self.width = width
        self.height = height
        self.vel = 1.5
        self.y = y
        x = random.randrange(screen_width - 64 * 2)
        self.hitbox = (x, self.y, self.width, self.height)

    def draw(self, win):
        if index == 0:
            self.hitbox = (x + 68, self.y + 68, self.width - 10, self.height - 14)
            pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
        elif index == 1:
            self.hitbox = (x + 38, self.y + 47, self.width + 20, self.height - 5)
            pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
        elif index == 2:
            self.hitbox = (x + 18, self.y + 12, self.width + 32, self.height + 30)
            pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
        elif index == 3:
            self.hitbox = (x + 20, self.y + 32, self.width + 16, self.height + 5)
            pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
        else:
            self.hitbox = (x + 4, self.y + 7, self.width - 24, self.height - 31)
            pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)


my_event_id = pygame.USEREVENT + 1
pygame.time.set_timer(my_event_id, 1000)


class Projectile:
    def __init__(self, x, y, width, height, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.vel = 5

    def draw(self, win):
        pygame.draw.rect(win, self.color, (self.x, self.y, self.height, self. width))


class Unit:
    def __init__(self):
        self.last = pygame.time.get_ticks()
        self.cooldown = 200

    def fire(self):
        now = pygame.time.get_ticks()
        if now - self.last >= self.cooldown:
            self.last = now
            spawn_bullet()


def spawn_bullet():
    if keys[pygame.K_SPACE]:
        bullets.append(Projectile((man.x + 126 // 2), (man.y + 5), 7, 3, (255, 0, 0)))


def re_draw():
    win.fill((0, 0, 0))
    man.draw(win)
    for bullet in bullets:
        bullet.draw(win)
    for a in asteroids_on_screen:
        a.draw(win)
    pygame.display.update()


asteroids = [pygame.image.load('rock0.png'), pygame.image.load('rock1.png'), pygame.image.load('rock2.png'),
             pygame.image.load('rock3.png'), pygame.image.load('rock4.png')]

number = [0, 1, 2, 3, 4]

delay = Unit()
man = Player(186, 400, 128, 128)
bullets = []
asteroids_on_screen = []
rock = Enemy(10, 64, 64)

run = True
clock = pygame.time.Clock()
while run:
    last = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == my_event_id:
            x = random.randrange(screen_width - 64 * 2)
            index = random.choice(number)
            asteroids_on_screen.append(Enemy((x, rock.y), rock.width, rock.height))

    for a in asteroids_on_screen:
        if 0 < a.y < 500:
            a.y -= a.vel
        else:
            asteroids_on_screen.pop(asteroids_on_screen.index(a))

    for bullet in bullets:
        if 0 < bullet.y < 500:
            bullet.y -= bullet.vel
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()
    move()
    delay.fire()
    clock.tick(60)
    re_draw()

pygame.quit()