设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 126283|回复: 10
打印 上一主题 下一主题

集中答疑专用贴

  [复制链接]

0

主题

0

好友

64

积分

注册会员

Rank: 2

楼主
发表于 2018-4-30 19:29:37 |显示全部楼层
crossing先生你好,

我刚开始学pygame,安装了pygame,  

MacBook-Pro:~ Max$ pip3 --version
pip 9.0.1 from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (python 3.6)

但运行import pygame,都显示

Traceback (most recent call last):
  File "/Users/Max/Desktop/Untitled.py", line 1, in <module>
    import pygame
ModuleNotFoundError: No module named 'pygame'

我的是py自带的IDLE3.6.4。Mac系统。
这是怎么回事?
谢谢!
回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

沙发
发表于 2018-5-1 16:22:52 |显示全部楼层
crossin先生 发表于 2018-5-1 15:55
你是怎么装的,pip list里面有没有pygame,如果没有就是没装对

下载python会自带pygame库吗?我在terminal查了下

MacBook-Pro:~ Max$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the
    section) to disable this warning.
    basemap (1.0.4)
    Cython (0.16)
    fonttools (2.3)
    h5py (2.0.1)
    ipython (0.12.1)
    matplotlib (1.1.1)
    netCDF4 (1.0)
    nose (1.1.2)
    numexpr (2.0.1)
    numpy (1.6.2)
    Pillow (1.7.7)
    pip (9.0.3)
    Polygon (2.0.1)
    pygame (1.9.1release)
    scipy (0.10.1)
    setuptools (39.0.1)
    sympy (0.7.1)
    tables (2.3.1)
    TTFQuery (1.0.4)
    You are using pip version 9.0.3, however version 10.0.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.

    这里面有的,还是要另外安装呢?谢谢
回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

板凳
发表于 2018-5-1 19:16:54 |显示全部楼层
还有,我的mac版本是10.13.4,是否与现有的mac版pygame不适配。谢谢。
回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

地板
发表于 2018-5-4 07:27:03 |显示全部楼层
crossin先生 发表于 2018-5-1 23:47
pip里有,那至少应该import不会有问题。除非你电脑上装了多个python,然后你执行的python并不是pip这个

...

Crossin先生你好,

折腾好多天终于把import pygame解决了,我下了python2和3(32bit和64bit)都会报错,后来在stackoverflow上看到这个回答:

The Python 2.7.3 .dmg Mac OS installer installs both 64-bit and 32-bit binaries in:

/Library/Frameworks/Python.framework/Versions/2.7/bin/

There is a 32-bit binary called python2.7-32 in that folder.

To use it in the Terminal simply type $ python2.7-32 instead of python

To use it in IDLE simply rename the 64-bit python2.7 binary to something like python2.7-64 then rename python2.7-32' topython2.7` and next time you launch IDLE or the Terminal it will use the 32-bit binary. Change it back when you are done.

You can also force launch IDLE in 32-bit mode from the Terminal:

$ arch -i386 /Library/Frameworks/Python.framework/Versions/2.7/bin/idle2.7 -n

You can create a shell script Automator application to make it easier to launch.

就解决了,下载了32bit的python为何还要手动改成32bit模式呢?谢谢
回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

5#
发表于 2018-5-6 10:30:54 |显示全部楼层
Crossin先生你好,

学pygame用什么编辑器比较好?我下了SPE但显示打不开,这是为什么?谢谢。

Max
回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

6#
发表于 2018-5-12 17:41:07 |显示全部楼层
Crossin先生你好,下面这段代码中 pygame.draw.rect(screen,color,[left,top,width,height],line_width) pygame.display.flip() 显示 SyntaxError: invalid syntax,是什么问题?谢谢。

import pygame,sys,random
from pygame.color import THECOLORS
pygame.init()
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
for i in range(100):
    width=random.randint(0,250)
    height=random.randint(0,100)
    top=random.randint(0,400)
    left=random.randint(0,500)
    color_name=random.choice(THECOLORS.keys())
    color=THECOLORS[color_name]
    line_width=random.randint(1,3)
    pygame.draw.rect(screen,color,[left,top,width,height],line_width) pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()


回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

7#
发表于 2018-6-16 04:31:15 |显示全部楼层
crossin先生你好,请教一个代码:

import sys,pygame
pygame.init()
screen=pygame.display.set_mode([640,480])
background=pygame.Surface(screen.get_size())
background.fill([255,255,255])
clock=pygame.time.Clock()
class Ball(pygame.sprite.Sprite):
    def __init__(self,image_file,speed,location):
        pygame.sprite.Sprite. __init__(self)
        self.image=pygame.image.load(image_file)
        self.rect=self.image.get_rect()
        self.rect.left,self.rect.top=location
        self.speed =speed
        def move(self):
            if self.rect.left<=screen.get_rect().left or \
                    self.rect.right>=screen.get_rect().right:
                self.speed[0]=-self.speed[0]

            newpos=self.rect.move(self.speed)
            self.rect=newpos

    my_ball = Ball('beach_ball.png',[10,0],[20,20])
    while True:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
        clock.tick(30)
        screen.blit(background,(0,0))
        my_ball.move()
        screen.blit(my_ball.image,my_ball.rect)
        pygame.display.flip()
-----------------------------------------
运行后显示

Traceback (most recent call last):
  File "/Users/Max/PycharmProjects/untitled1/hi.py", line 7, in <module>
    class Ball(pygame.sprite.Sprite):
  File "/Users/Max/PycharmProjects/untitled1/hi.py", line 22, in Ball
    my_ball = Ball('beach_ball.png',[10,0],[20,20])
NameError: name 'Ball' is not defined

第7行定义Ball(),为何第22行调用的时候又显示没定义呢?谢谢!
回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

8#
发表于 2018-6-16 20:28:38 |显示全部楼层
crossin先生 发表于 2018-6-16 15:34
缩进不对呀
22行后面的代码不应该在Ball的定义内部

27行以后是在while循环内部吗,还是会报错:
my_ball.move()
AttributeError: 'Ball' object has no attribute 'move'

谢谢
回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

9#
发表于 2018-6-18 09:58:25 |显示全部楼层
crossin先生你好,在pycharm安装project interpreter 时显示安装错误:

Could not find a version that satisfies the requirement PythonCard (from versions: )
No matching distribution found for PythonCard

在终端安装也显示同样的信息,py2和py3都不行,怎么破?谢谢
回复

使用道具 举报

0

主题

0

好友

64

积分

注册会员

Rank: 2

10#
发表于 2018-6-19 08:51:38 |显示全部楼层
crossin先生你好,帮忙看下这个储存list的代码:

import pickle
f = ['Hello','Bob',21]
pickle_file = open('pickle.txt','w')
pickle.dump(f,pickle_file)
f.close()
----------------------------
报错:
Traceback (most recent call last):
  File "/Users/Max/PycharmProjects/untitled1/hi.py", line 4, in <module>
    pickle.dump(f,pickle_file)
TypeError: write() argument must be str, not bytes

文档有创建,但里面没东西,这是什么问题?谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-18 21:10 , Processed in 0.027774 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部