弹球游戏升级版
弹球游戏的效果
通过上次的弹球游戏实验我决定进一步升级改进,让游戏更加生动,步入正题,首先我们建立目录下的所有文件,如下图:
images文件夹里面的图片内容如下:
包含一百三十个特色的弹球对象
这个图是鼠标光标的代替图片
这个图片是窗口的图标
游戏设计的主逻辑
首先我们的游戏目的是要通过随机读取所有的弹球然后显示在屏幕上,遇到边框就反弹,我们通过我们的鼠标棒棒糖去点击弹球,弹球即可消失。
首先先创建所有的弹球图片目录链接,通过如下模块程序完成此功能:
#!/usr/bin/python
#coding=utf-8
import os
def ImagesName():
mydir = r'images/'
lis=[]
index=0
for pathstr,dirlist,filelist in os.walk(mydir):
for file in filelist:
index+=1
lis.append(file)
return lis,index
此程序是读取images中所有的弹球图片然后吧完整的图片目录链接放到一个名为 lis 列表中,最终返回列表和列表长度,这就完成了读取所有链接并返回的模块。
接下来写游戏主程序,首先导入主要的包
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pygame,time,wx,os
from sys import exit
import random
from pygame.locals import *
import imgli
pygame.init()
这里的imgli就是刚刚设计的链接模块
app=wx.App()#wx frames
WHFRAMES=wx.DisplaySize()
WIDTH=int(WHFRAMES[0]))
HEIGHT=int(WHFRAMES[1]))
创建wx框架,再读取当前电脑的分辨率,并把分辨率数值放入宽和高的变量中
screen=pygame.display.set_mode((WIDTH,HEIGHT),0,32)
caption=pygame.display.set_caption("game")
ic=pygame.image.load("icon.png").convert_alpha()
pygame.display.set_icon(ic)
screen.fill([0,0,0])
创建游戏窗口并设置窗口标题和窗口图标,然后游戏窗口用黑色背景填充。
ds=0
list_color=[]
bg_flag=1
bg_rect=50 #rect size
#bg list
collition_num=0 #collition success num flag暂时未使用
cur=pygame.image.load("cursor.png").convert_alpha()
cur=pygame.transform.scale(cur,(60,60))
Hero_0=pygame.image.load('images/0.png').convert_alpha()
Hero_0=pygame.transform.scale(Hero_0,(int(1.5*Hero_0.get_width()),int(1.5*Hero_0.get_height())))
Hero_life=True
Hero_create=True
Hero_pos=[random.randint(0,WIDTH-Hero_0.get_width()),random.randint(0,HEIGHT-Hero_0.get_height())]
Hero_pos_v=[1,1]
Ecollition=False
playPause=False
angle=0
angle_zf=0.5
sca=1
curcallition=False
cur_size=cur.get_width(),cur.get_height()
curpos=[-10,-10]
CurClick_Animation=False
pygame.mouse.set_visible(False)
ImgNames,ImgNames_index=imgli.ImagesName()#import imagesname
通过游戏设置好所有的数据变量,看不懂没关系,后面有完整的代码可以测试。
事件检测模块代码如下,通过鼠标事件去操作检测是否发生点击和滚轮操作,并判断此操作是否和弹球相撞。
def forcusecollition(playPause,curcallition,curpos,Ecollition,CurClick_Animation):
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
pressed_array=pygame.mouse.get_pressed()
for index in range(len(pressed_array)):
if pressed_array[index]:
pos=pygame.mouse.get_pos()
if index==0:
#print 'left pressed'
Ecollition=EachCollition()
CurClick_Animation=True
elif index==1:
pass#print 'wheel pressed'
elif index==2:
pass#print 'right pressed'
elif event.type==pygame.MOUSEMOTION:
mspos_array=pygame.mouse.get_pos()
curpos=mspos_array
curcallition=True
elif event.type==pygame.KEYDOWN:
if event.key==K_a:
pass#print 'a'
return playPause,curcallition,curpos,Ecollition,CurClick_Animation
下面是弹球是否越界,如果越界就反弹
def WallColiition(Hero_pos,Hero_pos_v,angle_zf):
if Hero_pos[0]<Hero_0.get_width()/2 or Hero_pos[0]>WIDTH-Hero_0.get_width()/2:
Hero_pos_v[0]=-Hero_pos_v[0]
angle_zf=-angle_zf
if Hero_pos[1]<Hero_0.get_height()/2 or Hero_pos[1]>HEIGHT-Hero_0.get_height()/2:
Hero_pos_v[1]=-Hero_pos_v[1]
angle_zf=-angle_zf
return Hero_pos_v,angle_zf
下面的代码是通过事件检测模块返回的数据变量来判断是否和我的鼠标棒棒糖相撞
def EachCollition():
if Hero_pos[0]-Hero_0_trans.get_width()/2>=curpos[0]-25 and Hero_pos[0]-Hero_0_trans.get_width()/2>=curpos[0]-25:
return False
if curpos[0]-25>=Hero_pos[0]-Hero_0_trans.get_width()/2 and curpos[0]-25>=Hero_pos[0]-Hero_0_trans.get_width()/2+Hero_0.get_width():
return False
if Hero_pos[1]-Hero_0_trans.get_height()/2>=curpos[1]-20 and Hero_pos[1]-Hero_0_trans.get_height()/2>=curpos[1]-20:
return False
if curpos[1]-20>=Hero_pos[1]-Hero_0_trans.get_height()/2 and curpos[1]-20>=Hero_pos[1]-Hero_0_trans.get_height()/2+Hero_0.get_height():
return False
return True
然后填充并刷新屏幕
screen.fill([0,255,255])
pygame.display.flip()
再设计游戏循环体中的内容,游戏中,弹球是一边移动一边旋转的,撞了边框然后就往反方向旋转,在游戏的开局是有一个动画设计如下:
动画的代码如下:
#background
bg_tx=HEIGHT/bg_rect if HEIGHT%bg_rect==0 else HEIGHT/bg_rect+1
movebg_list=[0]*int(bg_tx)
bg_tx = int(bg_tx)
if bg_flag==1:
for i in range(int(bg_tx)):
r,g,b=0,random.randint(200,255),random.randint(200,255)
for j in range(WIDTH):
if movebg_list[i]<WIDTH:
movebg_list[i]+=10
pygame.draw.rect(screen,(r,g,b),(0,ds,movebg_list[i],bg_rect))
pygame.display.update()
pygame.time.delay(1)
else:
movebg_list[i]=WIDTH
list_color.append([r,g,b])
ds+=bg_rect
ds=0
bg_flag=0
else:
for i in range(bg_tx):
pygame.draw.rect(screen,list_color[i],(0,ds,WIDTH,bg_rect))
ds+=bg_rect
ds=0
#background
上面的动画代码是写在游戏循环体中的
通过上述介绍,我们完整的代码如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pygame,time,wx,os
from sys import exit
import random
from pygame.locals import *
import imgli
pygame.init()
app=wx.App()#wx frames
WHFRAMES=wx.DisplaySize()
WIDTH=int(WHFRAMES[0])
HEIGHT=int(WHFRAMES[1])
screen=pygame.display.set_mode((WIDTH,HEIGHT),0,32)
caption=pygame.display.set_caption("game")
ic=pygame.image.load("icon.png").convert_alpha()
pygame.display.set_icon(ic)
screen.fill([0,0,0])
#bg list
ds=0
list_color=[]
bg_flag=1
bg_rect=50 #rect size
#bg list
collition_num=0 #collition success num flag暂时未使用
cur=pygame.image.load("cursor.png").convert_alpha()
cur=pygame.transform.scale(cur,(60,60))
Hero_0=pygame.image.load('images/0.png').convert_alpha()
Hero_0=pygame.transform.scale(Hero_0,(int(1.5*Hero_0.get_width()),int(1.5*Hero_0.get_height())))
Hero_life=True
Hero_create=True
Hero_pos=[random.randint(0,WIDTH-Hero_0.get_width()),random.randint(0,HEIGHT-Hero_0.get_height())]
Hero_pos_v=[1,1]
Ecollition=False
playPause=False
angle=0
angle_zf=0.5
sca=1
curcallition=False
cur_size=cur.get_width(),cur.get_height()
curpos=[-10,-10]
CurClick_Animation=False
pygame.mouse.set_visible(False)
ImgNames,ImgNames_index=imgli.ImagesName()#import imagesname
def forcusecollition(playPause,curcallition,curpos,Ecollition,CurClick_Animation):
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
pressed_array=pygame.mouse.get_pressed()
for index in range(len(pressed_array)):
if pressed_array[index]:
pos=pygame.mouse.get_pos()
if index==0:
#print 'left pressed'
Ecollition=EachCollition()
CurClick_Animation=True
elif index==1:
pass#print 'wheel pressed'
elif index==2:
pass#print 'right pressed'
elif event.type==pygame.MOUSEMOTION:
mspos_array=pygame.mouse.get_pos()
curpos=mspos_array
curcallition=True
elif event.type==pygame.KEYDOWN:
if event.key==K_a:
pass#print 'a'
return playPause,curcallition,curpos,Ecollition,CurClick_Animation
def WallColiition(Hero_pos,Hero_pos_v,angle_zf):
if Hero_pos[0]<Hero_0.get_width()/2 or Hero_pos[0]>WIDTH-Hero_0.get_width()/2:
Hero_pos_v[0]=-Hero_pos_v[0]
angle_zf=-angle_zf
if Hero_pos[1]<Hero_0.get_height()/2 or Hero_pos[1]>HEIGHT-Hero_0.get_height()/2:
Hero_pos_v[1]=-Hero_pos_v[1]
angle_zf=-angle_zf
return Hero_pos_v,angle_zf
def EachCollition():
if Hero_pos[0]-Hero_0_trans.get_width()/2>=curpos[0]-25 and Hero_pos[0]-Hero_0_trans.get_width()/2>=curpos[0]-25:
return False
if curpos[0]-25>=Hero_pos[0]-Hero_0_trans.get_width()/2 and curpos[0]-25>=Hero_pos[0]-Hero_0_trans.get_width()/2+Hero_0.get_width():
return False
if Hero_pos[1]-Hero_0_trans.get_height()/2>=curpos[1]-20 and Hero_pos[1]-Hero_0_trans.get_height()/2>=curpos[1]-20:
return False
if curpos[1]-20>=Hero_pos[1]-Hero_0_trans.get_height()/2 and curpos[1]-20>=Hero_pos[1]-Hero_0_trans.get_height()/2+Hero_0.get_height():
return False
return True
screen.fill([0,255,255])
pygame.display.flip()
while True:
#background
bg_tx=HEIGHT/bg_rect if HEIGHT%bg_rect==0 else HEIGHT/bg_rect+1
movebg_list=[0]*int(bg_tx)
bg_tx = int(bg_tx)
if bg_flag==1:
for i in range(int(bg_tx)):
r,g,b=0,random.randint(200,255),random.randint(200,255)
for j in range(WIDTH):
if movebg_list[i]<WIDTH:
movebg_list[i]+=10
pygame.draw.rect(screen,(r,g,b),(0,ds,movebg_list[i],bg_rect))
pygame.display.update()
pygame.time.delay(1)
else:
movebg_list[i]=WIDTH
list_color.append([r,g,b])
ds+=bg_rect
ds=0
bg_flag=0
else:
for i in range(bg_tx):
pygame.draw.rect(screen,list_color[i],(0,ds,WIDTH,bg_rect))
ds+=bg_rect
ds=0
#background
if Hero_create:
HeroRand=random.randint(0,ImgNames_index-1)
Hero_pos=[random.randint(Hero_0.get_width()/2,WIDTH-Hero_0.get_width()/2),random.randint(Hero_0.get_height()/2,HEIGHT-Hero_0.get_height()/2)]
Hero_create=False
Hero_life=True
hero_dx=random.randint(0,1)
hero_dy=random.randint(0,1)
Hero_0=pygame.image.load('images/%s'%ImgNames[HeroRand]).convert_alpha()
Hero_0=pygame.transform.scale(Hero_0,(int(1.5*Hero_0.get_width()),int(1.5*Hero_0.get_height())))
if Hero_life:
Hero_0_trans=pygame.transform.rotate(Hero_0,angle)
if angle>360 or angle<-360:
angle=0
angle+=angle_zf
screen.blit(Hero_0_trans,(Hero_pos[0]-Hero_0_trans.get_width()/2,Hero_pos[1]-Hero_0_trans.get_height()/2))
if hero_dx==0:
Hero_pos[0]-=Hero_pos_v[0]
else:
Hero_pos[0]+=Hero_pos_v[0]
if hero_dy==0:
Hero_pos[1]-=Hero_pos_v[1]
else:
Hero_pos[1]+=Hero_pos_v[1]
Hero_pos_v,angle_zf=WallColiition(Hero_pos,Hero_pos_v,angle_zf)
playPause,curcallition,curpos,Ecollition,CurClick_Animation=forcusecollition(playPause,curcallition,curpos,Ecollition,CurClick_Animation)
if Ecollition:#ecollition text
angle_zf=-angle_zf
Hero_life=False
Hero_create=True
Ecollition=False
#cursor Animation
if CurClick_Animation:
CurClick_Animation=False
#cursor Animation
#cursor
if curcallition:
if curpos[0]>=0 and curpos[1]>=0:
screen.blit(cur,(curpos[0]-25,curpos[1]-20))
#cursor
pygame.display.update()
pygame.time.delay(5)
游戏很有趣且可爱
好了,今天的游戏就介绍到这儿了,有空在来玩玩,pygame这个东西挺有趣的,有兴趣的可以和作者交流交流,就算交个朋友也是木得问题的哦。