|
| 1 | +# Game Ping-Pong |
| 2 | + |
| 3 | +# Imports |
| 4 | +from tkinter import * |
| 5 | +import random |
| 6 | +import time |
| 7 | + |
| 8 | +# Variável recebendo o valor digitado pelo usuário |
| 9 | +level = int(input("Qual nível você gostaria de jogar? 1/2/3/4/5 \n")) |
| 10 | + |
| 11 | +# Variável |
| 12 | +length = 500/level |
| 13 | + |
| 14 | +# Instância do Objeto Tk |
| 15 | +root = Tk() |
| 16 | +root.title("Ping Pong") |
| 17 | +root.resizable(0,0) |
| 18 | +root.wm_attributes("-topmost", -1) |
| 19 | + |
| 20 | +# Variável recebendo o resultado da função Canvas |
| 21 | +canvas = Canvas(root, width=800, height=600, bd=0,highlightthickness=0) |
| 22 | +canvas.pack() |
| 23 | +root.update() |
| 24 | + |
| 25 | +# Variável |
| 26 | +count = 0 |
| 27 | + |
| 28 | +# Variável |
| 29 | +lost = False |
| 30 | + |
| 31 | +# Classe |
| 32 | +class Bola: |
| 33 | + def __init__(self, canvas, Barra, color): |
| 34 | + |
| 35 | + # Variáveis |
| 36 | + self.canvas = canvas |
| 37 | + self.Barra = Barra |
| 38 | + self.id = canvas.create_oval(0, 0, 15, 15, fill=color) |
| 39 | + self.canvas.move(self.id, 245, 200) |
| 40 | + |
| 41 | + # Lista |
| 42 | + starts_x = [-3, -2, -1, 1, 2, 3] |
| 43 | + random.shuffle(starts_x) |
| 44 | + |
| 45 | + # Variáveis |
| 46 | + self.x = starts_x[0] |
| 47 | + self.y = -3 |
| 48 | + self.canvas_height = self.canvas.winfo_height() |
| 49 | + self.canvas_width = self.canvas.winfo_width() |
| 50 | + |
| 51 | + # Função |
| 52 | + def draw(self): |
| 53 | + |
| 54 | + # Variáveis |
| 55 | + self.canvas.move(self.id, self.x, self.y) |
| 56 | + pos = self.canvas.coords(self.id) |
| 57 | + |
| 58 | + # Condicional if |
| 59 | + if pos[1] <= 0: |
| 60 | + |
| 61 | + # Variável |
| 62 | + self.y = 3 |
| 63 | + |
| 64 | + # Condicional if |
| 65 | + if pos[3] >= self.canvas_height: |
| 66 | + |
| 67 | + # Variável |
| 68 | + self.y = -3 |
| 69 | + |
| 70 | + # Condicional if |
| 71 | + if pos[0] <= 0: |
| 72 | + |
| 73 | + # Variável |
| 74 | + self.x = 3 |
| 75 | + |
| 76 | + # Condicional if |
| 77 | + if pos[2] >= self.canvas_width: |
| 78 | + |
| 79 | + # Variável |
| 80 | + self.x = -3 |
| 81 | + |
| 82 | + # Variável |
| 83 | + self.Barra_pos = self.canvas.coords(self.Barra.id) |
| 84 | + |
| 85 | + # Condicional if aninhado |
| 86 | + if pos[2] >= self.Barra_pos[0] and pos[0] <= self.Barra_pos[2]: |
| 87 | + if pos[3] >= self.Barra_pos[1] and pos[3] <= self.Barra_pos[3]: |
| 88 | + |
| 89 | + # Variáveis |
| 90 | + self.y = -3 |
| 91 | + global count |
| 92 | + count +=1 |
| 93 | + |
| 94 | + # Chamada à função |
| 95 | + score() |
| 96 | + |
| 97 | + # Condicional if |
| 98 | + if pos[3] <= self.canvas_height: |
| 99 | + |
| 100 | + # Variável |
| 101 | + self.canvas.after(10, self.draw) |
| 102 | + else: |
| 103 | + |
| 104 | + # Chamada à função |
| 105 | + game_over() |
| 106 | + |
| 107 | + # Variáveis |
| 108 | + global lost |
| 109 | + lost = True |
| 110 | + |
| 111 | +# Classe |
| 112 | +class Barra: |
| 113 | + def __init__(self, canvas, color): |
| 114 | + |
| 115 | + # Variáveis |
| 116 | + self.canvas = canvas |
| 117 | + self.id = canvas.create_rectangle(0, 0, length, 10, fill=color) |
| 118 | + self.canvas.move(self.id, 200, 400) |
| 119 | + self.x = 0 |
| 120 | + self.canvas_width = self.canvas.winfo_width() |
| 121 | + self.canvas.bind_all("<KeyPress-Left>", self.move_left) |
| 122 | + self.canvas.bind_all("<KeyPress-Right>", self.move_right) |
| 123 | + |
| 124 | + # Função |
| 125 | + def draw(self): |
| 126 | + |
| 127 | + # Chamada ao método |
| 128 | + self.canvas.move(self.id, self.x, 0) |
| 129 | + |
| 130 | + # Variável |
| 131 | + self.pos = self.canvas.coords(self.id) |
| 132 | + |
| 133 | + # Condicional if |
| 134 | + if self.pos[0] <= 0: |
| 135 | + |
| 136 | + # Variável |
| 137 | + self.x = 0 |
| 138 | + |
| 139 | + # Condicional if |
| 140 | + if self.pos[2] >= self.canvas_width: |
| 141 | + |
| 142 | + # Variável |
| 143 | + self.x = 0 |
| 144 | + |
| 145 | + global lost |
| 146 | + |
| 147 | + # Condicional if |
| 148 | + if lost == False: |
| 149 | + self.canvas.after(10, self.draw) |
| 150 | + |
| 151 | + # Função |
| 152 | + def move_left(self, event): |
| 153 | + |
| 154 | + # Condicional if |
| 155 | + if self.pos[0] >= 0: |
| 156 | + |
| 157 | + # Variável |
| 158 | + self.x = -3 |
| 159 | + |
| 160 | + # Função |
| 161 | + def move_right(self, event): |
| 162 | + |
| 163 | + # Condicional if |
| 164 | + if self.pos[2] <= self.canvas_width: |
| 165 | + |
| 166 | + # Variável |
| 167 | + self.x = 3 |
| 168 | + |
| 169 | +# Função |
| 170 | +def start_game(event): |
| 171 | + |
| 172 | + # Variáveis |
| 173 | + global lost, count |
| 174 | + lost = False |
| 175 | + count = 0 |
| 176 | + |
| 177 | + # Chamada à função |
| 178 | + score() |
| 179 | + |
| 180 | + # Variável que recebe o resultado da função |
| 181 | + canvas.itemconfig(game, text=" ") |
| 182 | + |
| 183 | + # Métodos dos objetos |
| 184 | + time.sleep(1) |
| 185 | + Barra.draw() |
| 186 | + Bola.draw() |
| 187 | + |
| 188 | +# Função |
| 189 | +def score(): |
| 190 | + canvas.itemconfig(score_now, text="Pontos: " + str(count)) |
| 191 | + |
| 192 | +# Função |
| 193 | +def game_over(): |
| 194 | + canvas.itemconfig(game, text="Game over!") |
| 195 | + |
| 196 | +# Instâncias dos objetos Barra e Bola |
| 197 | +Barra = Barra(canvas, "orange") |
| 198 | +Bola = Bola(canvas, Barra, "purple") |
| 199 | + |
| 200 | +# Variáveis que recebem o resultado das funções |
| 201 | +score_now = canvas.create_text(430, 20, text="Pontos: " + str(count), fill = "green", font=("Arial", 16)) |
| 202 | +game = canvas.create_text(400, 300, text=" ", fill="red", font=("Arial", 40)) |
| 203 | +canvas.bind_all("<Button-1>", start_game) |
| 204 | + |
| 205 | +# Executa o programa |
| 206 | +root.mainloop() |
| 207 | + |
| 208 | + |
0 commit comments