TORONTO KIDS COMPUTER CLUB | Monday 20:00 Python Practice 21.01.11.
18917

# Monday 20:00 Python Practice 21.01.11.

## 14 Jan Monday 20:00 Python Practice 21.01.11.

Question:

How to create ending screen shows the following 3 lines of information:

`Game OverYour score is: ??press space to restart`

and the game will be restarted if you press the space.

• ##### Ethan Li
Posted at 01:03h, 19 January Reply

import pygame, sys, time, math
import random as r
SCREEN_WIDTH= 640
SCREEN_HEIGHT= 640
BG_HEIGHT=1055

STATUS_START=1
STATUS_PLAY=2
STATUS_GAMEOVER=3

WEAPON_MISSILE=1
WEAPON_LASER=2

SCROLL_SPEED=2

‘2. Quit’]

e.msgbox(‘Welcome to the dogfight! Good luck!!’)
while True:
choice=e.choicebox(‘Please select from the following options: ‘,

confirm=e.buttonbox(‘Are you sure you want to quit the game?’,
choices=[‘Yes’, ‘No’])
if confirm==’Yes’:
break
else:

class Fighter(pygame.sprite.Sprite):
def __init__(self, image, location):
pygame.sprite.Sprite.__init__(self)
self.rect=self.image.get_rect()
self.rect.centerx, self.rect.centery=location
self.lives=3
self.counter_protection=60

def canBeKill(self):
return self.counter_protection==0

def resetProtection(self):
self.counter_protection=60

def getCounter(self):
return self.counter_protection

def gotShot(self):
self.lives -=1

def getLives(self):
return self.lives

def isGameOver(self):
return self.lives==0

def update(self):
”’
if pygame.key.get_pressed()[pygame.K_w]:
self.rect.top -= 10
elif pygame.key.get_pressed()[pygame.K_s]:
self.rect.top += 10
if pygame.key.get_pressed()[pygame.K_a]:
self.rect.left -= 10
elif pygame.key.get_pressed()[pygame.K_d]:
self.rect.left += 10
”’
#mouse control 1
#mouse control 2
self.rect.centerx += (pygame.mouse.get_pos()[0]-self.rect.centerx)//10
self.rect.centery += (pygame.mouse.get_pos()[1]-self.rect.centery)//10
if self.counter_protection>0:
self.counter_protection -=1

class Enemy(pygame.sprite.Sprite):
def __init__(self, image, location, speed):
pygame.sprite.Sprite.__init__(self)
self.rect=self.image.get_rect()
self.rect.left, self.rect.top=location
self.speed=speed
self.hp=5

def gotShot(self):
if self.hp > 0:
self.hp -= 1

def isKilled(self):
if self.hp==0:
return True
return False

def update(self):
self.rect=self.rect.move(self.speed)
if self.rect.leftSCREEN_WIDTH:
self.speed[0]=-self.speed[0]

class Missile(pygame.sprite.Sprite):
def __init__(self, image, location, speed, degree):
pygame.sprite.Sprite.__init__(self)
self.rect=self.image.get_rect()
self.rect.left, self.rect.top=location
self.speed=speed
self.degree= degree

def update(self):
self.rect=self.rect.move(self.speed)
self.speed[0]=int(10*math.sin(self.degree))
self.degree +=0.2
if self.rect.bottom 0:
self.image_index=(self.image_index+1)%2
self.rect.bottom -= 40
else:
self.kill()
#kill means to delete that item/command from code.

class Energy(pygame.sprite.Sprite):
def __init__(self, image, location, speed):
pygame.sprite.Sprite.__init__(self)
self.rect=self.image.get_rect()
self.rect.left, self.rect.top=location
self.speed=speed

def update(self):
self.rect=self.rect.move(self.speed)
if self.rect.leftSCREEN_WIDTH:
self.speed[0]=-self.speed[0]

class Explosion(pygame.sprite.Sprite):
def __init__(self, location, spriteNum=1):
pygame.sprite.Sprite.__init__(self)
self.images_list_1=[‘explosion-1.png’,
‘explosion-2.png’,
‘explosion-3.png’,
‘explosion-4.png’,
‘explosion-5.png’,
‘explosion-6.png’,
‘explosion-7.png’]
self.images_list_2=[‘hitexplosion0.png’,
‘hitexplosion1.png’,
‘hitexplosion2.png’,
‘hitexplosion3.png’,
‘hitexplosion4.png’,
‘hitexplosion5.png’,
‘hitexplosion6.png’]

self.spriteNum=spriteNum
if spriteNum==1:
self.images_list=self.images_list_1
else:
self.images_list=self.images_list_2

self.image_index=0
self.rect=self.image.get_rect()
self.rect.centerx, self.rect.centery=location

def update(self):
if self.image_index==6:
self.kill()
else:
self.image_index+=1

class Planet(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.images_list=[‘p0.png’,
‘p1.png’,
‘p2.png’]
self.image_index=r.randint(0,2)
self.rect=self.image.get_rect()
self.rect.centerx=r.randint(-200, SCREEN_WIDTH-300)
self.rect.centery=-1000

def update(self):
self.rect.centery += SCROLL_SPEED
if self.rect.centery>1000:
self.image_index=(self.image_index+1)%3
self.rect.centerx=r.randint(-200, SCREEN_WIDTH-300)
self.rect.centery=-1000

class Titan(pygame.sprite.Sprite):
def __init__(self, image, location, speed):
pygame.sprite.Sprite.__init__(self)
self.rect=self.image.get_rect()
self.rect.left, self.rect.top=location
self.speed=speed
self.hp=100

def gotShot(self, damage):
if self.hp > 0:
self.hp -= damage

def isKilled(self):
if self.hp==0:
return True
return False

def update(self):
self.rect=self.rect.move(self.speed)
if self.rect.top > SCREEN_HEIGHT:
self.kill()

class Bullet(pygame.sprite.Sprite):
def __init__(self, image, location, speed):
pygame.sprite.Sprite.__init__(self)
self.rect=self.image.get_rect()
self.rect.left, self.rect.top=location
self.speed=speed

def update(self):
self.rect=self.rect.move(self.speed)
if self.rect.right SCREEN_WIDTH or \
self.rect.bottom SCREEN_HEIGHT:
self.kill()

pygame.init()
screen=pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
screen.fill([0,0,0])

scroll_height=SCREEN_HEIGHT – BG_HEIGHT

planet=Planet()

#initialize all the sprite and sprite groups
fighter=Fighter(‘fighter.png’, [SCREEN_WIDTH//2,SCREEN_HEIGHT-100])
enemies=pygame.sprite.Group()
for i in range(5):

titan = None

lasers= pygame.sprite.Group()

missiles=pygame.sprite.Group()
[fighter.rect.centerx, fighter.rect.centery],
[0, -20], 0))
[fighter.rect.centerx, fighter.rect.centery],
[0, -20], 90))
energies=pygame.sprite.Group()

