-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisions.py
More file actions
145 lines (138 loc) · 5.91 KB
/
Copy pathCollisions.py
File metadata and controls
145 lines (138 loc) · 5.91 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
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
144
145
import pyglet
from pyglet.gl import *
from pyglet.window import mouse
import time
#Windows
background_color = (0, .5, .3, 1)
window_w=1280
window_h=720
window=pyglet.window.Window(width=window_w,height=window_h)
gl.glClearColor(*background_color)
#Tank Image
tankImage=pyglet.resource.image("Tank.png")
tankImage.width=150
tankImage.height=150
tank_sprite=pyglet.sprite.Sprite(tankImage,x=(window_w/2)-(tankImage.width/2),y=(window_h/2)-(tankImage.height/2))
tankImage.anchor_x=tankImage.width/2
tankImage.anchor_y=tankImage.height/2
#Tank2 Image
tank_sprite2=pyglet.sprite.Sprite(tankImage,x=((tankImage.width/2)+100),y=((tankImage.height/2)+100))
tank_sprite2.visible=True
tank_sprite2.rotation=270
#Bullet Image
bulletImage=pyglet.resource.image("Bullet.png")
bulletImage.width=30
bulletImage.height=30
bullet_sprite=pyglet.sprite.Sprite(bulletImage,x=(window_w*3/4),y=(window_h/2))
bullet_sprite.visible=False
bulletImage.anchor_x=bulletImage.width/2
bulletImage.anchor_y=bulletImage.height/2
'''#Bullet2 Image
bullet_sprite2=pyglet.sprite.Sprite(bulletImage,x=(window_w*3/4),y=(window_h/2))
bullet_sprite2.visible=True'''
#IMAGE LIST
#ammo=[bullet_sprite,bullet_sprite2]
#Explode Image
explodeImage=pyglet.resource.image("Explode.png")
explodeImage.width=300
explodeImage.height=300
explode_sprite=pyglet.sprite.Sprite(explodeImage,x=(window_w/4),y=(window_h/2))
explode_sprite.visible=False
explodeImage.anchor_x=explodeImage.width/2
explodeImage.anchor_y=explodeImage.height/2
#Draw function
@window.event
def on_draw():
window.clear()
tank_sprite.draw()
tank_sprite2.draw()
bullet_sprite.draw()
explode_sprite.draw()
#NEEDS TO FIX THE FIRST MOVE AFTER RUNNING
steps=10
speed=800
coll_listx=[tank_sprite.y+(tankImage.height/2),tank_sprite.y-(tankImage.height/2)]
coll_listy=[tank_sprite.x+(tankImage.width/2),tank_sprite.x-(tankImage.width/2)]
collision_under=(bullet_sprite.y<(tank_sprite.y+(tankImage.height/2)))
collision_over=(bullet_sprite.y>(tank_sprite.y-(tankImage.height/2)))
collision_left=(bullet_sprite.x<(tank_sprite.x+(tankImage.width/2)))
collision_right=(bullet_sprite.x>(tank_sprite.x-(tankImage.width/2)))
#if collision_under and collision_over and collision_left and collision_right:
#if bullet_sprite.x in coll_listx:
#if (bullet_sprite.y<(tank_sprite.y+(tankImage.height/2))) and (bullet_sprite.y>(tank_sprite.y-(tankImage.height/2))) and (bullet_sprite.x<(tank_sprite.x+(tankImage.width/2)))and (bullet_sprite.x>(tank_sprite.x-(tankImage.width/2))):
def collision():
if (bullet_sprite.y<(tank_sprite2.y+(tankImage.height/2))) and (bullet_sprite.y>(tank_sprite2.y-(tankImage.height/2))) and (bullet_sprite.x<(tank_sprite2.x+(tankImage.width/2)))and (bullet_sprite.x>(tank_sprite2.x-(tankImage.width/2))):
tank_sprite2.visible=False
bullet_sprite.visible=False
explode_sprite.visible=True
explode_sprite.x=tank_sprite2.x
explode_sprite.y=tank_sprite2.y
bullet_sprite.dy = 0
bullet_sprite.x=-10
bullet_sprite.y=-10
#time.sleep(2)
#explode_sprite.visible=False
#Key Functions
@window.event
def on_text_motion(motion):
if (motion==pyglet.window.key.MOTION_DOWN):
if tank_sprite.y>=0:
tank_sprite.rotation=0
tank_sprite.y=tank_sprite.y-steps
elif (motion==pyglet.window.key.MOTION_UP):
if tank_sprite.y<=window_h-tankImage.height:
tank_sprite.rotation=180
tank_sprite.y=tank_sprite.y+steps
elif (motion==pyglet.window.key.MOTION_LEFT):
if tank_sprite.x>=0:
tank_sprite.rotation=90
tank_sprite.x=tank_sprite.x-steps
elif (motion==pyglet.window.key.MOTION_RIGHT):
if tank_sprite.x<=window_w-tankImage.width:
tank_sprite.rotation=270
tank_sprite.x=tank_sprite.x+steps
@window.event
def on_key_press(key,modifiers):
if (key==pyglet.window.key.SPACE):
bullet_sprite.visible=True
if tank_sprite.rotation==90:#LEFT
bullet_sprite.x=tank_sprite.x-tankImage.width/2
bullet_sprite.y=tank_sprite.y
bullet_sprite.rotation=0
bullet_sprite.dx = speed
def update(dt):
bullet_sprite.x -= bullet_sprite.dx * dt
collision()
pyglet.clock.schedule_interval(update, 1/60.0) # update at 60Hz
elif tank_sprite.rotation==0:#DOWN
bullet_sprite.x=tank_sprite.x
bullet_sprite.y=tank_sprite.y-tankImage.height/2
bullet_sprite.rotation=270
bullet_sprite.dy = speed
def update(dt):
bullet_sprite.y -= bullet_sprite.dy * dt
collision()
pyglet.clock.schedule_interval(update, 1/60.0) # update at 60Hz
elif tank_sprite.rotation==180:#UP
bullet_sprite.x=tank_sprite.x
bullet_sprite.y=tank_sprite.y+tankImage.height/2
bullet_sprite.rotation=90
bullet_sprite.dy = speed
def update(dt):
bullet_sprite.y += bullet_sprite.dy * dt
collision()
pyglet.clock.schedule_interval(update, 1/60.0) # update at 60Hz
elif tank_sprite.rotation==270:#RIGHT
bullet_sprite.x=tank_sprite.x+tankImage.width/2
bullet_sprite.y=tank_sprite.y
bullet_sprite.rotation=180
bullet_sprite.dx = speed
def update(dt):
bullet_sprite.x += bullet_sprite.dx * dt
collision()
pyglet.clock.schedule_interval(update, 1/60.0) # update at 60Hz
bullet_sprite.visible=True
#time.sleep(1)
collision()
#Run Window
pyglet.app.run()