Reputation: 1
I've search for a few days but nothing worked, each time I try to run my code I get this error:
AttributeError: 'module' object has no attribute 'sin'
I've tried everything but hopefully someone will find how to fix it
My code:
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)
The end result of my game is a paddle on the lower level of the screen and squares falling down that the player has to dodge, what I'm trying to do is make the square's amount of sides increase to a certain amount but every time I get the error stated above.
Thanks in advance
Upvotes: 0
Views: 2407
Reputation: 34205
You're first importing math
, then everything from pygame
. This results in importing pygame.math
as math
.
The result is that you're importing a module which does not have a sin
function. That's also one of the reasons python zen says:
Explicit is better than implicit
Since you already imported pygame
on its own, you can qualify its members (which you seem to be doing anyway) and remove the from ... import *
lines. Alternatively you could import only the things you actually want to use, like you do for from pygame import mixer
.
from ... import *
can bite you unexpectedly and it's better not to use it if you don't have to.
Upvotes: 1