explos=pygame.sprite.Group()

bullets=pygame.sprite.Group()

clock=pygame.time.Clock()

score=0

”’
keyDelay=50
keyInterval=50
pygame.key.set_repeat(keyDelay, keyInterval)
”’

counter_enemy=r.randint(15, 30)

counter_titan=r.randint(150, 200)

counter_bullet=30

game_status= STATUS_START

weapon = WEAPON_MISSILE

score=0
font_score=pygame.font.Font(None, 20)

while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.display.quit()
sys.exit()

#keyboard control 1
elif event.type == pygame.KEYDOWN:
if event.key==pygame.K_1:
weapon=WEAPON_MISSILE
elif event.key==pygame.K_2:
weapon=WEAPON_LASER

”’
elif event.key==pygame.K_a:
fighter.rect.left -= 20
elif event.key==pygame.K_d:
fighter.rect.left += 20
”’

clock.tick(30)

#start screen
if game_status==STATUS_START:
screen.blit(startImage, [0, 0])
pygame.display.flip()
if True in pygame.mouse.get_pressed() and \
pygame.mouse.get_pos()[0]>220 and \
pygame.mouse.get_pos()[0]360 and \
pygame.mouse.get_pos()[1]<430:
game_status=STATUS_PLAY

continue

#set up scroll background
scroll_height+= SCROLL_SPEED
if scroll_height < 0:
screen.blit(bg0, [0, scroll_height])
elif scroll_height < SCREEN_HEIGHT:
screen.blit(bg0, [0, scroll_height])
screen.blit(bg1, [0, scroll_height – BG_HEIGHT])
elif scroll_height<BG_HEIGHT:
screen.blit(bg1, [0, scroll_height – BG_HEIGHT])
elif scroll_height 0 or \
len(pygame.sprite.spritecollide(fighter, bullets, True))>0:
fighter.resetProtection()
fighter.rect.centerx= SCREEN_WIDTH//2
fighter.rect.centery=SCREEN_HEIGHT -100

