我已经安装了pygame(1.9.1 v)。 这是我的代码:
# 1 - Import library
import pygame
from pygame.locals import *
import math
import random
# 2 - Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
keys = [False, False, False, False]
playerpos=[100,100]
# 3 - Load images
player = pygame.image.load("resources/images/messi.jpg")
podo = pygame.image.load("resources/images/podo3.png")
podo1 = pygame.image.load("resources/images/podo1.png")
# 4 - keep looping through
while 1:
# 5 - clear the screen before drawing it again
screen.fill(0)
# 6 - draw the screen elements
for x in range(width/podo.get_width()+1):
for y in range(height/podo.get_height()+1):
screen.blit(podo,(x*0,y*0))
screen.blit(podo1,(0,345))
screen.blit(player, playerpos)
# 7 - update the screen
pygame.display.flip()
# 8 - loop through the events
for event in pygame.event.get():
# check if the event is the X button
if event.type==pygame.QUIT:
# if it is quit the game
pygame.quit()
exit(0)
if event.type == pygame.KEYDOWN:
if event.key==K_w:
keys[0]=True
elif event.key==K_a:
keys[1]=True
elif event.key==K_s:
keys[2]=True
elif event.key==K_d:
keys[3]=True
if event.type == pygame.KEYUP:
if event.key==pygame.K_w:
keys[0]=False
elif event.key==pygame.K_a:
keys[1]=False
elif event.key==pygame.K_s:
keys[2]=False
elif event.key==pygame.K_d:
keys[3]=False
# 9 - Move player
if keys[0]:
playerpos[1]-=5
elif keys[2]:
playerpos[1]+=5
if keys[1]:
playerpos[0]-=5
elif keys[3]:
playerpos[0]+=5
然而它一直向我显示信息:
Traceback (most recent call last):
File "C:\Users\George\Desktop\game.py", line 2, in <module>
import pygame
File "C:\Users\George\Desktop\pygame.py", line 6, in <module>
pygame.init()
NameError: name 'pygame' is not defined
我删除了库并再次安装它,但它没有改变任何东西。我该怎么办?
答案 0 :(得分:5)
您的桌面文件夹中有一个名为pygame.py
的文件,它正在屏蔽pygame
库:
import pygame
File "C:\Users\George\Desktop\pygame.py", line 6, in <module>
请注意,行import pygame
导入了文件C:\Users\George\Desktop\pygame.py
。
将该文件重命名为其他文件或完全删除它。