pygame 编写的小游戏
1.导入pygame及其他模块
import sys
import threading
import pygame
2建立使用类-人`
class ClassPeople:
colunm=0
row=0
speed=[0.1,0.1]
ball=pygame.image.load('D:\\图片\\黄色.PNG')
ballrect=ball.get_rect()
def __init__(self, c,r,s,s1):
self.colunm=c
self.row=r
self.ball=pygame.image.load(s)
self.speed=s1
self.ballrect=self.ball.get_rect()
self.ballrect.left=r
self.ballrect.top=c
def PeopleMove(self):
# global ballrect
# self.ballrect = self.ballrect.move(self.speed)
self.ballrect.left=self.ballrect.left+self.speed[0]
self.ballrect.top=self.ballrect.top+self.speed[1]
if self.ballrect.left < 0 or self.ballrect.right > width:
self.speed[0] = -self.speed[0]
if self.ballrect.top < 0 or self.ballrect.bottom > height:
self.speed[1] = -self.speed[1]
3.建立实用类-狗`
class ClassDOG:
colunm=0
row=0
speed=[0.1,0.1]
ball=pygame.image.load('D:\\图片\\黄色.PNG')
ballrect=ball.get_rect()
def __init__(self, c,r,s,s1):
self.colunm=c
self.row=r
self.ball=pygame.image.load(s)
self.speed=s1
self.ballrect=self.ball.get_rect()
self.ballrect.left=r
self.ballrect.top=c
def DOGMove(self):
#global ballrect
#self.ballrect = self.ballrect.move(self.speed)
self.ballrect.left=self.ballrect.left+self.speed[0]
self.ballrect.top=self.ballrect.top+self.speed[1]
if self.ballrect.left < 0 or self.ballrect.right > width:
self.speed[0] = -self.speed[0]
if self.ballrect.top < 0 or self.ballrect.bottom > height:
self.speed[1] = -self.speed[1]
4.将两个类实例化
#实列化人
numpeople =[]
for item in range(10):
numpeople.append(ClassPeople(item*50+100,999+item*4,'D:\\图片\\绿色.PNG',[1+item*0.5,1+item*0.1]))
for item in range(10):
print(numpeople[item])
print('---------'+str(numpeople[item].colunm))
#实例化狗
numDOG =[]
for item in range(10):
numDOG.append(ClassDOG(item*50+1,1,'D:\\图片\\黄色.PNG',[1.5+item*0.5,1.0+item*0.1]))
for item in range(10):
print(numDOG[item])
print('---------'+str(numDOG[item].colunm))
5.调用类中move函数。使用for_in_循环来实现多个同类的位置移动
def gamepeople():
for item in range(len(numpeople)):
numpeople[item].PeopleMove()
def gamedog():
for item in range(10):
numDOG[item].DOGMove()
6.类的消失。当人类和狗类左上角的位置坐标在±5内,就讲人类从numpeople =[]
移除。
def dis( num ):
dispeople=[]
for item in range(num):
for ite in range(10):
a=numpeople[item].ballrect.left-numDOG[ite].ballrect.left
b=numpeople[item].ballrect.top-numDOG[ite].ballrect.top
if a<5 and a>-5 and b<5 and b>-5:
if dispeople.count(numpeople[item])==0:
dispeople.append(numpeople[item])
for item in range(len(dispeople)):
numpeople.remove( dispeople[item])
print(dispeople)
7.主函数代码
def main(a):
a+=1
thread=[]
thred_my=threading.Thread(target=gamepeople())
thred_my1 = threading.Thread(target=gamedog())
thred_my.start()
thred_my1.start()
screen.fill(color)
for item in range(len(numpeople)):
screen.blit(numpeople[item].ball, numpeople[item].ballrect)
for item in range(10):
screen.blit(numDOG[item].ball, numDOG[item].ballrect)
pygame.display.flip()
dis( len(numpeople))
print(len(numpeople))
if len(numpeople)==0:#a>1800 or
event.type=pygame.QUIT#停止游戏
print(a)
return(a)在这里插入代码片
8.pygame初始化及参数设置
a=0
pygame.init()
size=width,height=1080,960
screen=pygame.display.set_mode(size)
color=(100,100,2)
clock=pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
a=main(a)
pygame.quit()
9.效果图