AttributeError:'module'对象没有属性'sin'

时间:2017-04-18 02:19:09

标签: python

我已经搜索了几天但没有任何效果,每次我尝试运行我的代码时都会收到此错误:

AttributeError: 'module' object has no attribute 'sin'

我已尝试过所有内容,但希望有人会找到解决方法

我的代码:

import pygame, sys, time
import math

from math import sin

from random import *

from time import *

from pygame import *

from pygame import mixer

pygame.init()

Blur = False

# Dead Or No Dead

not_dead = True

# Default Life Up Score

life_up = 100

# Heart PNG

heart_image = pygame.image.load('/home/pi/Desktop/heart_image.bmp')

# Font

myfont = pygame.font.SysFont("robotocondensed", 40)

# Pop Up Heart Variables

pop_up1 = True

pop_up2 = True

pop_up3 = True

pop_up4 = True

pop_up5 = True

pop_up6 = True

pop_up7 = True

pop_up8 = True

pop_up9 = True

pop_up10 = True

# Start Score

Score = False

score = 0

# Boulder Size Along With Max Boulder Size

B_size = 30

max_B_size = 59

# Start Life

life = 5

# Default max / min Boulder Speed

max_B_speed = 29

min_B_speed = 10

# Create Window

window_width = 1000

window_length = 700

screen = pygame.display.set_mode(((window_width), (window_length)))

# Toggle Damage (Use Invincibility For No Damage)

yes = False

# Toggle Invincibility

Invincibility = False

# Color Variables

RED = (255, 0, 0)
DARK_RED = (51, 0, 0)
GREEN = (0, 255, 0)
BLUE= (0, 0, 255)
WHITE = (255, 255, 255)
BROWN = (101, 67, 33)
LIGHT_BROWN = (98, 64, 30)

# Boulder's(Blur) Color

B_Color = BROWN

BLUR_Color = LIGHT_BROWN

# Player's Color

playerColor = (WHITE)

# Variable for -30

z = -30

x = 1

# Blur Options

s = pygame.Surface(((B_size),(B_size)))

s.set_alpha(40)

s.fill(((RED)))

# Power Up 

toggle = True

power_up = True

level = 2

paused = False

direction = 4

## CLASSES


def polygon(sides, radius=1, rotation=0, translation=None):
    one_segment = 3.14 * 2 / sides

    points = [
        (math.sin(one_segment * i + rotation) * radius,
         math.cos(one_segment * i + rotation) * radius)
        for i in range(sides)]

    if translation:
        points = [[sum(pair) for pair in zip(point, translation)]
                  for point in points]

    return points


# Player Class

class Player (pygame.sprite.Sprite):
    def __init__(self, pos):
        pygame.sprite.Sprite.__init__(self)

        self.x = (window_width / 2)
        self.y = 670
        self.width = 30
        self.height = 5
        self.rect = pygame.Rect(pos[0], pos[1], 30, 5)

        self.rect.move_ip(0,self.y)
        self.rect.move_ip(29,0)


    def handle_keys(self):

        key = pygame.key.get_pressed()
        dist = 1
        speed = 10

        print(self.x)


        if key[pygame.K_LEFT]:
            self.rect.move_ip(speed *-1,0)
            self.x = self.x - 1

        if key[pygame.K_RIGHT]:
            self.rect.move_ip(speed,0)
            self.x += 1
    def draw(self, surface):

        pygame.draw.rect(screen, (playerColor), self.rect)


    def window_border(self):
        speed = 10
        if (self.x == 560):
            self.rect.move_ip(speed * -1,0)
            self.x = (self.x - 1)

        elif (self.x == 497):
            self.rect.move_ip(speed,0)
            self.x = (self.x + 1)



# 1st Boulder Class

class Boulder(object):

    def __init__(self):
        self.x = -70 #randint(56, 124)
        self.rect = pygame.rect.Rect((self.x, 0, (B_size), 1))
        self.blur = pygame.rect.Rect((self.x, 0 + z, (B_size), (B_size)))
        self.y = -30
        self.speed = 16 #randint((min_B_speed), (max_B_speed))
        self.size = (B_size)
        self.direction = 4

    def draw(self):

        if(level == 2):

            pygame.draw.polygon(screen, BROWN,(polygon(7,15, self.direction,[self.x, self.y])) , 0)

            self.direction += .005

    def updater(self):

        self.rect = pygame.rect.Rect((self.x, self.y, (B_size), (B_size)))

        #self.polygon = pygame.polygon.Polygon((self.x, 0, (B_size), (B_size)))



    def move(self, speed):

        self.y += (self.speed)

    def back_to_top(self):

        if (self.y >= 770):
            self.y = -30 
            self.x = randint(0, 660)
            self.speed = randint((min_B_speed), (max_B_speed))


