1.Oil Spill游戏简介.
这款游戏中, 玩家必须要使用发射出高压水流的水枪, 将泄漏到污染区域的油渍冲洗干净. 至少在理论上是可行的. 实际上, 我们只是使用鼠标来点击油污以清除它. 这款游戏使用颜色alpha通道操作以擦除掉油污, 并且这在用户输入以及编程逻辑中是一种好的做法.
2.游戏源码
import sys
import time
import random
import math
import pygame
from pygame.locals import *
from MyLibrary import *
darktan = 190,190,110,255
tan = 210,210,130,255
#油渍类
class OilSprite(MySprite, pygame.sprite.Sprite):
def __init__(self,target):
MySprite.__init__(self,target)
self.radius = random.randint(0,60)+30
play_sound(new_oil)
def update(self, timming, rate=30):
MySprite.update(self, timming, rate)
def fade(self):
r2 = self.radius / 2
color = self.image.get_at((r2,r2))
if color.a > 5:
color.a -= 5
pygame.draw.circle(self.image, color, (r2,r2), r2, 0)
else:
oil_group.remove(self)
play_sound(clean_oil)
#游戏初始化
def game_init():
global screen, backbuffer, font, timer, oil_group, cursor, cursor_group
global sc_width, sc_height
pygame.init()
sc_width = 600
sc_height = 500
screen = pygame.display.set_mode((sc_width,sc_height))
pygame.display.set_caption("Oil Spill Game")
font = pygame.font.Font(None, 36)
pygame.mouse.set_visible(False)
timer = pygame.time.Clock()
backbuffer = pygame.Surface((sc_width,sc_height))
backbuffer.fill(darktan)
oil_group = pygame.sprite.Group()
cursor = MySprite(screen)
cursor.radius = 60
image = pygame.Surface((60,60)).convert_alpha()
image.fill((255,255,255,0))
pygame.draw.circle(image, (80,80,220,70), (30,30), 30, 0)
pygame.draw.circle(image, (80,80,250,255), (30,30), 30, 4)
cursor.set_image(image)
cursor_group = pygame.sprite.Group()
cursor_group.add(cursor)
#音频初始化
def audio_init():
global new_oil, clean_oil
pygame.mixer.init()
new_oil = pygame.mixer.Sound("new_oil.wav")
clean_oil = pygame.mixer.Sound("clean_oil.wav")
#播放音乐
def play_sound(sound):
channel = pygame.mixer.find_channel(True)
channel.set_volume(0.5)
channel.play(sound)
#添加油渍
def add_oil():
global oil_group, new_oil, screen
oil = OilSprite(screen)
image = pygame.Surface((oil.radius,oil.radius)).convert_alpha()
image.fill((255,255,255,0))
oil.fadelevel = random.randint(50,150)
oil_color = 10,10,20,oil.fadelevel
r2 = oil.radius / 2
pygame.draw.circle(image,oil_color,(r2,r2),r2,0)
oil.set_image(image)
oil.set_pos((random.randint(0,600-oil.frame_width),random.randint(0,500-oil.frame_height)))
oil_group.add(oil)
game_init()
audio_init()
game_over = False
last_time = 0
while True:
timer.tick(30)
ticks = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
b1,b2,b3 = pygame.mouse.get_pressed()
mx,my = pygame.mouse.get_pos()
pos = (mx+30,my+30)
if b1 > 0:
pygame.draw.circle(backbuffer, tan, pos, 30, 0)
oil_hit = None
for oil in oil_group:
if pygame.sprite.collide_circle_ratio(0.5)(cursor, oil):
oil_hit = oil
if b1 > 0:
oil_hit.fade()
break
if ticks > last_time + 1000:
add_oil()
last_time = ticks
screen.blit(backbuffer, (0,0))
oil_group.update(ticks, 50)
oil_group.draw(screen)
cursor.set_pos((mx,my))
cursor_group.update(ticks)
cursor_group.draw(screen)
if oil_hit:
print_text(font, 0, 0, "OIL SPLOTCH - CLEAN IT")
else:
print_text(font, 0, 0, "CLEAN")
pygame.display.update()
time.sleep(1.0/100)
3.音频文件
如需音频文件, 可以联系本人