0

I have a problem with python programming, when I'm trying to write a game, it says: No module named 'pgzrun',but I had already install pyzero.On windows,it works perfectly,but on my macbook,it could not work.I has serched it for nearly two hours.Maybe you can give me some help. here is my code:

import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(100, 100)
import pgzrun
import pygame
pygame.init()
pygame.display.set_caption("rick astleys bizarre adventure")
import random
import time
WIDTH = 500
HEIGHT = 700
#创建
player = Actor('小核桃')
player.x = 250
player.y = 600
music.play('2')
act=Actor('world')
blood=Actor('blood')
money=0
cho=0
kill=0
cg=0
gc=0
cga=0
pd=0
zidan=40
moneybag=Actor('money')
font1 = pygame.font.SysFont('宋体', 32)
font2=pygame.font.SysFont('微软雅黑',32)
weapons = []
#创建3个bubu兽角色
bubus = []
for i in range(3):
    b = Actor('bubu')
    b.x = random.randint(0, 500)
    bubus.append(b)
theworld=pygame.mixer.Sound('5.wav')
hengheng=pygame.mixer.Sound('3.wav')
bzsc=pygame.mixer.Sound('bzsc.wav')
high=pygame.mixer.Sound('high.wav')
cant=pygame.mixer.Sound('cant.wav')
def draw():
    global cho
    global key
    global score
    global money
    global cg
    global gc
    global cga
    global pd
    global zidan
    global weapon
    surface1=font1.render(str(money),True,[255,255,255])
    surface2=font1.render('do u want win 1 or 2',True,[255,255,255])
    s3=font2.render('human nature',True,[255,255,255],True)
    s4=font1.render('score:'+str(score),True,[255,255,255])
    s5=font1.render('if you already died press r to restart',True,[255,255,255])
    screen.blit('星球', [0, 0])
    screen.blit(surface1, [100, 50])
    moneybag.draw()
    moneybag.x=50
    moneybag.y=50
    screen.blit(s4,[100, 100])
    for b in bubus:
        for w in weapons:
            if b.colliderect(w):
                blood.draw()
                blood.x=b.x
                blood.y=b.y
                blood.x=blood.x
                blood.y=blood.y
    if cg==1:
        screen.blit(surface2, [250, 50])
        if gc==1:
            screen.blit(s3,[250, 100])
            player.image='小核桃失败'
            music.play('1')
        elif cga==1:
            music.play('rickg.wav')
            cant.set_volume(20)
            cant.play()
            score+=1000
            cg==0
            cga=0
            kill=0
        if score>=1200:
            high.play()
    screen.blit(s5,[100,350])
    if player.image != '小核桃失败':
        for i in bubus:
            if i.image=='迫害':
                act.draw()
                hengheng.stop()
                music.pause()
                music.unpause()
    player.draw()
    #游戏失败之前绘制角色
    if player.image != '小核桃失败':
        for w in weapons:
            w.draw()
            for b in bubus:
                if b.image!='迫害':
                    w.image='agun'
        for b in bubus:
            b.draw()
