#sprites
import random, sys, time, math, pygame
from pygame.locals import *
from pygame import mixer # Load the required library
FPS = 30 # frames per second to update the screen
fpsClock = pygame.time.Clock()
WINWIDTH = 1500 # width of the program's window, in pixels
WINHEIGHT = 1000 # height in pixels
HALF_WINWIDTH = int(WINWIDTH / 2)
HALF_WINHEIGHT = int(WINHEIGHT / 2)
moveX,moveY=0,0
move2X,move2Y=0,0
GRASSCOLOR = (24, 255, 0)
WHITE = (255, 255, 255)
PURPLE = (128, 0, 128)
RED = (255, 0, 0)
pygame.init()
global FPSCLOCK, DISPLAYSURF, self
DISPLAYSURF = pygame.display.set_mode((WINWIDTH, WINHEIGHT))
pygame.display.set_icon(pygame.image.load('god.png'))
pygame.display.set_caption('Smash Bros Melee')
BASICFONT = pygame.font.Font('freesansbold.ttf', 32)
class Sprite:
def __init__(self,x,y):
self.x=x
self.y=y
self.width=50
self.height=50
self.i0= pygame.image.load('ryanrenolds.jpeg')
self.timeTarget=10
self.timeNum=0
self.currentImage=0
def render(self):
DISPLAYSURF.blit(self.i0, (self.x, self.y))
player=Sprite(0,0)
class Sprite2:
def __init__(self,x,y):
self.x=x
self.y=y
self.width=50
self.height=50
self.i0= pygame.image.load('ArkhamKnight.png')
self.i1= pygame.image.load('Black-Panther.png')
self.timeTarget=10
self.timeNum=0
self.currentImage=0
def update(self):
self.timeNum+=1
if (self.timeNum==self.timeTarget):
if (self.currentImage==0):
self.currentImage+=1
else:
self.currentImage=0
self.timeNum=0
self.render()
def render(self):
if(self.currentImage==0):
DISPLAYSURF.blit(self.i0, (self.x, self.y))
else:
DISPLAYSURF.blit(self.i1, (self.x, self.y))
player2=Sprite2(200,300)
def checkCollision(Sprite, Sprite2):
col = Sprite.rect.colliderect(Sprite2.rect)
if col == True:
sys.exit()
while True:
for event in pygame.event.get():
if event.type == KEYUP:
if event.key == K_a:
moveX = moveX - 0
elif event.key == K_d:
moveX == False
elif event.key == K_w:
moveY == False
elif event.key == K_s:
moveY == False
if (event.type==pygame.KEYDOWN):
if (event.key==pygame.K_a):
moveX = moveX - 5
elif (event.key==pygame.K_d):
moveX = moveX + 5
elif (event.key==pygame.K_w):
moveY = moveY - 5
elif (event.key==pygame.K_s):
moveY = moveY + 5
elif (event.key==pygame.K_ESCAPE):
pygame.quit()
sys.exit()
if event.type == KEYUP:
if event.key == K_LEFT:
move2X = move2X - 0
elif event.key == K_RIGHT:
move2X == False
elif event.key == K_UP:
move2Y == False
elif event.key == K_DOWN:
move2Y == False
if (event.type==pygame.KEYDOWN):
if (event.key==pygame.K_LEFT):
move2X = move2X - 5
elif (event.key==pygame.K_RIGHT):
move2X = move2X + 5
elif (event.key==pygame.K_UP):
move2Y = move2Y - 5
elif (event.key==pygame.K_DOWN):
move2Y = move2Y + 5
elif (event.key==pygame.K_o):
player=Sprite(0,0)
player2=Sprite2(200,300)
elif (event.key==pygame.K_ESCAPE):
pygame.quit()
sys.exit()
DISPLAYSURF.fill(PURPLE)
player.x+=moveX
player.y+=moveY
player2.x+=move2X
player2.y+=move2Y
player.render()
player2.update()
checkCollision(Sprite, Sprite2)
pygame.display.update()
fpsClock.tick(FPS)
pygame.quit()
sys.exit()
如何佩戴我的精灵会碰撞我的碰撞直接代码,当我在参数中使用self时会要求三个参数但是当我自我解决时它表示类Sprite没有属性'rect'
这个错误我一直在弄清楚我如何解决这个问题到我的游戏工作的地方,如果他们互相碰撞我的精灵会退出游戏
追踪(最近一次通话): 文件“C:\ Python27 \ Sprites \ sprites.py”,第159行,in checkCollision(Sprite,Sprite2) 在checkCollision中输入文件“C:\ Python27 \ Sprites \ sprites.py”,第88行 col = Sprite.rect.colliderect(Sprite2.rect) AttributeError:类Sprite没有属性'rect'
答案 0 :(得分:0)
您的Sprite类中没有属性public override void OnViewRecycled(Java.Lang.Object holder)
{
var MyHolder = holder as ViewHolder;
(MyHolder.ItemView as ImageView).RemoveAllViewsInLayout();
base.OnViewRecycled(MyHolder);
}
,这会导致此问题。您还需要Sprite2类中的属性rect
。您可以使用rect
方法执行此操作。
您可以在Sprite类中执行此操作,方法是在get_rect()
__init__(self)
对于Sprite2类,您可以执行此操作(因为显示的图像可以更改,将其放在self.rect = self.i0.get_rect()
中。)
update()
我希望这会有所帮助。