-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvader.py
143 lines (117 loc) · 4.34 KB
/
Invader.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from tkinter import *
import RunningValues
from Collision import Collision
from InvaderMissile import InvaderMissile
from PlayerMissile import PlayerMissile
import Explosion
import random
import Sound
sound=0
sound_after=50
def invader_group_change():
global sound,sound_after
sound=sound+1
Invader.dont_move_down_clicks = Invader.dont_move_down_clicks + 1
if sound%sound_after == 0:
Sound.play()
sound_after = 25 + Invader.alive_invader_count
if sound_after < 15: sound_after=15
if Invader.next_direction != 0:
Invader.direction = Invader.next_direction
if Invader.next_down != 0:
Invader.move_down = Invader.next_down
Invader.next_down = 0
if Invader.move_down > 0:
Invader.move_down = Invader.move_down - 1
def setup(canvas):
Invader.move_down = 0
Invader.direction = -1
Invader.next_direction = 0
Invader.next_down = 0
Invader.dont_move_down_clicks = 0
Invader.speed = 1
Invader.ticker = 0
space_between_x = 80
space_between_y = 50
Invader.invader_count = 0
Invader.alive_invader_count = 0
for xx in range(1, 11):
for yy in range(1, 7):
RunningValues.render_list.append(
Invader(canvas, RunningValues.canvas_width - xx * space_between_x, yy * space_between_y))
Invader.invader_count = Invader.invader_count + 1
Invader.alive_invader_count = Invader.invader_count
def all_dead():
if Invader.alive_invader_count <= 0:
return True
return False
def down():
if Invader.dont_move_down_clicks >= 0:
Invader.next_down = 8
Invader.dont_move_down_clicks = -50
class Invader(Collision):
speed = 1
ticker = 0
invader_count = 0
alive_invader_count = 0
move_down: int = 0
direction: int = -1
next_direction: int = 0
next_down: int = 0
dont_move_down_clicks = 0
def __init__(self, canvas: Canvas, x, y):
self.width = 40
self.height = 30
self.canvas = canvas
self.x = x - self.width / 2
self.y = y - self.height / 2
self.img1 = canvas.create_image(self.x, self.y, image=RunningValues.invader_photo1)
self.img2 = canvas.create_image(self.x, self.y, image=RunningValues.invader_photo2)
def move(self, byx: int, byy: int):
self.speed = (Invader.invader_count*4) / (Invader.alive_invader_count + 7)
self.speed = self.speed + 2
byx = byx * self.speed
self.x = self.x + byx
byy = byy * self.speed * 2
self.y = self.y + byy
self.canvas.move(self.img1, byx, byy)
self.canvas.move(self.img2, byx, byy)
if self.ticker % 20 < 10:
self.canvas.itemconfig(self.img1, state=HIDDEN)
self.canvas.itemconfig(self.img2, state=NORMAL)
else:
self.canvas.itemconfig(self.img1, state=NORMAL)
self.canvas.itemconfig(self.img2, state=HIDDEN)
def render(self):
self.ticker = self.ticker + 1
if self.ticker % 5 < 4:
return
if Invader.move_down > 0:
self.move(0, 1)
else:
ran = random.randint(1, 100000)
if ran > 99958:
missile = InvaderMissile(self.canvas, self.x + self.width / 2, self.y + self.height)
RunningValues.render_list.append(missile)
if self.x <= self.width:
Invader.next_direction = 1
down()
if self.x >= RunningValues.canvas_width - self.width:
Invader.next_direction = -1
down()
self.move(Invader.direction, 0)
if self.y > RunningValues.canvas_height - self.height * 3:
RunningValues.game_over = True
def hit(self, other):
if isinstance(other, PlayerMissile):
Sound.invader()
Invader.alive_invader_count = Invader.alive_invader_count - 1
RunningValues.delete_list.append(self)
if self in RunningValues.render_list:
RunningValues.render_list.remove(self)
self.canvas.delete(self.img1)
self.canvas.delete(self.img2)
score = RunningValues.score_variable.get()
score = score + 1
RunningValues.score_variable.set(score)
Explosion.create_explosion(self.canvas, self.x + self.width / 2, self.y - self.height / 2)