学习python中,想用pygame写游戏,在运行过程中越来越卡顿,
按照网上说的fill刷新,但仍然有这个问题
import threading
import random
import time
import pygame
import sys
All_light=50
fall_light_num=0
all_zombie_list=[]
appear_zombies_list=[]
appear_plants_list=[]
fall_sun_list=[]
grass_xy=[[[290,130],[370,130],[450,130],[530,130],[610,130],[690,130],[770,130],[850,130],[930,130]],
[[290,225],[370,225],[450,225],[530,225],[610,225],[690,225],[770,225],[850,225],[930,225]],
[[290,325],[370,325],[450,325],[530,325],[610,325],[690,325],[770,325],[850,325],[930,325]],
[[290,425],[370,425],[450,425],[530,425],[610,425],[690,425],[770,425],[850,425],[930,425]],
[[290,520],[370,520],[450,520],[530,520],[610,520],[690,520],[770,520],[850,520],[930,520]]]
class plants(object):
def __init__(self,color,health,type,y,x):
self.color=color
self.health=health
self.type=type
self.y=y
self.x=x
def stop_zombie(self):
global appear_zombies_list
while True:
for i in appear_zombies_list:
if i.x==self.x and i.y==self.y and len(self.type)-i.z==1:
i.speed==0
def die(self):
while True:
if self.health==0:
del self
class xrk(plants):
def light(self):
global All_light
time.sleep(round(random.uniform(5,10),1))
if self.type=="向日葵":
All_light+=25
elif self.type=="双子向日葵":
All_light+=50
print(All_light)
time.sleep(round(random.uniform(23, 25), 1))
while True:
if self.type=="向日葵":
All_light += 25
elif self.type=="双子向日葵":
All_light += 50
time.sleep(round(random.uniform(23, 25), 1))
print(All_light)
def grow(self):
self.type="双子向日葵"
class wd(plants):
def __init__(self, color, health, type,y,x,hair):
self.color=color
self.health=health
self.type=type
self.x=x
self.y=y
self.hair=hair
def boom(self):
global appear_zombies_list
while True:
time.sleep(1.4)
mum_x_zombie = None
mum_x = 10
for i in appear_zombies_list:
if self.x-i.x<mum_x:
mum_x=i.x
mum_x_zombie=i
if mum_x_zombie!=None:
if self.type == "豌豆射手":
mum_x_zombie.health -= 20
elif self.type == "双发射手":
mum_x_zombie.health -= 20
time.sleep(0.3)
mum_x_zombie.health -= 20
elif self.type == "三发射手":
mum_x_zombie.health -= 20
time.sleep(0.3)
mum_x_zombie.health -= 20
time.sleep(0.3)
mum_x_zombie.health -= 20
def shake_head(self):
while True:
head_toward="left"
time.sleep(0.5)
head_toward="right"
class js(object):
def __init__(self,color,health,type,speed,x,y,z):
self.color=color
self.health=health
self.type=type
self.speed=speed
self.x=x
self.y=y
self.z=z
def walk(self):
while True:
time.sleep(round(random.uniform(0.8,1.4)))
self.y+=self.speed
def jump(self):
global appear_plants_list
while True:
for i in appear_plants_list:
if i.x == self.x and i.y == self.y and i.type!="高坚果":
self.y+=1
def eat(self):
while True:
if self.speed==0:
for i in appear_plants_list:
if i.y==self.y:
i.health-=100
time.sleep(1)
def die(self):
while True:
if self.health==0:
del self
class sun(object):
def __init__(self,x):
self.x=x
def sun_move2(self):
global screen
self.y=0
while True:
if self.y<500:
self.y+=1
time.sleep(0.01)
elif self.y>=500:
pass
def fall_light_time():
global fall_light_num
while True:
time.sleep(round(random.uniform(6.5,7.5)))
fall_light_num=1
def find_mum_xy(mouse_xy):
global grass_xy
mum_x_list_int = 11111111
mum_y_list_int = 11111111
mum_x=11111111
mum_y=11111111
for i in range(len(grass_xy)):
for j in range(len(grass_xy[i])):
if grass_xy[i][j][0]==mouse_xy[0]:
mum_x=0
mum_x_list_int=grass_xy[i][j][0]
elif grass_xy[i][j][0]>mouse_xy[0]:
if grass_xy[i][j][0] - mouse_xy[0] < mum_x:
mum_x=grass_xy[i][j][0] - mouse_xy[0]
mum_x_list_int = grass_xy[i][j][0]
elif grass_xy[i][j][0]<mouse_xy[0]:
if mouse_xy[0]-grass_xy[i][j][0]<mum_x:
mum_x=mouse_xy[0]-grass_xy[i][j][0]
mum_x_list_int = grass_xy[i][j][0]
if grass_xy[i][j][1] == mouse_xy[1]:
mum_y = 0
mum_y_list_int = grass_xy[i][j][1]
elif grass_xy[i][j][1] > mouse_xy[1]:
if grass_xy[i][j][1] - mouse_xy[1] < mum_y:
mum_y = grass_xy[i][j][1] - mouse_xy[1]
mum_y_list_int = grass_xy[i][j][1]
elif grass_xy[i][j][1] < mouse_xy[1]:
if mouse_xy[1] - grass_xy[i][j][1] < mum_y:
mum_y = mouse_xy[1] - grass_xy[i][j][1]
mum_y_list_int = grass_xy[i][j][1]
return [mum_x_list_int,mum_y_list_int]
pygame.init()
screen = pygame.display.set_mode((1100,600))
pygame.display.set_caption("植物大战僵尸")
img1=pygame.image.load("background1.jpg").convert()
img2=pygame.image.load("SeedBank.png").convert()
img3=pygame.image.load("Sun2.png").convert_alpha()
font_name = pygame.font.match_font('fangsong')
font = pygame.font.Font(font_name, 20)
font.set_bold(True)
a=xrk("yellow",300,"向日葵",1,1)
b=xrk("yellow",300,"双子向日葵",1,2)
c = threading.Thread(target=b.light)
c.start()
d = threading.Thread(target=a.light)
d.start()
e=threading.Thread(target=fall_light_time)
e.start()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==pygame.MOUSEBUTTONUP:
mouse_xy=pygame.mouse.get_pos()
mum_xy=find_mum_xy(list(mouse_xy))
print(mouse_xy)
print(mum_xy)
screen.fill('white')
screen.blit(img1, (0, 0))
screen.blit(img2, (250, 0))
if fall_light_num==1:
random_sun_x = random.randint(250, 980)
ad=screen.blit(img3, (random_sun_x, 0))
fall_sun_list.append("sun")
fall_sun_list[-1]=sun(random_sun_x)
ac=threading.Thread(target=fall_sun_list[-1].sun_move2)
ac.start()
fall_light_num=0
for p in fall_sun_list:
screen.blit(img3,(p.x,p.y))
font_surface = font.render(str(All_light), True, 'black')
if All_light==0:
screen.blit(font_surface, (282, 61))
elif All_light<100:
screen.blit(font_surface, (277, 61))
elif All_light<1000:
screen.blit(font_surface, (269, 61))
elif All_light<=9999:
screen.blit(font_surface, (264, 61))
pygame.display.flip()
background1.jpg