gpt4 book ai didi

python - 我需要一些帮助来理解这个错误

转载 作者:行者123 更新时间:2023-12-02 02:54:43 27 4
gpt4 key购买 nike

我正在使用 python 尝试制作游戏,但我不断收到错误,我不明白在我的特定上下文中的解决方案。

我遇到的错误如下:类型错误:-= 不支持的操作数类型:“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)


最佳答案

错误似乎在另一行

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

应该是

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

关于python - 我需要一些帮助来理解这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61377670/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com