URGENT! can someone help me fix error in my code i keep getting in __init__ self.color = Constants.COLORS AttributeError: cant set attribute `import pygame from
URGENT! can someone help me fix error in my code i keep getting "in __init__ self.color = Constants.COLORS AttributeError: cant set attribute"
`import pygame
from random import randint, choice
from Item import *
from Constants import *
import Constants
import random
class Person(pygame.sprite.Sprite, Item):
def __init__(self, name = "player 1", x = 0, y = 0):
self.name = name
self.x = x
self.y = y
self.size = 1
self.color = Constants.COLORS[1]
self.surf.fill(self.color)
self.surf = pygame.Surface((Constants.WIDTH, Constants.HEIGHT))
def setSize(self):
size = random.randint(10, 100)
self.surf = pygame.Surface((size, size))
@property
def color(self):
return self.color
def setColor(self):
self.color = random.choice(Constants.COLORS)
self.surf.fill(self.color)
def update(self, pressed_keys):
pressed_keys = pygame.key.get_pressed()
if pressed_keys[K_UP]:
self.Item.goUp(self)
elif pressed_keys[K_DOWN]:
self.Item.goDown(self)
elif pressed_keys[K_LEFT]:
self.Item.goLeft(self)
elif pressed_keys[K_RIGHT]:
self.Item.goRight(self)
elif pressed_keys[K_SPACE]:
self.setSize()
self.setColor()
def setRandomPosition(self):
self.x = random.randint(0, WIDTH - self.size)
self.y = random.randint(0, HEIGHT - self.size)
def __str__(self):
#string method that formats the output and creates columns using the TAB(\t)
return "Person({}):\tsize = {},\tx = {}\ty = {} Color = {}".format(self.name, self.size, self.x, self.y, self.color)
########################### main game################################
# DO NOT CHANGE ANYTHING BELOW THIS LINE
#####################################################################
# Initialize pygame library and display
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Create a person object
p = Person()
RUNNING = True # A variable to determine whether to get out of the
# infinite game loop
while (RUNNING):
# Look through all the events that happened in the last frame to see
# if the user tried to exit.
for event in pygame.event.get():
if (event.type == KEYDOWN and event.key == K_ESCAPE):
RUNNING = False
elif (event.type == QUIT):
RUNNING = False
elif (event.type == KEYDOWN and event.key == K_SPACE):
print(p)
# Otherwise, collect the list/dictionary of all the keys that were
# pressed
pressedKeys = pygame.key.get_pressed()
# and then send that dictionary to the Person object for them to
# update themselves accordingly.
p.update(pressedKeys)
# fill the screen with a color
screen.fill(WHITE)
# then transfer the person to the screen
screen.blit(p.surf, p.getPosition())
pygame.display.flip()` Below is the Constants file `
import pygame
from random import randint, choice
from Item import *
# constants for screen size
WIDTH = 1000
HEIGHT = 800
# constants for colors
RED = [0xe3, 0x1b, 0x23]
BLUE = [0x00,0x2F,0x8B]
GREY = [0xA2, 0xAA, 0xAD]
WHITE = [0xFF, 0xFF, 0xFF]
BLACK = [0x00, 0x00, 0x00]
COLORS = [BLUE, RED, GREY, WHITE, BLACK]
# keys from pygame
from pygame.locals import (
RLEACCEL,
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
K_SPACE,
)` below is item file
class Item:
class Item:
def init(self):
self.name = "Player 1"
self.x = 0
self.y = 0
self.size = 1
def goLeft(self, dist=1):
self.x -= dist
def goRight(self, dist=1):
self.x += dist
def goUp(self, dist=1):
self.y -= dist
def goDown(self, dist=1):
self.y += dist
pass`
below is the criteria Thank you for any help!
Note that the functionality that Person contained in the last assignment has been moved to another class i.e. Item. Person is a subclass of the Item class and has a few extra instance variables and functions. - color is represented by a list of 3 elements containing the hex values representation of a color e.g. [0xe3, 0x1b, 0x23] which represents red. - surf is the name given to a pygame surface (a square region of the screen that will represent the Person object). surf's dimensions are determined by the size of the Person/Item. - setcolor is a function that randomly selects the value to be stored in the color instance variable, and then changes the color of the surf to match that color. - setSize is a function that changes the size of the Person/Item to a random value between 10 and 100 , and then changes the size of the surf to match that size. - update receives as an argument a dictionary containing all the key pressed events and then updates the state of the Person based on what was pressed: if the up direction was pressed, then goUp is executed, if the down direction key was pressed, then goDown is executed if the left direction key was pressed, then goLeft is executed if the right direction key was pressed, then goRight is executed - if the space bar key was pressed, then the size and color of the Person is changed. - setRandomPosition updates the Person's x and y coordinates to a randomly selected value within the appropriate range i.e. HEIGHT and WIDTH. - getposition calculates and returns the coordinates of the top left corner of the rectangle representing the Person. Note that when pygame is attempting to draw a Surface on the screen, it requires the coordinates of the top left corner of the surface and yet the x and y coordinates of the Person should be the coordinates of the center of the Surface. getPosition calculates the appropriate coordinates using x,y and size and returns the result as a tuple. - A Person can be printed. The __str_ function adds the value of the color to the String representation of an Item. You are provided with a template file (Game-TEMPLATE.py) that will import and use your Item class. The file will also import another file (Constants .py) that contains some constants that can be used in your code. Make sure to look over them to identify what libraries and constants you have access toStep by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started