Skip to content

Commit

Permalink
try some simple tune ups
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardjason committed Sep 9, 2019
1 parent 0ad50fa commit 7c4ff04
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 24 deletions.
7 changes: 5 additions & 2 deletions Explosion.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ def __init__(self, canvas: Canvas, x, y):
fill=colours[random.randrange(0, 3)])

def render(self):
self.canvas.move(self.shape, self.addx, self.addy)
self.ticker = self.ticker + 1
if self.ticker > 75:
if self.ticker % 8 < 5:
return
self.canvas.move(self.shape, self.addx, self.addy)
if self.ticker > 50:
if self in RunningValues.render_list:
RunningValues.render_list.remove(self)
self.canvas.delete(self.shape)
RunningValues.delete_list.append(self)
return

def hit(self, other):
Expand Down
20 changes: 17 additions & 3 deletions Game.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

master = Tk()

master.attributes("-fullscreen", True)

RunningValues.invader_photo1 = PhotoImage(file="invader.gif")
RunningValues.invader_photo2 = PhotoImage(file="invader2.gif")

master.attributes("-fullscreen", True)
RunningValues.canvas_width = master.winfo_screenwidth()
RunningValues.canvas_height = master.winfo_screenheight()

Expand Down Expand Up @@ -56,9 +56,20 @@ def set_bindings():
master.bind("<KeyRelease-%s>" % char, released)


tps_ticker=0
tps_last_ticker=0
time_taken=0
tps_last=0
def render():
global canvas
global canvas,tps_ticker,tps_last,tps_last_ticker,time_taken
xx= RunningValues.unix_time_millis()
now = RunningValues.unix_time_millis()
tps_ticker=tps_ticker+1
if now-tps_last > 1000:
print(time_taken / (tps_ticker - tps_last_ticker),(tps_ticker - tps_last_ticker))
tps_last_ticker = tps_ticker
time_taken = 0
tps_last=now

if RunningValues.is_create_player:
RunningValues.create_player(canvas)
Expand All @@ -80,12 +91,13 @@ def render():
for f in RunningValues.render_list:
f.render()

for f in RunningValues.render_list:
for f in RunningValues.collision_list:
for o in RunningValues.render_list:
if o != f and f.collide(o):
f.hit(o)
o.hit(f)


for d in RunningValues.delete_list:
del d
RunningValues.delete_list.clear()
Expand Down Expand Up @@ -113,6 +125,7 @@ def render():
start_game()
master.after(int(10 - diff), render)

time_taken = time_taken + RunningValues.unix_time_millis() - xx

def start_game(score=0, lives=3):
global canvas, pressedStatus
Expand All @@ -121,6 +134,7 @@ def start_game(score=0, lives=3):
for f in RunningValues.render_list:
del f
RunningValues.render_list.clear()
RunningValues.collision_list.clear()

RunningValues.game_over = False
Invader.setup(canvas)
Expand Down
5 changes: 4 additions & 1 deletion Invader.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, canvas: Canvas, x, y):

def move(self, byx: int, byy: int):
self.speed = Invader.invader_count / (Invader.alive_invader_count + 1) / 3
self.speed = self.speed + 1
self.speed = self.speed + 3
byx = byx * self.speed
self.x = self.x + byx
byy = byy * self.speed * 2
Expand All @@ -105,6 +105,9 @@ def move(self, byx: int, byy: int):

def render(self):
self.ticker = self.ticker + 1
if self.ticker % 5 < 4:
return

if Invader.move_down > 0:
self.move(0, 1)
else:
Expand Down
9 changes: 8 additions & 1 deletion InvaderMissile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

class InvaderMissile(Collision):

speed=2
speed=3
def __init__(self, canvas: Canvas, x, y):

RunningValues.collision_list.append(self)
self.height = 20
self.width = 10
self.canvas = canvas
self.ticker=0

self.x = x - self.width/2
self.y = y - self.height/2
Expand All @@ -30,6 +32,9 @@ def move(self, by: int):


def render(self):
self.ticker=self.ticker+1
if self.ticker%8 < 3 :
return
self.move(1)

def hit(self,other):
Expand All @@ -38,3 +43,5 @@ def hit(self,other):
RunningValues.delete_list.append(self)
RunningValues.render_list.remove(self)
self.canvas.delete(self.shape)
RunningValues.collision_list.remove(self)
RunningValues.delete_list.append(self)
12 changes: 10 additions & 2 deletions PlayerMissile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@


class PlayerMissile(Collision):
speed = 2
speed = 4

def __init__(self, canvas: Canvas, x, y):

RunningValues.collision_list.append(self)
self.height = 10
self.width = 10
self.canvas = canvas
self.ticker =0

self.x = x - self.width / 2
self.y = y - self.height / 2
Expand All @@ -27,10 +29,16 @@ def move(self, by: int):
RunningValues.delete_list.append(self)

def render(self):
self.ticker = self.ticker +1
if self.ticker % 8 < 3:
return
self.move(-1)

def hit(self, other):
if not isinstance(other, PlayerMissile):
RunningValues.delete_list.append(self)
RunningValues.render_list.remove(self)
if self in RunningValues.render_list:
RunningValues.render_list.remove(self)
self.canvas.delete(self.shape)
RunningValues.collision_list.remove(self)
RunningValues.delete_list.append(self)
2 changes: 1 addition & 1 deletion PlayerTurret.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def fire(self):
if now > self.time_since_last_fire:
RunningValues.render_list.append(
PlayerMissile(self.canvas, self.x + self.width / 2, self.y - self.height / 2))
self.time_since_last_fire = now + 300
self.time_since_last_fire = now + 500

def hit(self, other):
if str(type(other)).__contains__("Invader"):
Expand Down
1 change: 1 addition & 0 deletions RunningValues.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from PlayerTurret import PlayerTurret

render_list = list()
collision_list = list()
canvas_width = 1000
canvas_height = 800

Expand Down
39 changes: 25 additions & 14 deletions Sound.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import simpleaudio as sa
try:

filename = '/src/python/invaders/invader.wav'
background = sa.WaveObject.from_wave_file('invader.wav')
invader_hit = sa.WaveObject.from_wave_file('invaderhit.wav')
player_hit = sa.WaveObject.from_wave_file('playerhit.wav')
import simpleaudio as sa

background = sa.WaveObject.from_wave_file('invader.wav')
invader_hit = sa.WaveObject.from_wave_file('invaderhit.wav')
player_hit = sa.WaveObject.from_wave_file('playerhit.wav')

def play():
global background
background.play()

def play():
global background
background.play()

def invader():
global invader_hit
invader_hit.play()

def invader():
global invader_hit
invader_hit.play()

def hit():
global player_hit
player_hit.play()

def hit():
global player_hit
player_hit.play()

except ImportError as e:
def play():
return

def invader():
return

def hit():
return

0 comments on commit 7c4ff04

Please sign in to comment.