Python 我试图编写一个游戏,并进行碰撞,但我总是得到相同的错误 #基本Pygame结构 导入pygame#导入pygame和其他库 导入操作系统,系统 随机输入 #在此处定义类(精灵) img_path=os.path.join(“punch.png”) img_rock=os.path.join(“rocks_rotated.png”) 类别gen_对象(对象): 定义初始值(self,x=random.randint(1580),y=random.randint(1480)): self.image=pygame.Surface([160160]) self.image.set_颜色键(黑色) self.image.blit(pygame.image.load(img_rock),(0,0)) self.image\u b=self.image.get\u rect() self.x=x self.y=y 距离=10 self.dist=dist def rock(自我): 距离=10 self.x-=距离 def绘图(自、表面): blit(self.image,(self.x,self.y)) def碰撞(自身、精灵1、精灵2): col=pygame.sprite.collide_rect(sprite1,sprite2) 如果col==True: sys.exit() 类字符(对象): 定义初始化(self,x=275,y=250): self.image=pygame.Surface([160160]) self.image.set_颜色键(黑色) self.image.blit(pygame.image.load(img_路径),(0,0)) self.image\u b=self.image.get\u rect() self.x=x self.y=y def移动(自): key=pygame.key.get_pressed() 距离=10 如果键[pygame.K_w]: self.y-=距离 elif key[pygame.K_s]: self.y+=dist 如果键[pygame.K_a]: self.x-=距离 elif key[pygame.K_d]: self.x+=dist 如果self.x>580: self.x=580 如果self.y480: self.y=480 如果self.x220和self.y260且self.x220,self.y260,self.x
但是我得到了这个错误:Python 我试图编写一个游戏,并进行碰撞,但我总是得到相同的错误 #基本Pygame结构 导入pygame#导入pygame和其他库 导入操作系统,系统 随机输入 #在此处定义类(精灵) img_path=os.path.join(“punch.png”) img_rock=os.path.join(“rocks_rotated.png”) 类别gen_对象(对象): 定义初始值(self,x=random.randint(1580),y=random.randint(1480)): self.image=pygame.Surface([160160]) self.image.set_颜色键(黑色) self.image.blit(pygame.image.load(img_rock),(0,0)) self.image\u b=self.image.get\u rect() self.x=x self.y=y 距离=10 self.dist=dist def rock(自我): 距离=10 self.x-=距离 def绘图(自、表面): blit(self.image,(self.x,self.y)) def碰撞(自身、精灵1、精灵2): col=pygame.sprite.collide_rect(sprite1,sprite2) 如果col==True: sys.exit() 类字符(对象): 定义初始化(self,x=275,y=250): self.image=pygame.Surface([160160]) self.image.set_颜色键(黑色) self.image.blit(pygame.image.load(img_路径),(0,0)) self.image\u b=self.image.get\u rect() self.x=x self.y=y def移动(自): key=pygame.key.get_pressed() 距离=10 如果键[pygame.K_w]: self.y-=距离 elif key[pygame.K_s]: self.y+=dist 如果键[pygame.K_a]: self.x-=距离 elif key[pygame.K_d]: self.x+=dist 如果self.x>580: self.x=580 如果self.y480: self.y=480 如果self.x220和self.y260且self.x220,self.y260,self.x,python,pygame,collision,rect,Python,Pygame,Collision,Rect,但是我得到了这个错误: # Basic Pygame Structure import pygame # Imports pygame and other libraries import os,sys import random # Define Classes (sprites) here img_path = os.path.join("punch.png") img_rock = os.path.join("rock
# Basic Pygame Structure
import pygame # Imports pygame and other libraries
import os,sys
import random
# Define Classes (sprites) here
img_path = os.path.join("punch.png")
img_rock = os.path.join("rocks_rotated.png")
class gen_object(object):
def __init__(self, x = random.randint(1,580), y = random.randint(1,480)):
self.image = pygame.Surface([160,160])
self.image.set_colorkey(black)
self.image.blit(pygame.image.load(img_rock),(0,0))
self.image_b = self.image.get_rect()
self.x = x
self.y = y
dist = 10
self.dist = dist
def rock(self):
dist = 10
self.x -=dist
def draw(self,surface):
surface.blit(self.image,(self.x,self.y))
def collision(self, sprite1, sprite2):
col = pygame.sprite.collide_rect(sprite1, sprite2)
if col == True:
sys.exit()
class character(object):
def __init__(self, x = 275, y = 250):
self.image = pygame.Surface([160,160])
self.image.set_colorkey(black)
self.image.blit(pygame.image.load(img_path),(0,0))
self.image_b = self.image.get_rect()
self.x = x
self.y = y
def movement(self):
key = pygame.key.get_pressed()
dist = 10
if key[pygame.K_w]:
self.y -= dist
elif key[pygame.K_s]:
self.y += dist
if key[pygame.K_a]:
self.x -= dist
elif key[pygame.K_d]:
self.x += dist
if self.x > 580:
self.x = 580
if self.y < 0:
self.y = 0
elif self.y > 480:
self.y = 480
if self.x <0:
self.x = 0
elif self.x == 580 and self.y > 220 and self.y < 240:
self.x = 5
if self.y == 0 and self.x >260 and self.x < 285:
self.y = 475
elif self.x == 0 and self.y > 220 and self.y < 240:
self.x = 575
if self.y == 480 and self.x >260 and self.x < 285:
self.y = 5
# top of left and right doors are y = 220, bottom of left and right doors are y = 240
# right side of top and bottom doors are x = 285, left of top and bottom doors are x = 260
def draw(self,surface):
surface.blit(self.image,(self.x,self.y))
pygame.init() # Pygame is initialised (starts running)
players = pygame.sprite.Group()
screen = pygame.display.set_mode([700,600]) # Set the width and height of the screen [width,height]
pygame.display.set_caption("My Game") # Name your window
background_image = pygame.image.load("untilted.png")
done = False # Loop until the user clicks the close button.
clock = pygame.time.Clock() # Used to manage how fast the screen updates
black = ( 0, 0, 0) # Define some colors using rgb values. These can be
white = ( 255, 255, 255) # used throughout the game instead of using rgb values.
bird = character()
rock = gen_object()
# -------- Main Program Loop -----------
while done == False:
for event in pygame.event.get(): # Check for an event (mouse click, key press)
if event.type == pygame.QUIT: # If user clicked close window
done = True # Flag that we are done so we exit this loop
# Update sprites here
if rock.x < 0:
y = random.randint(0,590)
rock = gen_object(700,y)
rock.collision(bird.image_b,rock.image_b)
bird.movement()
screen.blit(background_image,[0,0])
bird.draw(screen)
rock.rock()
rock.draw(screen)
pygame.display.update() # Go ahead and update the screen with what we've drawn.
clock.tick(20) # Limit to 20 frames per second
pygame.quit() # Close the window and quit.
回溯(最近一次呼叫最后一次):
文件“/Users/arranmcfadyn/Desktop/CourseWork/CourseWork.py”,第154行,在
岩石。碰撞(鸟。图像,岩石。图像)
文件“/Users/arranmcfadyen/Desktop/CourseWork/CourseWork.py”,第37行,冲突
col=pygame.sprite.collide_rect(sprite1,sprite2)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pygame/sprite.py”,第1300行,在collide_rect中
返回left.rect.colliderect(right.rect)
AttributeError:“pygame.Rect”对象没有属性“Rect”
pygame.sprite.collide_rect()
需要两个sprite
对象或具有self.rect
改变
Traceback (most recent call last):
File "/Users/arranmcfadyen/Desktop/CourseWork/coursework.py", line 154, in <module>
rock.collision(bird.image_b,rock.image_b)
File "/Users/arranmcfadyen/Desktop/CourseWork/coursework.py", line 37, in collision
col = pygame.sprite.collide_rect(sprite1, sprite2)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site- packages/pygame/sprite.py", line 1300, in collide_rect
return left.rect.colliderect(right.rect)
AttributeError: 'pygame.Rect' object has no attribute 'rect'
进入
然后使用
self.rect = self.image.get_rect()
顺便问一下:你为什么要用
rock.collision(bird, rock) # without .image_b or .rect
代替
self.image = pygame.Surface([160,160])
self.image.set_colorkey(black)
self.image.blit(pygame.image.load(img_rock),(0,0))
self.image = pygame.Surface([160,160])
self.image.set_colorkey(black)
self.image.blit(pygame.image.load(img_rock),(0,0))
self.image = pygame.image.load(img_rock)
self.image.set_colorkey(black)