# 2nd Boulder Class

class Boulder_2(object):

    def __init__(self):
        self.size = 30
        self.x = -70 
        self.rect = pygame.rect.Rect((self.x, 0, (self.size), 5))
        self.y = -30
        self.speed = randint((min_B_speed), (max_B_speed))
        print("5")

    def draw(self):

        pygame.draw.rect(screen, (B_Color), self.rect)

        if (self.speed >= 20):

            screen.blit(s, (self.x, self.y + z))

    def updater(self):
        self.rect = pygame.rect.Rect((self.x, self.y, (B_size), (B_size)))         

    def move(self, speed):

        self.y += (self.speed)

    def back_to_top(self):

        if (self.y >= 770):
            self.y = -30
            self.x = randint(0, 660)
            self.speed = randint((min_B_speed), (max_B_speed))


# 3nd Boulder Class

class Boulder_3(object):

    def __init__(self):
        self.x = -70
        self.rect = pygame.rect.Rect((self.x, 0, 30, 30))
        self.y = -30
        self.speed = randint((min_B_speed), (max_B_speed))


    def draw(self):

        pygame.draw.rect(screen, (B_Color), self.rect)

        if (self.speed >= 20):

            screen.blit(s, (self.x, self.y + z))

    def updater(self):



        self.rect = pygame.rect.Rect((self.x, self.y, (B_size), (B_size)))

    def move(self, speed):

        self.y += (self.speed)

    def back_to_top(self):

        if (self.y >= 770):
            self.y = -30
            self.x = randint(0, 660)
            self.speed = randint((min_B_speed), (max_B_speed))

# 4th Boulder Class 

class Boulder_4(object):

    def __init__(self):
        self.x = -70 
        self.rect = pygame.rect.Rect((self.x, 0, 30, 30))
        self.y = -30
        self.speed = randint((min_B_speed), (max_B_speed))


    def draw(self):

        pygame.draw.rect(screen, (B_Color), self.rect)

        if (self.speed >= 20):

            screen.blit(s, (self.x, self.y + z))

    def updater(self):

        self.rect = pygame.rect.Rect((self.x, self.y, (B_size), (B_size)))

    def move(self, speed):

        self.y += (self.speed)

    def back_to_top(self):

        if (self.y >= 770):
            self.y = -30
            self.x = randint(0, 660)
            self.speed = randint((min_B_speed), (max_B_speed))

class Boulder_5(object):

    def __init__(self):
        self.x = -70
        self.rect = pygame.rect.Rect((self.x, 0, (B_size), (B_size)))
        self.blur = pygame.rect.Rect((self.x, 0 + z, (B_size), (B_size)))
        self.y = -30
        self.speed = 16 #randint((min_B_speed), (max_B_speed))
        self.size = (B_size)

    def draw(self):

        pygame.draw.rect(screen, (BROWN), self.rect)

    def updater(self):

        self.rect = pygame.rect.Rect((self.x, self.y, (B_size), (B_size)))

        if (self.speed >= 20):

            screen.blit(s, (self.x, self.y + z))

    def move(self, speed):

        self.y += (self.speed)

    def back_to_top(self):

        if (self.y >= 770):
            self.y = -30 
            self.x = randint(0, 660)
            self.speed = randint((min_B_speed), (max_B_speed))


# Floor Class

class Floor(object):

    def __init__(self):

        self.x = 0
        self.rect = pygame.rect.Rect((self.x, 0, 1000, 1))
        self.y = 750

# Border Class

class Big_Blue_Line(object):

    def __init__(self):

        self.x = 700

        self.rect = pygame.rect.Rect((self.x, 0, 2, 1000))

    def draw(self):

        pygame.draw.rect(screen, (BROWN), (self.rect))


