-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
executable file
·116 lines (90 loc) · 3.65 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os, math
import pygame, Box2D
from Box2D import *
vec = b2Vec2
import params
import random
FPS = 60
def toScreen(v):
import game_state
return game_state.GameState.current.toScreen(v)
def rot_point(point, origin, angle):
sin_t = math.sin(math.radians(angle))
cos_t = math.cos(math.radians(angle))
return (origin[0] + (cos_t * (point[0] - origin[0]) - sin_t * (point[1] - origin[1])), origin[1] + (sin_t * ( point[0] - origin[0]) + cos_t * (point[1] - origin[1])) )
def rot_point_img(screen, img, point, origin, point_angle, twist_angle):
former_center = img.get_rect().center
pygame.draw.circle(screen, [128, 255, 255], origin, 2, 0)
new_center = rot_point(point, origin, point_angle)
rotated = img = pygame.transform.rotate(img, twist_angle)
#pygame.draw.circle(screen, [255, 0, 0], (int(point[0]), int(point[1])), 2, 0)
#pygame.draw.circle(screen, [0,255, 0], (int(origin[0]),int(origin[1])), 2, 0)
rot_rect = rotated.get_rect()
rot_rect.center = new_center
#screen.blit(rotated, rot_rect)
return rot_rect
def rot_point_img_rect(screen, img, origin, point, point_angle, twist_angle):
former_center = img.get_rect().center
new_p = rot_point(origin, point, point_angle)
rotated = pygame.transform.rotate(img, twist_angle)
rot_rect = rotated.get_rect()
rot_rect.center = new_p
#screen.blit(rotated, rot_rect)
#pygame.draw.circle(screen, [128, 255, 128], (int(point[0]), int(point[1])), 2, 0)
#pygame.draw.circle(screen, [0,255, 0], (int(origin[0]),int(origin[1])), 2, 0)
return rot_rect
#return rotated.get_rect()
def polar_vec(r, t):
return vec(r*math.cos(t), r*math.sin(t))
def rotate_img(img, angle):
rotate = pygame.transform.rotate
dup = img
img = rotate(dup, angle)
return img, img.get_rect()
def rot_center(image, angle):
former_center = image.get_rect().center
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect()
rot_rect.center = former_center
return rot_image
class Media():
media = None
def __init__(self):
#Load all media needed(images, animations, sounds, etc)
self.test = load_img('test.png')
dragon_scale = 0.25
self.dragon = {
'head': load_img('dragon/head.png', scale=dragon_scale) ,
'base': load_img('dragon/base.png', scale=dragon_scale) ,
'mid': load_img('dragon/mid.png', scale=dragon_scale) ,
'tail1': load_img('dragon/tail-1.png', scale=dragon_scale) ,
'tail2': load_img('dragon/tail-2.png', scale=dragon_scale) ,
'tail3': load_img('dragon/tail-3.png', scale=dragon_scale) ,
'torso': load_img('dragon/torso.png', scale=dragon_scale)
}
self.cannon = [load_img('cannon_back.png'), load_img('cannon.png'), load_img('cannon_front.png')]
self.vertcannon = load_img('upminicannon.png')
self.clod = load_img('clod.png')
self.home = load_img('home.png')
self.back = load_img('clouds.png')
self.snake = load_img('dragon.png')
self.music_seqs = []
for i in range(1, 22):
self.music_seqs.append(pygame.mixer.Sound(os.path.join('media/music', 'music_seq_{num}.ogg'.format(num=i))))
'''
Returns the image surface resource only
'''
def load_img(name, colorkey = None, scale = 1.0):
fullname = os.path.join('media', name)
image = pygame.image.load(fullname)
# To speed things up, convert the images to SDL's internal format
image = image.convert_alpha()
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
if scale!=1.0:
image = pygame.transform.scale(image, (int(image.get_width()*scale), int(image.get_height()*scale)))
return image
# KEEP DOWN HERE
from physics import world