-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_v1.py
145 lines (96 loc) · 3.2 KB
/
game_v1.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
# Game Ping-Pong
from tkinter import *
import random
import time
level = int(input("Qual nível você gostaria de jogar? 1/2/3/4/5 \n"))
length = 500/level
root = Tk()
root.title("Ping Pong")
root.resizable(0,0)
root.wm_attributes("-topmost", -1)
canvas = Canvas(root, width=800, height=600, bd=0,highlightthickness=0)
canvas.pack()
root.update()
# Variável
count = 0
lost = False
class Bola:
def __init__(self, canvas, Barra, color):
self.canvas = canvas
self.Barra = Barra
self.id = canvas.create_oval(0, 0, 15, 15, fill=color)
self.canvas.move(self.id, 245, 200)
starts_x = [-3, -2, -1, 1, 2, 3]
random.shuffle(starts_x)
self.x = starts_x[0]
self.y = -3
self.canvas_height = self.canvas.winfo_height()
self.canvas_width = self.canvas.winfo_width()
def draw(self):
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 3
if pos[3] >= self.canvas_height:
self.y = -3
if pos[0] <= 0:
self.x = 3
if pos[2] >= self.canvas_width:
self.x = -3
self.Barra_pos = self.canvas.coords(self.Barra.id)
if pos[2] >= self.Barra_pos[0] and pos[0] <= self.Barra_pos[2]:
if pos[3] >= self.Barra_pos[1] and pos[3] <= self.Barra_pos[3]:
self.y = -3
global count
count +=1
score()
if pos[3] <= self.canvas_height:
self.canvas.after(10, self.draw)
else:
game_over()
global lost
lost = True
class Barra:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, length, 10, fill=color)
self.canvas.move(self.id, 200, 400)
self.x = 0
self.canvas_width = self.canvas.winfo_width()
self.canvas.bind_all("<KeyPress-Left>", self.move_left)
self.canvas.bind_all("<KeyPress-Right>", self.move_right)
def draw(self):
self.canvas.move(self.id, self.x, 0)
self.pos = self.canvas.coords(self.id)
if self.pos[0] <= 0:
self.x = 0
if self.pos[2] >= self.canvas_width:
self.x = 0
global lost
if lost == False:
self.canvas.after(10, self.draw)
def move_left(self, event):
if self.pos[0] >= 0:
self.x = -3
def move_right(self, event):
if self.pos[2] <= self.canvas_width:
self.x = 3
def start_game(event):
global lost, count
lost = False
count = 0
score()
canvas.itemconfig(game, text=" ")
time.sleep(1)
Barra.draw()
Bola.draw()
def score():
canvas.itemconfig(score_now, text="Pontos: " + str(count))
def game_over():
canvas.itemconfig(game, text="Game over!")
Barra = Barra(canvas, "orange")
Bola = Bola(canvas, Barra, "purple")
score_now = canvas.create_text(430, 20, text="Pontos: " + str(count), fill = "green", font=("Arial", 16))
game = canvas.create_text(400, 300, text=" ", fill="red", font=("Arial", 40))
canvas.bind_all("<Button-1>", start_game)
root.mainloop()