for i in range(fighter.getLives()-1):
screen.blit(small_fighter,
[SCREEN_WIDTH-i* 30, SCREEN_HEIGHT-30])

fighter.update()
if fighter.getCounter()%2==0:
screen.blit(fighter.image, fighter.rect)

for enemy in enemies:
enemy.update()
screen.blit(enemy.image, enemy.rect)

if True in pygame.mouse.get_pressed():
if weapon==WEAPON_MISSILE:
for missile in missiles:
missile.rect.centerx=fighter.rect.centerx
missile.rect.centery=fighter.rect.centery
elif weapon == WEAPON_LASER:

#update and show weapons
for missile in missiles:
missile.update()
screen.blit(missile.image, missile.rect)
missile_kills_list=pygame.sprite.spritecollide(missile, enemies, True)
if len(missile_kills_list) >0:
missile.rect.bottom=-100
for enemy in missile_kills_list:
[enemy.rect.centerx, enemy.rect.centery],
[r.randint(1,3), r.randint(1,2)]))

for laser in lasers:
laser.update()
screen.blit(laser.image, laser.rect)
laser_shot_list=pygame.sprite.spritecollide(laser, enemies, False)
if len(laser_shot_list)>0:
laser.kill()
for enemy in laser_shot_list:
enemy.gotShot()
if enemy.isKilled():
[enemy.rect.centerx, enemy.rect.centery],
[r.randint(1,3), r.randint(1,2)]))
enemy.kill()

#create titan enemy
counter_titan -= 1
if counter_titan ==0:
titan=Titan(‘spaceship_tut.png’,
[r.randint(100, SCREEN_WIDTH-200), -160],
[0, 1])
counter_titan=900

if titan:
titan.update()
screen.blit(titan.image, titan.rect)
counter_bullet -= 1
if counter_bullet==0:
for i in range(20):
[titan.rect.centerx, titan.rect.centery],
[int(10*math.sin(i*18)), int(10*math.cos(i*18))]))

hitList=pygame.sprite.spritecollide(titan, lasers, True,
for hit in hitList:
titan.gotShot(1)
hitList=pygame.sprite.spritecollide(titan, missiles, False,

for hit in hitList:
hit.rect.bottom = -100
titan.gotShot(5)
if titan.isKilled():
for i in range(15):
titan.rect.centery + r.randint(-60, 60)]))

titan=None
score+=50

score += len(pygame.sprite.spritecollide(fighter, energies, True))

for energy in energies:
energy.update()
screen.blit(energy.image, energy.rect)

for explo in explos:
explo.update()
screen.blit(explo.image, explo.rect)

for bullet in bullets:
bullet.update()
screen.blit(bullet.image, bullet.rect)