FANDOM


#23.1
# PUTTING TEXT ONTO THE SCREEN
# By Jaseman - 23rd April 2012
print ("*** Welcome to The Python Pit! ***")
print () # A line space
print ("Brought to you by The MagPi")

#23.2
# ARITHMETIC
# By Jaseman - 23rd April 2012
print ("Two plus two equals ",2+2); print ()
print ("Eight minus three equals ",8-3); print ()
print ("Four times two equals ",4*2); print ()
print ("Ten divided by two equals ",10/2); print()


#24.1
# SPARE ME THE DETAILS
# By Jaseman - 24th April 2012
print ("Seven divided by three is",7/3); print ()
print ("Seven divided by three is roughly",int(7/3)); print()
print ("Five divided by two is",5/2); print ()
print ("Five divided by two is roughly",int(5/2)); print()


#24.2
# ALGEBRA
# By Jaseman - 24th April 2012
a = 3
b = 7
c = 4
print ("A is equal to",a); print ()
print ("B is equal to",b); print()
print ("C is equal to",c); print ()
print ("A plus B equals",a+b); print()
print ("A plus B plus C equals",a+b+c); print()
print ("A plus B minus C equals",a+b-c); print()


#25.1
# STRINGS OF WORDS
# By Jaseman - 24th April 2012
a = "If you"
b = "notice"
c = "this"
d = "you will"
e = "is not worth"
f = "noticing!"
print (a,b,c,b,d,b,c,b,e,f); print ()


#25.2
# COUNTING WITH FOR LOOPS
# By Jaseman - 24th April 2012
for n in range(0,10+1):
    print (n)



#26.1
# COUNTING WITH WHILE LOOPS
# By Jaseman - 24th April 2012
n=0
while n <= 10:
    print (n)
    n += 1




#26.2
# LOTTERY NUMBERS
# By Jaseman - 24th April 2012
import random
for n in range(1,6+1): # We want six random numbers
    print (random.randint(1,100)) # Picks a number between 1 and 100



#27.1
# BINGO!
# By Jaseman - 24th April 2012
import random
n = 0
while n != 13: # Keep looping till you get thirteen
    n = random.randint(1,100)
    print (n)



#27.2
# STRING ARRAYS
# By Jaseman - 24th April 2012
mytext = ["I", "Love", "My","Raspberry","Pi"]
for n in range(0,5):
    print (mytext[n])
    print()


#28
# BAT AND BALL

# By antiloquax - 28th April 2012

import pygame
from pygame.locals import *
pygame.init()

# set the width and height of the screen
size = [400, 400]
screen = pygame.display.set_mode(size)

# give the window a title
pygame.display.set_caption("Bat and Ball")

# This makes the normal mouse pointer invisible in graphics window
pygame.mouse.set_visible(0)

# create surfaces for the bat and ball
bat_surf = pygame.Surface((64,12))
bat_surf.fill((0,255,0))
batrect = bat_surf.get_rect()
ball_surf = pygame.Surface((30,30))
ballrect = ball_surf.get_rect()

ball = pygame.draw.circle(ball_surf, (0,0,255),[15, 15], 15)

# set speed of ball
speed = [3, 3]

# puts the bat center of screen, near the bottom
batrect.center = ((size[0]/2), (size[1] - 50))

# make a text object
font = pygame.font.Font(None, 36)
text = font.render("Game Over", True, (255,0,0))
textRect = text.get_rect()
textRect.centerx = (size[0]/2)
textRect.centery = (size[1]/2)

# loop until the user clicks the close button
done=0

# create a timer to control how often the screen updates
clock = pygame.time.Clock()

# main game loop
while done == 0:

    screen.fill((0,0,0))

    # event handling
    for event in pygame.event.get(): # if we click something ...
        if event.type == pygame.QUIT: # if we click close ...
            done=1 # this will cause the loop to finish.    

    # moves bat in accordance with the mouse position        
    position = pygame.mouse.get_pos()
    batrect.centerx = position[0]

    # move the ball
    ballrect.left += speed[0]
    ballrect.top += speed[1]

    # collision detection
    if ballrect.colliderect(batrect):
        speed[1] = -speed[1]
    
    # check if the ball is going off screen
    if ballrect.left < 0 or ballrect.right > size[0]:
        speed[0] = -speed[0]
    if ballrect.top < 0:
            speed[1] = -speed[1]

    # print "Game Over" if the ball leaves screen
    if ballrect.top > size[1]:
        screen.blit(text, textRect)
        pygame.display.flip()
        pygame.time.wait(2000) # 2000 milliseconds pause
        ballrect.top=0; ballrect.left=(size[0]/2) # reset the ball position

    screen.blit(ball_surf, ballrect)
    screen.blit(bat_surf, batrect)

    # set the loop to 60 cycles per second
    clock.tick(60)

    #  update the display
    pygame.display.flip()


#added by dbojan 2012-05-09
pygame.quit()
#not sure about this, probably needs import sys.
#sys.exit()
#ref http://stackoverflow.com/questions/5615860/pygame-screen-freezes-when-i-close-it

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.