我目前正在使用程序生成的pygame中的关卡制作2D平台游戏,但是我收到以下错误消息,这使我无法继续:
Traceback (most recent call last):
File "C:\Users\Win10\Desktop\SCV v3.2.py", line 63, in <module>
main() #calls the main function
File "C:\Users\Win10\Desktop\SCV v3.2.py", line 36, in main
block_object = block_class(0, 5)
File "C:\Users\Win10\Desktop\SCV v3.2.py", line 29, in __init__
self.image.Rect = (32, 32) #block is 32*32 pixels large
AttributeError: 'pygame.Surface' object has no attribute 'Rect'
我首先想到的是,我只是以错误的顺序创建了一些类和函数,但这似乎并没有帮助,然后我尝试重命名一些变量,并确保我正确地进行了操作。用这个事实命名了我所有的变量。然后我去youtube看看是否还有其他人遇到类似的问题,我发现它在python 3.4中有效,因为我看到了非常相似的类工作,但是我感觉这只会给其余的问题带来更多问题我只使用了从3.6到现在的python版本的代码。 这是有问题的代码:
import pygame #imports pygame
import time #imports the timer so I can use the tick function to make game 60 fps
import sys #imports system
from pygame import * #imports all pygame files
win_height = 500 #height of the window is 500 pixles
win_width = 500 #width of the window is 500 pixels
red = (255, 0, 0) #makes red a preset colour using rgb
green = (0, 255, 0) #makes green a preset colour using rgb
display = (win_height, win_width) #creates the windown as 500*500 pixels
depth = 32 #prevents infinate recursion
timer = pygame.time.Clock() #creates a timer
flags = 0 #I don't really know what this does, however I have seen in many places it being used, therefore I assumed that it was important
screen = pygame.display.set_mode(display, depth, flags) #loads up a pygame window
class entity(pygame.sprite.Sprite): #makes player a sprite
def __init__(self):
pygame.sprite.Sprite.__init__(self) #sets sprite to initiate
class block_class(entity):
def __init__(self, x, y):
self.image = Surface((32, 32))
self.image.rect = (32, 32) #block is 32*32 pixels large
self.image.fill(Color ("#FF0400")) #block is red
self.image.convert()
self.rect = Rect(x, y, 32, 32)
def main(): #main game function
block_object = block_class(0, 5)
player_object = player_class(0,0)
while 1: #updates the screen so you can see changes like movement
timer.tick(60)
player_object.update()
screen.fill(red) #makes the screen red(will probably be temporary)
pygame.display.update()
class player_class(entity): #defines the player class
def __init__(self, x, y): #x is the players x coordinate, y is player y coordinate
self.xvel = 0 #how fast the player is moving to the left and right
self.yvel = 0 #how fast the player is moving up and down
self.image = Surface((32, 32))
self.image.rect = (32, 32) #player is 32*32 pixels large
self.image.fill(Color ("#00FF33")) #player is green
self.image.convert()
self.rect = Rect(x, y, 32, 32)
def update():
pass
main() #calls the main function