-
Notifications
You must be signed in to change notification settings - Fork 3
/
onepaddlepong_simple.py
109 lines (94 loc) · 3.8 KB
/
onepaddlepong_simple.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
#!/usr/bin/python3
''' Simple Pong game, code built on arcade template from
https://opensource.com/article/18/4/easy-2d-game-creation-python-and-arcade
'''
import arcade
from arcade.geometry import check_for_collision
import random
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
TITLE = 'One Paddle Pong'
class Pong(arcade.Window):
""" Main application class. """
def __init__(self, width, height,title):
super().__init__(width, height,title)
arcade.set_background_color(arcade.color.AMAZON)
self.score = 0
self.set_mouse_visible(False)
self.ball_sound = arcade.load_sound("resources/coin1.wav")
self.paddle_sound = arcade.load_sound("resources/jump1.wav")
self.lose_sound = arcade.load_sound("resources/laser1.wav")
def setup(self):
# Set up your game here
self.player_paddle = arcade.Sprite('resources/paddle.png',1)
self.ball = arcade.Sprite('resources/ball.png')
self.start_ball()
def start_ball(self):
# direction also affects speed
self.direction_x = 1
self.direction_y = 1
self.speed = 180
self.player_paddle.center_x = SCREEN_WIDTH -10
self.player_paddle.center_y = 50
self.ball.center_x = 10
self.ball.center_y = random.randint (10, SCREEN_HEIGHT - 10)
def on_draw(self):
""" Render the screen. """
arcade.start_render()
self.player_paddle.draw()
self.ball.draw()
arcade.draw_text('Score: ' + str( self.score) , 50, 50, arcade.color.BLACK, 16)
def update(self, delta_time):
""" All the logic to move, and the game logic goes here.
"""
# print (delta_time)
self.ball.center_x += self.direction_x * self.speed * delta_time # move to the right
self.ball.center_y += self.direction_y * self.speed * delta_time # move up and down
# next make ball start from random spot move in random direction
# bounce off walls and paddle.
if check_for_collision (self.ball, self.player_paddle) == True:
self.score += 10
arcade.play_sound(self.paddle_sound)
# keeps ball from bouncing back and forth when it hits paddle end
#self.ball.center_x = SCREEN_WIDTH - SCREEN_WIDTH / 10
self.direction_x *= -1
elif self.ball.center_x > SCREEN_WIDTH:
# player misses ball
arcade.play_sound(self.lose_sound)
arcade.pause(2)
self.score -= 1
self.start_ball()
elif self.ball.center_x <10:
self.direction_x *= -1
arcade.play_sound(self.ball_sound)
elif self.ball.center_y > SCREEN_HEIGHT or self.ball.center_y < 10:
self.direction_y *= -1
arcade.play_sound(self.ball_sound)
elif self.score > 10:
print ('Winner')
arcade.draw_text('You Win! ', 100, 100, arcade.color.RED, 36)
arcade.start_render()
arcade.pause(5)
exit()
def on_key_press(self, key, key_modifiers):
# this needs code to keep paddle on screen
if key == arcade.key.UP:
self.player_paddle.center_y += 30
elif key == arcade.key.DOWN:
self.player_paddle.center_y += -30
elif key == arcade.key.Q:
exit()
def on_key_release(self, key, key_modifiers):
pass
def on_mouse_press(self, x, y, button, modifiers):
print("x=", x, "y=", y, "button=", button)
def main():
'''a main() function is not necessary unless you use this as a module
you import into another program. The code in main() will only runs if
it is not imported. If it is imported you can use all the methods.
'''
game = Pong(SCREEN_WIDTH, SCREEN_HEIGHT, TITLE)
game.setup()
arcade.run()
if __name__ == "__main__":
main()