我需要一些帮助来了解此错误

时间:2020-04-23 01:09:29

标签: python python-3.x pygame

我正在使用python尝试制作游戏,但不断收到错误消息,在特定情况下我不理解该解决方案。

我的错误如下: TypeError:-=:'CreateEnemy'和'int'的不受支持的操作数类型

此行发生错误:

if Player.x < Missle.x:
    Missle.x -= Espeed

如果有助于提供更多详细信息,我将在文件的其余部分粘贴以下内容:

#!/usr/bin/env python

# Import the pygame library and initialise the game engine
import pygame
import sys
import math
pygame.init()

# Open a new window
WinSize = [1000, 900]
size = (WinSize[0], WinSize[1])
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Space Adventures")


# Global Variables
FPS = 30
Speed = 30
Espeed = 5


class CreatePlayer(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.x = 30
        self.y = 20
        self.image = pygame.image.load('SpaceShip.png')
        self.rect = pygame.Rect(self.x,self.y,210,190) #The rect for collision detection.

class CreateEnemy(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.x = 580
        self.y = 420
        self.image = pygame.image.load("EnemyMissle.png")
        self.rect = pygame.Rect(self.x,self.y,110,90)

class CreateBoom(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("Explosion.png")

# We create the player object
Player = CreatePlayer()
# We create the enemy object
Missle = CreateEnemy()
# We create the explosion FX
Explosion = CreateBoom()

# Game Loop and clock
GameOver = False
clock = pygame.time.Clock()


# This function works specificallSpaceShipImgy for the KEYDOWN function
# 10(1) = initial interval and 10(2) = repeat interval
pygame.key.set_repeat(10,10)

# This function can detect collision between two different sprites
def col_check(x,y,w,h,x2,y2,w2,h2):
    if (x < (x2 + w2) and (x + w) > x2 and y < (y2 + h2) and (h + y) > y2):                     
        print("collision detected")
        screen.blit(Explosion.image, (Player.x +30, Player.y + 20)) # we have to offset the player x and y for convincing effect


while not GameOver:

    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
              pygame.display.quit()
              pygame.quit()
              sys.exit()

        if event.type == pygame.KEYDOWN: # event space for user Key input

            if event.key == pygame.K_RIGHT:
                if (Player.x + 210 >= WinSize[0]):
                    Player.x = WinSize[0] - 210

                else:
                    Player.x += Speed


            if event.key == pygame.K_LEFT:        
                if (Player.x <= 0):                # Left side collision
                    Player.x = 0

                else:                             # Left Side movement
                    Player.x -= Speed

            if event.key == pygame.K_UP:
                if (Player.y <= 0):                # Up side collision
                    Player.y = 0

                else:                             # Up side movement
                    Player.y -= Speed

            if event.key == pygame.K_DOWN:
                if (Player.y + 190 >= WinSize[1]): # Down side collision
                    Player.y = WinSize[1] - 190

                else:                             # Down side movement
                    Player.y += Speed



    # --- Drawing code should go here

    # Missle
    PointAngle = math.atan2(Player.y - Missle.y, Player.x - Missle.x)
    MissleSurf = pygame.transform.rotate(Missle.image, 270 - PointAngle * 57.29) # 270 and 57.29 are random numbers found online / 360
    screen.blit(MissleSurf, (Missle.x, Missle.y))

    if Player.x > Missle.x:
        Missle.x += Espeed

    if Player.x < Missle.x:
        Missle.x -= Espeed

    # y axis movement
    if Player.y > Missle.y:
        Missle.y += Espeed

    if Player.y < Missle.y:
        Missle -= Espeed

    # Player
    screen.blit(Player.image, (Player.x, Player.y)) # paint to screen

    # Explosion
    col_check(Player.x, Player.y, 210, 190, Missle.x, Missle.y, 110, 90)


    pygame.display.update()
    screen.fill(0)
    clock.tick(FPS)


1 个答案:

答案 0 :(得分:5)

错误似乎在另一行

if Player.y < Missle.y:
    Missle -= Espeed

应该是

if Player.y < Missle.y:
    Missle.y -= Espeed