-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSprites2D.py
More file actions
94 lines (77 loc) · 2.97 KB
/
Sprites2D.py
File metadata and controls
94 lines (77 loc) · 2.97 KB
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
import pygame
import sys
import random
from CONSTANTS import *
class Object_2D(pygame.sprite.Sprite):
def __init__(self, spawnPoint, size, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface(size)
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = spawnPoint[0]
self.rect.y = spawnPoint[1]
self.spawnPoint = spawnPoint
def load_image(self, imagePath):
self.image = pygame.image.load(imagePath).convert_alpha()
#return self.image
class Item(Object_2D):
def __init__(self, spawnPoint, size, color):
Object_2D.__init__(self, spawnPoint, size, color)
class Bullet(Object_2D):
def __init__(self, spawnPoint, direction, size, color):
Object_2D.__init__(self, spawnPoint, size, color)
self.speed = 45
self.direction = direction
self.steigung = (self.direction[1]-self.spawnPoint[1])/(self.direction[0]-self.spawnPoint[0])
print(self.steigung)
self.yS = self.steigung*self.spawnPoint[0]-self.spawnPoint[1]
def update(self):
self.rect.x += self.speed
self.rect.y = self.rect.x*self.steigung-self.yS
if self.rect.x > SCREEN_WIDTH:
self.kill()
class Enemy(Object_2D):
def __init__(self, spawnPoint, size, color):
Object_2D.__init__(self, spawnPoint, size, color)
self.speed = random.randrange(1, 4)
self.health = 3
def update(self):
self.rect.x -= self.speed
if self.rect.x < -128:
self.kill()
if self.health == 0:
die_sound = pygame.mixer.Sound('die.wav')
die_sound.play()
self.kill()
def collide(self, collision_group):
for sprite in collision_group:
if self.rect.colliderect(sprite.rect):
sprite.kill()
prehit_sound = pygame.mixer.Sound('prehit.wav')
prehit_sound.play()
self.health -= 1
class Player(Object_2D):
def __init__(self, spawnPoint, size, color):
Object_2D.__init__(self, spawnPoint, size, color)
self.speed = 5
self.health = 5
self.x_direction = 0
self.y_direction = 0
self.inventory = {'Bullets' : 120}
def collide(self, collision_group):
for sprite in collision_group:
if self.rect.colliderect(sprite.rect):
die_sound = pygame.mixer.Sound('dead.wav')
die_sound.play()
def update(self):
self.rect.x += self.speed * self.x_direction
self.rect.y += self.speed * self.y_direction
if self.rect.x <= 0:
self.rect.x = 1
self.x_direction = 0
if self.rect.y <= 0:
self.rect.y = 1
self.y_dircetion = 0
if self.rect.y >= SCREEN_HEIGHT-128:
self.rect.y = SCREEN_HEIGHT-129
self.y_direction = 0