class Power_Up(object):

    def __init__(self):

        self.timer = 0

        self.x = -50

        self.y = 0

        self.rect = pygame.rect.Rect((self.x, 0, 30, 30))

    def draw(self):

        screen.blit(heart_image,(self.x, self.y))

    def location(self):

        self.rect = pygame.rect.Rect((self.x, self.y, 20, 20))



        if (power_up == True):

            self.y += 10

            if (self.y >= 770):

                self.timer += 1

                self.y = -30

                print(self.timer)

                self.x = randint(-50, -49)

            if (self.timer >= 15):

                self.x = randint(30, 650)

                self.timer = 0





# Clock Variable

clock = pygame.time.Clock()

# Calling Instances

power = Power_Up()

player = Player([20, 20])

boulder = Boulder()

boulder2 = Boulder_2()

b3 = Boulder_3()

b4 = Boulder_4()

b5 = Boulder_5()

Floor = Floor()

Border = Big_Blue_Line()

#Main Game Loop

while True:

    window_width = window_width

    window_length = window_length

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.flip()


    screen.fill((0, 0, 0))

    if (paused == False):

        if (not_dead == False):

            key = pygame.key.get_pressed()

            GameOvertext = myfont.render("Game Over", 1, (255,255,255))

            screen.blit(GameOvertext, (430, 350))

            screen.blit(scoretext, (786, 60))

            Retrytext1 = myfont.render("RETRY?", 1, (255,255,255))

            screen.blit(Retrytext1, (450, 400))

            Retrytext2 = myfont.render("Y     N", 1, (255,255,255))

            screen.blit(Retrytext2, (460, 450))

            if key[pygame.K_y]:

                not_dead = True

                B_size = 30

                score = 0

                life += 5

                max_B_speed = 29

                min_B_speed = 10

            if key[pygame.K_n]:

                pygame.quit()

                sys.exit()

        key = pygame.key.get_pressed()

        if key[pygame.K_ESCAPE]:

            if (paused == False):


                paused = True

            else:

                paused = False


    if (not_dead == True) or (paused == False):


    ## HEALTH UP

        if (life <= 9):

            if score == (life_up):
                # Life Increase
                    life += 1

    ## Max / Min Boulder Speed Increase

        if score == (life_up):

            if (max_B_speed <= 49):

                max_B_speed += 2

                if (min_B_speed <= 46):

                    min_B_speed += 2


    ## Boulder Size Increase

        if score == (life_up):

            if (B_size <= (max_B_size)):

                B_size += 5

            life_up = (life_up) + 100

    ## COLLISION

        if (life <= 10):

            if pygame.sprite.collide_rect(player, power):
                life += .5



        if (Invincibility == False):

            if yes == True:

                if pygame.sprite.collide_rect(player, boulder):
                    life -= .5

                    print("HIT!")

                    yes = False

                if pygame.sprite.collide_rect(player, boulder2):
                    life -= .5

                    yes = False


                if pygame.sprite.collide_rect(player, b3):
                    life -= .5

                    yes = False


                if pygame.sprite.collide_rect(player, b4):
                    life -= .5

                    yes = False

                if pygame.sprite.collide_rect(player, b5):
                    life -= .5

                    yes = False

            else:
                clock.tick(9999999)
                yes = True

    ## DEATH

        if (life < 1):

            screen.fill((0, 0, 0))

            GameOvertext = myfont.render("Game Over", 1, (255,255,255))

            not_dead = False

    ## SCORE TRACKER

        if pygame.sprite.collide_rect(b5, Floor):
            score += 1


        if pygame.sprite.collide_rect(b4, Floor):
            score += 1


        if pygame.sprite.collide_rect(b3, Floor):
            score += 1


        if pygame.sprite.collide_rect(boulder2, Floor):
            score += 1


        if pygame.sprite.collide_rect(boulder, Floor):
            score += 1


    ## CLEAR SCREEN    

        screen.fill((0, 0, 0))


    ## HEATH BAR

        if (life >= 10):

            screen.blit(heart_image,(950,10))
            screen.blit(heart_image,(925,10))
            screen.blit(heart_image,(900,10))
            screen.blit(heart_image,(875,10))
            screen.blit(heart_image,(850,10))
            screen.blit(heart_image,(825,10))
            screen.blit(heart_image,(800,10))
            screen.blit(heart_image,(775,10))
            screen.blit(heart_image,(750,10))
            screen.blit(heart_image,(725,10)) 

        if (life >= 9):

            screen.blit(heart_image,(950,10))
            screen.blit(heart_image,(925,10))
            screen.blit(heart_image,(900,10))
            screen.blit(heart_image,(875,10))
            screen.blit(heart_image,(850,10))
            screen.blit(heart_image,(825,10))
            screen.blit(heart_image,(800,10))
            screen.blit(heart_image,(775,10))
            screen.blit(heart_image,(750,10))

        if (life >= 8):

            screen.blit(heart_image,(950,10))
            screen.blit(heart_image,(925,10))
            screen.blit(heart_image,(900,10))
            screen.blit(heart_image,(875,10))
            screen.blit(heart_image,(850,10))
            screen.blit(heart_image,(825,10))
            screen.blit(heart_image,(800,10))
            screen.blit(heart_image,(775,10))          

        if (life >= 7):

            screen.blit(heart_image,(950,10))
            screen.blit(heart_image,(925,10))
            screen.blit(heart_image,(900,10))
            screen.blit(heart_image,(875,10))
            screen.blit(heart_image,(850,10))
            screen.blit(heart_image,(825,10))
            screen.blit(heart_image,(800,10))

        if (life >= 6):

            screen.blit(heart_image,(950,10))
            screen.blit(heart_image,(925,10))
            screen.blit(heart_image,(900,10))
            screen.blit(heart_image,(875,10))
            screen.blit(heart_image,(850,10))
            screen.blit(heart_image,(825,10))

        if (life >= 5):

            screen.blit(heart_image,(950,10))
            screen.blit(heart_image,(925,10))
            screen.blit(heart_image,(900,10))
            screen.blit(heart_image,(875,10))
            screen.blit(heart_image,(850,10))

        if (life >= 4):

            screen.blit(heart_image,(950,10))
            screen.blit(heart_image,(925,10))
            screen.blit(heart_image,(900,10))
            screen.blit(heart_image,(875,10))

        if (life >= 3):

            screen.blit(heart_image,(950,10))
            screen.blit(heart_image,(925,10))
            screen.blit(heart_image,(900,10))

        if (life >= 2):

            screen.blit(heart_image,(950,10))
            screen.blit(heart_image,(925,10))

        if (life >= 1):

            screen.blit(heart_image,(950,10))


    ## SCORE DISPLAY

        scoretext = myfont.render("Score {0}".format (score), 1, (255,255,255))

        screen.blit(scoretext, (786, 60))


    #INSTANCE FUCTIONS

        # Boulder Functions

        power.draw()

        power.location()

        boulder.updater() 

        boulder2.updater()

        b3.updater()

        b4.updater()

        b5.updater()

        boulder.draw()

        boulder2.draw()

        Border.draw()

        b3.draw()

        b4.draw()

        b5.draw()

        boulder.back_to_top()

        boulder2.back_to_top()

        b3.back_to_top()

        b4.back_to_top()

        b5.back_to_top()

        boulder.move(surface)

        boulder2.move(surface)

        b3.move(surface)

        b4.move(surface)

        b5.move(surface)

        # Player Functions

        player.draw(surface)

        player.handle_keys()

        player.window_border()

        pygame.display.update()

        player.update()

        s = pygame.Surface(((B_size),(B_size)))

        s.set_alpha(40)

        s.fill(((RED)))

        clock.tick(40)

我的游戏的最终结果是在屏幕的下层有一个划桨,并且正方形下降,玩家必须躲闪,我正在尝试做的是让正方形的边数量增加到一定量但是每次我收到上述错误。

提前致谢

1 个答案:

答案 0 :(得分:1)

您首先导入math,然后导入pygame的所有内容。这导致将pygame.math导入为math

结果是您导入的a module没有sin功能。这也是python zen所说的原因之一:

  

显式优于隐式

由于您已经自行导入了pygame,因此您可以限定其成员(您似乎正在执行此操作)并删除from ... import *行。或者,您可以只导入您真正想要使用的内容,就像您对from pygame import mixer所做的那样。

from ... import *可能会意外地咬你,如果你不需要,最好不要使用它。