python - Chasing game - Error message: "'module' object has no attribute 'display'" -
i new programming , trying set simulation in 1 circle moving in random pattern , being chased second circle. hoping add 5 circles distractors moving around randomly.
in code called circle moving randomly "mouse" , circle chasing "cat".
i researched online , looked @ other people's code ideas , came far:
from pygame import * import random import sys, pygame, math, random pygame.locals import * pygame.init() background_colour = (255,255,255) (width, height) = (1024, 768) screen = pygame.display.set_mode((width, height),pygame.fullscreen) class mouse(pygame.sprite.sprite): def __init__(self, (x, y), size): pygame.sprite.sprite.__init__(self) self.x = mx self.y = self.size = 30 self.colour = (0, 0, 0) self.thickness = 2 self.speed = 2 self.angle = random.uniform(0, math.pi*2) def display(self): pygame.draw.circle(screen, self.colour, (int(mx), int(my)), self.size, self.thickness) def move(self): self.x += math.sin(self.angle) * self.speed self.y -= math.cos(self.angle) * self.speed class cat(pygame.sprite.sprite): def __init__(self, (x, y), size): pygame.sprite.sprite.__init__(self) self.x = cx self.y = cy self.size = 30 self.colour = (0, 0, 0) self.thickness = 2 self.speed = 2 self.angle = random.uniform(0, math.pi*2) pixchangec = 2 def display(self): pygame.draw.circle(screen, self.colour, (int(cx), int(cy)), self.size, self.thickness) def move(self): if mx >= cx: cx += pixchangec else: cx -= pixchangec if >= cy: cy += pixchangec else: cy -= pixchangec def main(): pygame.display.set_caption('chase') mouse = mouse() cat = cat() running = true while running: event in pygame.event.get(): if event.type == pygame.quit or (event.type == keyup , event.key == k_escape): pygame.quit() sys.exit() screen.fill(background_colour) mouse.display() mouse.move() cat.display() cat.move() pygame.display.flip()
unfortunately, when try run code this, following error message:
" file "c:...", line 75, in mouse.display() attributeerror: 'module' object has no attribute 'display'"
i not find answer online going wrong if has advice/ideas, appreciate help!
pydude on right track stopped way soon. you're getting un-helpful error message because pygame.mouse
(with small m) imported when use from blank import *
method of importing pygame, should overwrite 1 have mouse = mouse()
. problem indentation wrong. running = true
down needs indented once more, including line. then, main()
function run, need include @ end of file:
if __name__ == "__main__": main()
so happening when run code have skips right on main() method (which never called in code) , runs stuff after indentation goes back, starting running = true
. when gets mouse.display()
you haven't run line mouse = mouse()
yet uses mouse
imported pygame. mouse
doesn't have display()
method, hence error message.
you'll have more errors in code after fixing that, change past 1 error. next 1 has more helpful error message @ least, should able figure out.
also, advice: from blank import *
discouraged reason. it's better either import pygame
, refer as, eg, pygame.sprite
(which you're doing well...which odd you're doing both...) or from pygame import sprite, foo, bar, ...
, explicitly list each specific thing want out of module. these 2 methods prevent having unknown stuff cluttering namespace, , have prevented getting confusing error message couldn't figure out.
Comments
Post a Comment