Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/data/graphics/pong
blob: 0ce3a9ea32db7aba5d794b085c099d94e48bdd3d (plain)
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
# pong: hit the ball with the paddle
#
# use the escape key to exit
# 
# on the XO, the escape key is the top lefthand key,
# circle with an x in it.

import pippy, pygame, sys
from pygame.locals import *
from random import *

# always need to init first thing
pygame.init()

# XO screen is 1200x900
size = width, height = 1200, 900

# create the window and keep track of the surface
# for drawing into
screen = pygame.display.set_mode(size)

# turn off the cursor
pygame.mouse.set_visible(False)

# turn on key repeating (repeat 40 times per second)
pygame.key.set_repeat(25,25)

# start the screen all black
bgcolor = (0,0,0)
screen.fill(bgcolor)

# paddle constants
paddle_width = 20
paddle_length = 100
paddle_radius = paddle_length / 2
paddle_color = (250,250,250)
step = 6 # paddle moves 3 pixels at a go

# ball constants
ball_color = (250,250,250)
ball_radius = 25

# game constants
fsize = 48
msg = "Press 'g' to start game"

font=pygame.font.Font(None, fsize)
text = font.render(msg, True, (250,250,250))
textRect = text.get_rect()
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery

while pippy.pygame.next_frame():

  # display msg
  screen.fill(bgcolor)
  screen.blit(text, textRect)
  pygame.display.flip()

  # chill until a key is pressed
  for idle_event in pygame.event.get():
    if idle_event.type == QUIT:
      sys.exit()

    if idle_event.type == KEYDOWN:
      if idle_event.key == K_ESCAPE:
        sys.exit()

      if idle_event.key == 103: # g key

        # play a game!

        # start the paddle in the center
        paddle_location = height / 2

        # number of balls to a game
        balls = 4

        while balls > 0:

          ball_position = [ball_radius, ball_radius]
          ball_mvect = [randint(3,5), randint(3,5)]
          ball_limit = size
          balls = balls - 1

          while ball_position[0] + ball_radius < ball_limit[0]: # in play

            for event in pygame.event.get():
              if event.type == QUIT:
                sys.exit()

              elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                  sys.exit()
                elif event.key == 273 \
                or event.key == 265 \
                or event.key == 264: # up
                  paddle_location = paddle_location - step
                elif event.key == 274 \
                or event.key == 259 \
                or event.key == 258: # down
                  paddle_location = paddle_location + step
  
            # make sure the paddle is in-bounds
            if paddle_location - paddle_radius < 0:
              paddle_location = paddle_radius
            elif paddle_location + paddle_radius >= height:
              paddle_location = height - 1 - paddle_radius

            # clear the screen
            screen.fill(bgcolor)

            # draw the paddle on the right side of the screen
            pygame.draw.line(screen,
  	                     paddle_color,
                             (width - paddle_width, paddle_location - 
                              paddle_radius),
                             (width - paddle_width,
                              paddle_location+paddle_radius),
                             paddle_width)

            # draw the ball
            pygame.draw.circle(screen, ball_color, ball_position, ball_radius)

            # draw the unused balls
            for i in range(balls):
              pygame.draw.circle(screen, ball_color, (30+i*ball_radius*2.4, 30), 
                                 ball_radius)

            # update the display
            pygame.display.flip()

            # update the ball
            for i in range(2):
              ball_position[i] = ball_position[i] + ball_mvect[i]

              # bounce on top and left
              if ball_position[i] < ball_radius:
                ball_position[i] = ball_radius
                ball_mvect[i] = -1 * ball_mvect[i]
              # bounce on bottom
              elif i == 1 \
              and ball_position[i] >= ball_limit[i] - ball_radius:
                ball_position[i] = ball_limit[i] - ball_radius - 1
                ball_mvect[i] = -1 * ball_mvect[i]
              elif i == 0 \
              and ball_position[i] >= ball_limit[i] - ball_radius - paddle_width \
              and ball_position[1] > paddle_location - paddle_radius \
              and ball_position[1] < paddle_location + paddle_radius :
                ball_position[i] = ball_limit[i] - ball_radius - paddle_width - 1
                ball_mvect[i] = -1 * ball_mvect[i]