Reputation: 11
This is my code which draws a grid in a Pygame window which worked fine when I ran this on a PC in school. Now that I am trying to continue at home an error appears which I can't find a solution myself:
import sys, pygame
pygame.init()
white = (255, 255, 255)
black = (0,0,0)
orange = (255, 165,0)
green = (0,128, 0)
radius = 25
screen = pygame.display.set_mode((715,715))
screen.fill(white)
pygame.display.update()
def grid():
pygame.draw.line(screen, (black), (60, 0), (60,715))
pygame.draw.line(screen, (black), (360, 0), (360,715))
pygame.draw.line(screen, (black), (0, 65), (715, 65))
pygame.draw.line(screen, (black), (0, 130), (715, 130))
pygame.draw.line(screen, (black), (0, 195), (715, 195))
pygame.draw.line(screen, (black), (0, 260), (715, 260))
pygame.draw.line(screen, (black), (0, 325), (715, 325))
pygame.draw.line(screen, (black), (0, 390), (715, 390))
pygame.draw.line(screen, (black), (0, 455), (715, 455))
pygame.draw.line(screen, (black), (0, 520), (715, 520))
pygame.draw.line(screen, (black), (0, 585), (715, 585))
pygame.draw.line(screen, (black), (0, 650), (715, 650))
pygame.draw.line(screen, (black), (0,715), (715, 715))
pygame.display.update()
grid()
grid()
When I run this piece of code, I get an error:
Traceback (most recent call last):
File "C:\Users\Jack\Documents\Jack's Stuff\School\Year 10\Computer Science\Programs\Jack's Python programs\Useful programs\pygame_test.py", line 1, in <module>
import sys, pygame
File "C:\Users\Jack\Documents\Jack's Stuff\School\Year 10\Computer Science\Programs\Jack's Python programs\Useful programs\pygame.py", line 11, in <module>
screen = pygame.display.set_mode((715,715))
AttributeError: 'module' object has no attribute 'display'
What am I doing wrong with my code as I have tried several different things?
Upvotes: 0
Views: 970
Reputation: 1589
You're doing something quite unusual in your code. You are not instantiating a new object of type pygame. Instead you are running init manually on the module, then referencing the module directly. Presumably this is causing your error.
Try this instead
# rather than pygame.init()
game = pygame
# replace all future instances of "pygame" with "game"
game.display.update()
Upvotes: 0
Reputation: 3699
Module pygame
in your cwd is getting picked up. Is that the one you need ?
Upvotes: 1