score=0
def update():
    global zidan
    global money
    global weapon
    if player.image != '小核桃失败':
        if player.image!='attack':
    #小核桃移动
            if keyboard.left:
                player.x -= 5
            if keyboard.right:
                player.x += 5
            if keyboard.up:
                player.y -= 5
            if keyboard.down:
                player.y += 5
        else:
            if keyboard.left:
                player.x -= 2
            if keyboard.right:
                player.x += 2
            if keyboard.up:
                player.y -= 2
            if keyboard.down:
                player.y += 5
    #空格键发射子弹
        if keyboard.space:
            sanshe=random.randint(-20,20)
            weapon = Actor('子弹')
            if player.image=='attack.png':
                weapon.x = player.x+sanshe
            else:
                weapon.x=player.x+random.randint(-40,40)
            weapon.y = player.y - 50
            weapons.append(weapon)
            if player.image!='attack.png':
                if zidan>0:
                #反作用力,火力的代价
                    player.y+=5
            for i in bubus:
                if i.image=='迫害':
                    player.image='attack.png'
                else:
                    player.image='attack2.png'
        else:
            player.image='小核桃.png'
    #the world!
        if player.image!='w':
            if keyboard.z:
                global b
                for b in bubus:
                    b.image='迫害'
                    b.y += 0
                for w in weapons:
                    w.y -=20
                for w in weapons:
                    if w.y <player.y-200:
                        weapons.remove(w)
            else:
                for a in bubus:
                    a.image='bubu'
                    a.y += 5
                    if a.y > 800:
                        a.x = random.randint(0, 500)
                        a.y = -100
                    for w in weapons:
                        w.y -= 10
                    for w in weapons:
                        if w.y <= 0:
                         weapons.remove(w)
    # bubu兽从上向下移动, 到窗口下边后初始化到窗口上方
            if player.y>700:
                if money<200:
                    player.image='小核桃失败'
                    player.y-=100
                else:
                    money-=200
                    bzsc.play()
                    player.y-=100
            if player.x>500:
                player.x-=50
            if player.x<0:
                player.x+=50
    #bubu兽和子弹碰撞后, 回到初始位置
        for b in bubus:
            for w in weapons:
                if b.colliderect(w):
                    weapons.remove(w)
                    money+=10
                    global kill
                    kill+=1
                    b.x = random.randint(0, 500)
                    b.y = -100
                    music.pause
                    hengheng.set_volume(0.2)
                    if b.image!='迫害':
                        hengheng.play()
                    music.unpause()
                    #增加分数
                    global score
                    score+=1
            #bubu碰到小核桃游戏失败
            if b.colliderect(player):
                if player.image!='w':
                    if money<200:
                        player.image = '小核桃失败'
                    else:
                        money-=200
                        for b in bubus:
                            b.x = random.randint(0, 500)
                            b.y = 0
                            #第三炸弹 败者食尘
                            bzsc.play()
            if player.image=='小核桃失败':
                music.play('1')
            #白旗
            if keyboard.c:
                player.image='w'
#按下x切换背景音乐(随机)nm做不出来顺序切换,看你自己运气
def on_key_down(key):
    global cho
    global score
    global kill
    global cg
    global gc
    global cga
    global money
    global zidan
    global weapon
    if player.image!='小核桃失败':
        if key==keys.X:
            music.play(random.choice(['2','4']))
        if key==keys.Z:
            music.play('4')
            theworld.play()
        if kill>=100:
            cg=1
            if key==keys.K_1:
                gc=1
            elif key==keys.K_2:
                cga=1
    if player.image=='小核桃失败':
        if money<200:
            cho=1
            if key==keys.R:
                kill=0
                score=0
                money=0
                player.image='小核桃'
                music.play('2')
                player.x=250
                player.y=600
                for b in bubus:
                    b.x = random.randint(0, 500)
                    b.y = -100
def on_key_up(key):
    if key==keys.Z:
        music.play('2')
    if player.image!='小核桃失败':
        if key==keys.C:
            music.play('2')
pgzrun.go()

My project: Baidu cloud disk

2 Answers2

0

Question, do you have both pygame and pgzero installed on your OS device? If not, I would prefer you do it since the "pgzrun" module is meant to pair with pgzero & pygame. To download it, open your terminal or shell or whatever you use, then do this command,

python -m pip install pgzero
python -m pip install pygame 

Dont do the pygame command if you haven't already.Once you have done that, run your .py file again. But if you want it to look professional or want it organised i prefer you delete the "import pygame" or "import pgzero" unless your not using the original python IDE. It would still run without the imports. But that only works for those 2. Dont just delete all the imports like PIL or sleep or time and such. But i think that only works for windows. So add the imports. also if you put "pip install pyzero" in the Terminal you may want to correct that. not to be rude or anything.

0

Although the different situation, but I've got the same error and found the solution. On Windows, the older mu editor like 1.0.2 makes /bin/ under user home and there is pgzrun.exe when using pygame zero.

The /bin/ was removed in uninstalling the older mu-editor then I don't see the error anymore.

Naohiro Tsuji
  • 106
  • 1
  • 7