好久没有发表博文,堕落了
python是一门解释型,交互式的,面向对象的编程语言,它集成模块,异常,动态类型,非常高级的动态数据类型和类。
pygame就是其中一个游戏开发的模块
以下是我仿照甲鱼写的一个小型游戏:
import pygame
import sys
import math
from pygame.locals import *
from random import *
class Ball(pygame.sprite.Sprite):
def __init__(self, image, position, speed, bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image).convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = speed
self.width, self.height = bg_size[0], bg_size[1]
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.right < 0:
self.rect.left = self.width
elif self.rect.left > self.width:
self.rect.right = 0
elif self.rect.top > self.height:
self.rect.bottom = 0
elif self.rect.bottom < 0:
self.rect.top = self.height
#碰撞检测
def collide_check(item, target):
coll_balls = []
for each in target:
distance = math.sqrt\
(math.pow((item.rect.center[0] - each.rect.center[0]), 2) + \
math.pow((item.rect.center[1] - each.rect.center[1]), 2))
if distance <= (item.rect.width + each.rect.width) / 2:
coll_balls.append(each)
return coll_balls
def main():
pygame.init()
pygame.mixer.init()
ball_image = "gray_ball.png"
bg_image = "background.png"
running =True
BALL_NUM = 6
#添加背景音乐
pygame.mixer.music.load("bg_music.ogg")
pygame.mixer.music.set_volume(0.4)
pygame.mixer.music.play()
#添加音效,要使用的时候添加play()方法
cat_sound = pygame.mixer.Sound("cat.wav")
dog_sound = pygame.mixer.Sound("dog.wav")
hole_sound = pygame.mixer.Sound("hole.wav")
laugh_sound = pygame.mixer.Sound("laugh.wav")
loser_sound = pygame.mixer.Sound("loser.wav")
winner_sound = pygame.mixer.Sound("winner.wav")
#设置音量
cat_sound.set_volume(0.8)
dog_sound.set_volume(0.3)
#音乐播放完时游戏结束
GAMEOVER = USEREVENT
pygame.mixer.music.set_endevent(GAMEOVER)
#基本设置
bg_size = width, height = 1024, 666
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("shengtaoWorld!")
background = pygame.image.load(bg_image).convert_alpha()
balls = []
#实例化类
for each in range(BALL_NUM):
position = randint(0, width-100), randint(0, height-100)
speed = [randint(-10, 10), randint(-10, 10)]
ball = Ball(ball_image, position, speed, bg_size)
#使球运行时
while collide_check(ball, balls):
ball.rect.left, ball.rect.top = randint(0, width-100), randint(0, height-100)
balls.append(ball)
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == GAMEOVER:
loser_sound.play()
pygame.time.delay(2000)
laugh_sound.play()
running = False
#鼠标检测
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
cat_sound.play()
if event.button == 3:
dog_sound.play()
if event.button == 2:
for i in range(8):
cat_sound.play()
screen.blit(background, (0, 0))
#试球运动
for each in balls:
each.move()
screen.blit(each.image, each.rect)
#
for i in range(BALL_NUM):
item = balls.pop(i)
if collide_check(item, balls):
item.speed[0] = -item.speed[0]
item.speed[1] = -item.speed[1]
balls.insert(i, item)
pygame.display.flip()
clock.tick(30)
if __name__ == "__main__":
main()