-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
241 lines (199 loc) · 6.84 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import tkinter as tk
import random
import json
class UzayOyunu:
def __init__(self):
# Create window
self.pencere = tk.Tk()
self.pencere.title("Uzay Oyunu")
self.pencere.geometry("600x800")
# Game canvas
self.canvas = tk.Canvas(self.pencere, width=600, height=800, bg='black')
self.canvas.pack()
# Load score data
self.skor_yukle()
# Show main menu
self.ana_menu()
def skor_yukle(self):
"""Load saved score data"""
try:
with open('skorlar_i.json', 'r') as dosya:
self.skor_verileri = json.load(dosya)
except:
self.skor_verileri = {"yuksek_skor": 0}
def skor_kaydet(self):
"""Save score data"""
with open('skorlar_i.json', 'w') as dosya:
json.dump(self.skor_verileri, dosya)
def ana_menu(self):
"""Display main menu"""
self.canvas.delete('all')
# Game title
self.canvas.create_text(
300, 200,
text="UZAY OYUNU",
fill='white',
font=('Arial', 36, 'bold')
)
# High score
self.canvas.create_text(
300, 300,
text=f"En Yüksek Skor: {self.skor_verileri['yuksek_skor']}",
fill='yellow',
font=('Arial', 20)
)
# Start instruction
self.canvas.create_text(
300, 400,
text="Başlamak için SPACE'e basın",
fill='white',
font=('Arial', 20)
)
# Controls
self.canvas.create_text(
300, 500,
text="Kontroller:\n← → : Hareket\nSPACE : Ateş",
fill='white',
font=('Arial', 16)
)
# Bind space key
self.pencere.bind('<space>', self.oyunu_baslat)
def oyunu_baslat(self, event=None):
"""Start the game"""
self.pencere.unbind('<space>')
self.skor = 0
self.can = 3
self.oyun_aktif = True
# Game objects
self.gemi_x = 300
self.gemi_y = 700
self.meteorlar = []
self.mermiler = []
# Clear game area
self.canvas.delete('all')
# Create spaceship
self.gemi = self.canvas.create_polygon(
self.gemi_x, self.gemi_y - 20,
self.gemi_x - 15, self.gemi_y + 10,
self.gemi_x + 15, self.gemi_y + 10,
fill='white'
)
# Score display
self.skor_label = self.canvas.create_text(
50, 30, text=f"Skor: {self.skor}",
fill='white', font=('Arial', 14)
)
# Lives display
self.can_label = self.canvas.create_text(
550, 30, text=f"Can: {self.can}",
fill='white', font=('Arial', 14)
)
# Bind controls
self.pencere.bind('<Left>', self.sola_git)
self.pencere.bind('<Right>', self.saga_git)
self.pencere.bind('<space>', self.ates_et)
# Start game loop
self.oyun_dongusu()
def sola_git(self, event):
"""Move ship left"""
if self.gemi_x > 20:
self.gemi_x -= 10
self.canvas.move(self.gemi, -10, 0)
def saga_git(self, event):
"""Move ship right"""
if self.gemi_x < 580:
self.gemi_x += 10
self.canvas.move(self.gemi, 10, 0)
def ates_et(self, event):
"""Fire weapon"""
mermi = self.canvas.create_oval(
self.gemi_x - 2, self.gemi_y - 20,
self.gemi_x + 2, self.gemi_y - 15,
fill='yellow'
)
self.mermiler.append(mermi)
def meteor_olustur(self):
"""Create new meteor"""
x = random.randint(20, 580)
meteor = self.canvas.create_oval(
x - 10, -10, x + 10, 10,
fill='red'
)
self.meteorlar.append({'id': meteor, 'x': x, 'y': -10})
def oyun_dongusu(self):
"""Main game loop"""
if not self.oyun_aktif:
return
# Move meteors
for meteor in self.meteorlar[:]:
self.canvas.move(meteor['id'], 0, 3)
meteor['y'] += 3
# Check if meteor is off screen
if meteor['y'] > 800:
self.canvas.delete(meteor['id'])
self.meteorlar.remove(meteor)
# Check collision with ship
elif abs(meteor['y'] - self.gemi_y) < 30 and abs(meteor['x'] - self.gemi_x) < 20:
self.can -= 1
self.canvas.delete(meteor['id'])
self.meteorlar.remove(meteor)
self.canvas.itemconfig(self.can_label, text=f"Can: {self.can}")
if self.can <= 0:
self.oyun_bitti()
return
# Move bullets
for mermi in self.mermiler[:]:
self.canvas.move(mermi, 0, -5)
mermi_konum = self.canvas.coords(mermi)
# Check if bullet is off screen
if mermi_konum[1] < 0:
self.canvas.delete(mermi)
self.mermiler.remove(mermi)
continue
# Check meteor hits
for meteor in self.meteorlar[:]:
meteor_konum = self.canvas.coords(meteor['id'])
if (abs(mermi_konum[1] - meteor['y']) < 20 and
abs(mermi_konum[0] - meteor['x']) < 20):
self.skor += 10
self.canvas.itemconfig(self.skor_label, text=f"Skor: {self.skor}")
self.canvas.delete(mermi)
self.canvas.delete(meteor['id'])
self.mermiler.remove(mermi)
self.meteorlar.remove(meteor)
break
# Create new meteor
if random.random() < 0.03:
self.meteor_olustur()
# Continue loop
self.pencere.after(20, self.oyun_dongusu)
def oyun_bitti(self):
"""Show game over screen"""
self.oyun_aktif = False
# Unbind controls
self.pencere.unbind('<Left>')
self.pencere.unbind('<Right>')
self.pencere.unbind('<space>')
# Check high score
if self.skor > self.skor_verileri['yuksek_skor']:
self.skor_verileri['yuksek_skor'] = self.skor
self.skor_kaydet()
# Game over screen
self.canvas.delete('all')
self.canvas.create_text(
300, 300,
text=f"OYUN BİTTİ!\nSkorunuz: {self.skor}\n" +
f"En Yüksek Skor: {self.skor_verileri['yuksek_skor']}\n\n" +
"Tekrar oynamak için SPACE'e basın",
fill='white',
font=('Arial', 20),
justify='center'
)
# Rebind space key
self.pencere.bind('<space>', self.oyunu_baslat)
def baslat(self):
"""Start the game"""
self.pencere.mainloop()
if __name__ == "__main__":
oyun = UzayOyunu()
oyun.baslat()