-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
190 lines (158 loc) · 5.96 KB
/
main.py
File metadata and controls
190 lines (158 loc) · 5.96 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
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
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import (
NumericProperty, ReferenceListProperty, ObjectProperty
)
from kivy.vector import Vector
from kivy.clock import Clock
from kivy.core.audio import SoundLoader
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
import random
import pickle
import time
class PongPaddle(Widget):
score = NumericProperty(0)
def bounce_ball(self, ball):
if self.collide_widget(ball):
vx, vy = ball.velocity
#offset = (ball.center_y - self.center_y) / (self.height / 2)
offset = 0
bounced = Vector(vx, -1 * vy)
vel = bounced * 1.0000001
if vel.x == 0:
s = 1
else:
s = vel.x / abs(vel.x)
ball.velocity = vel.x + s * offset, vel.y
class PongBall(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def move(self):
self.pos = Vector(*self.velocity) + self.pos
class PongBall1(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def move(self, widthw):
self.pos = (self.pos[0] - 2, self.pos[1])
if self.pos[0] < 0 or self.pos[0] > widthw:
self.pos[0] = widthw - 20
def new(self, widthw, heightw):
self.pos = (random.randint(5, 20) * widthw // 20, random.randint(5, 20) * heightw // 20)
class PongBallr(Widget):
velocity_x = NumericProperty(0)
velocity_y = NumericProperty(0)
velocity = ReferenceListProperty(velocity_x, velocity_y)
def move(self, widthw, heightw):
self.pos = (self.pos[0] - 1, self.pos[1])
if self.pos[0] < 0 or self.pos[0] > widthw:
self.pos = (widthw, random.randint(5, 20) * heightw // 20)
class PongGame(Widget):
ball = ObjectProperty(None)
ball1 = ObjectProperty(None)
ballr = ObjectProperty(None)
player1 = ObjectProperty(None)
djump = 0
try:
with open('score.dat', 'rb') as file:
record = pickle.load(file)
file.close()
except:
record = 0
def serve_ball(self, vel=(0, 5)):
self.ball.center = self.center
self.ball.velocity = vel
self.ball1.center = (random.randint(0, 20) * self.width//20, random.randint(0, 20) * self.height // 20)
self.ballr.pos = (self.width, random.randint(0, 20) * self.height // 20)
self.vel = vel
def update(self, dt):
self.ball.move()
self.ballr.move(self.width, self.height)
self.ball1.move(self.width)
#print(self.ball1.y)
# bounce of paddles
self.player1.bounce_ball(self.ball)
if self.djump > 0:
self.ball.velocity = (self.vel[0], -self.vel[1])
self.ball.velocity_y = -1
if self.ball.y < 50:
self.ball.y = 75
self.ball.velocity_y *= -1
self.djump -= 1
if self.djump == 0:
self.ball.velocity_y = -self.vel[1]
# bounce ball off bottom or top
#if (self.ball.y < self.y) or (self.ball.top > self.top):
#self.ball.velocity_y *= -1
# went of to a side to score point?
if self.ball.x < self.x or self.ball.x > self.width:
self.ball.velocity_x *= -1
if self.ball.y > self.height or self.ball.y <= 0:
self.ball.velocity_y *= -1
if abs(self.ball.x - self.ball1.x) < 50 and abs(self.ball.y - self.ball1.y) < 50:
self.ball1.new(self.width, self.height)
self.player1.score += 1
if abs(self.ball.x - self.ballr.x) < 50 and abs(self.ball.y - self.ballr.y) < 50:
#self.player1.score += 1
#PobApp().run()
self.record = max(int(self.record), self.player1.score)
with open('score.dat', 'wb') as file:
pickle.dump(self.record, file)
file.close()
#App.get_running_app().stop()
quit()
def on_touch_move(self, touch):
if touch.y < self.height / 3:
self.player1.center_x = touch.x
self.ball.x = touch.x
else:
if self.ball.velocity_y < 0:
self.ball.velocity_y *= -1
#self.ball.pos = self.pos - Vector(*self.ball.velocity)
#print(self.ball.velocity_y)
if self.ball.y >= self.height:
self.ball.y = self.height - 20
self.djump = 5
class RaymanApp(App):
def build(self):
self.icon = r'icon.png'
game = PongGame()
game.serve_ball()
Clock.schedule_interval(game.update, 1.0 / 60.0)
return game
class PobApp(App):
def build(self):
self.icon = r'icon.png'
try:
with open('score.dat', 'rb') as file:
record = pickle.load(file)
file.close()
except:
record = 0
layout = BoxLayout()
img = Image(source='r.jpg',
size_hint=(1, 1),
pos_hint={'center_x': .5, 'center_y': 0.5})
layout.add_widget(img)
button = Button(text='record ' + str(record),
size_hint=(.5, .5),
pos_hint={'center_x': .5, 'center_y': .5})
button.bind(on_press=self.callback)
layout.add_widget(button)
return layout
def callback(self, items):
#PobApp.stop(self)
global PobApp
PobApp.stop(self)
def main():
#RaymanApp().run()
soundo = SoundLoader.load('Overworld.mp3')
sound = SoundLoader.load('Rayman2.mp3')
PobApp().run()
RaymanApp().run()
main()
main()