求大楼帮忙检查代码!
import pygameimport random
import sys
WIDTH, HEIGHT = 1000, 600
WINDOW_RECT = pygame.rect.Rect(0, 0, WIDTH, HEIGHT)
TEXT_COLOR = (255, 255, 255)
class wu:
def __init__(self):
self.rect = pygame.rect.Rect(-10, -10, 1, 1)
class wu2:
def __init__(self, rect):
self.rect = rect
class kapai:
'''卡牌类'''
def __init__(self, name, yangguang):
self.name = name
self.yang = yangguang
self.img = pygame.image.load('imgs/KaPian.png')
self.img = pygame.transform.rotozoom(self.img, 0, 0.5)
self.font = pygame.font.SysFont('Arial', 12)
self.text = self.font.render(str(self.yang), True, (0, 0, 0))
self.images = {
'豌豆射手': pygame.image.load('imgs/zhiwu/wandousheshou/1.png'),
'向日葵': pygame.image.load('imgs/zhiwu/xiangrikui/1.png'),
'坚果墙': pygame.image.load('imgs/zhiwu/jianguoqiang/1.png')
}
self.rect = self.img.get_rect()
def show_kapian(self, x, y):
self.rect = self.img.get_rect()
self.image = self.images
self.image = pygame.transform.rotozoom(self.image, 0, 0.5)
self.image_rect = self.image.get_rect()
self.image_rect.x = x
self.image_rect.y = y
self.rect.center = self.image_rect.center
self.image_rect.y -= 3
self.text_rect = self.text.get_rect()
self.text_rect.centerx = self.rect.centerx - 5
self.text_rect.centery = self.rect.centery + 27
Main.window.blit(self.img, self.rect)
Main.window.blit(self.image, self.image_rect)
Main.window.blit(self.text, self.text_rect)
return self.rect
def show_zhiwu(self, x, y):
self.image = self.images
rect = self.image.get_rect()
rect.centerx = x
rect.centery = y
Main.window.blit(self.image, rect)
class zhiwu:
'''植物基类'''
def __init__(self):
pass
# 种植的方法
def zhongzhi(self):
pass
class WanDou_sheshou(zhiwu):
'''豌豆射手类'''
name = '豌豆射手'
yangguang = 100
imgs =
def __init__(self, lu):
self.xiabiao = 0
self.q = 1
self.rect = self.imgs.get_rect()
self.lu = lu
self.xue = 140
def show(self, x, y):
self.rect.x = x
self.rect.y = y
Main.window.blit(self.imgs, self.rect)
self.q += 1
if self.q % 5 == 0:
self.xiabiao += 1
if self.xiabiao == len(self.imgs):
self.xiabiao = 0
self.q = 1
class xiangrikui(zhiwu):
'''向日葵类'''
name = '向日葵'
yangguang = 50
imgs =
def __init__(self, lu):
self.xiabiao = 0
self.q = 1
self.rect = self.imgs.get_rect()
self.lu = lu
self.xue = 140
def show(self, x, y):
self.rect.x = x
self.rect.y = y
Main.window.blit(self.imgs, self.rect)
self.q += 1
if self.q % 5 == 0:
self.xiabiao += 1
if self.xiabiao == len(self.imgs):
self.xiabiao = 0
self.q = 1
class jianguo(zhiwu):
'''坚果墙类'''
name = '坚果墙'
yangguang = 50
imgs =
def __init__(self, lu):
self.xiabiao = 0
self.q = 1
self.rect = self.imgs.get_rect()
self.lu = lu
self.xue = 2000
def show(self, x, y):
self.rect.x = x
self.rect.y = y
Main.window.blit(self.imgs, self.rect)
self.q += 1
if self.q % 5 == 0:
self.xiabiao += 1
if self.xiabiao == len(self.imgs):
self.xiabiao = 0
self.q = 1
class jiangshi:
'''僵尸基类'''
def __init__(self):
pass
# 移动方法
def move(self):
pass
class PuTong(jiangshi):
'''普通僵尸类'''
def __init__(self, lu):
self.zhanshi =
self.imgs =
self.kenshi_img =
self.xia = 0
self.xiabiao = 0
self.ken = 0
self.r = 1
self.r2 = 1
self.rect = self.imgs.get_rect()
self.name = '普通僵尸'
self.lu = lu
self.xue = 100
self.gongji = 20
def go(self, x, y):
self.rect = self.imgs.get_rect()
self.rect.left = x
self.rect.bottom = y
Main.window.blit(self.imgs, self.rect)
self.r += 1
if self.r % 7 == 0:
self.r = 1
self.xiabiao += 1
if self.xiabiao == 21:
self.xiabiao = 0
def kenshi(self, x, y):
self.rect = self.kenshi_img.get_rect()
self.rect.left = x
self.rect.bottom = y
Main.window.blit(self.kenshi_img, self.rect)
self.r2 += 1
if self.r2 % 7 == 0:
self.r2 = 1
self.ken += 1
if self.ken == 20:
self.ken = 0
def showzhanShi(self, x, y, zan):
rect = self.zhanshi.get_rect()
rect.centerx = x
rect.centery = y
Main.window.blit(self.zhanshi, rect)
if zan % 5 == 0:
self.xia += 1
if self.xia == 10:
self.xia = 0
class Main:
'''主类'''
def __init__(self):
pygame.init()
# 设置长宽
Main.window = pygame.display.set_mode((WIDTH, HEIGHT))
# 设置标题
pygame.display.set_caption('植物大战僵尸Python版 - 元豪')
# 加载音乐
self.bgsound = pygame.mixer.Sound('music/bg.mp3')
self.startsound = pygame.mixer.Sound('music/start.mp3')
# 储存卡片的列表
self.kapian = []
# 加载背景图集
self.bgs = [
pygame.image.load('imgs/bg1.jpg'),
pygame.image.load('imgs/bg2.jpg')
]
# 加载小推车
self.tuiche = pygame.image.load('imgs/tuiche.png')
# 小推车的坐标
self.tuichepos = [, , , , ]
# 加载卡槽栏
self.kacao = pygame.image.load('imgs/KaCao.png')
self.kacao_rect = self.kacao.get_rect()
# 加载选择栏
self.xuanze = pygame.image.load('imgs/XuanZe.png')
self.xuan_rect = self.xuanze.get_rect()
# 加载好、准备、开始的图片和音乐
self.hao_img = pygame.image.load('imgs/hao.png')
self.hao_img_rect = self.hao_img.get_rect()
self.hao_img_rect.center = WINDOW_RECT.center
self.zhunbei_img = pygame.image.load('imgs/zhunbei.png')
self.kaishi_img = pygame.image.load('imgs/kaishi.png')
self.zhunbei_sound = pygame.mixer.Sound('music/ZhunBei.ogg')
# 初始化僵尸的坐标
self.jiangshi_init_pos = [, , , , ]
# 僵尸的列表
self.jiangshiList = []
# 僵尸坐标
self.jiangshi_pos = []
# 刷僵尸计时器
self.jiangshi_time = 1
# 检测5条路是否有僵尸
self.ifLu =
# 设置背景下标
self.bgindex = 0
# 设置背景图
self.bg = self.bgs
# 获取背景图的矩形
self.bg_rect = self.bg.get_rect()
# 游戏是否开始
self.open = False
# 是否选择完毕
self.go = False
# 是否可以放置植物
self.ifstart = False
# 是否已经选上了植物
self.zhongzhi = False
# 初始化选上植物的坐标
self.yix, self.yiy = -100, -100
# 如果已选择,记录种植的植物
self.yixuanze = None
# 初始化阳光
self.yangguang = 50000
# 设置背景x, y坐标
self.bgx, self.bgy = 0, 0
# 设置卡槽x, y坐标
self.kax, self.kay = 15, -self.kacao_rect.height
# 设置选择栏x, y坐标
self.xuanx, self.xuany = -self.xuan_rect.width, HEIGHT - self.xuan_rect.height
# 鼠标点击后创建一个小矩形
self.ju = wu()
# 储存卡片矩形的列表
self.ka_rect_list = []
# 储存已选择卡片的列表
self.yi_kapian = []
# 储存已选择卡片的矩形的列表
self.yi_kapian_rect = []
# 初始化第一个已选择卡片的坐标
self.kapos_x = 105
self.kapos_y = 25
# 指定已选择卡片坐标的列表
self.yi_pos = []
for i in range(6):
self.yi_pos.append()
self.kapos_x += 60
# 设置展示僵尸的数量
self.zhanshi_num = random.randint(5, 9)
# 设置展示僵尸的x, y坐标
self.zhanshi = [ for i in range(self.zhanshi_num)]
# 设置展示僵尸的列表
self.zhanshi_list =
# 已经种下的植物
self.yizhong = []
# 鼠标按下的坐标
self.moupos = []
# 已经种下的植物的坐标
self.yipos = []
# 以下属于暂用变量
self.numder = 0
self.ci = 1
self.ci2 = 0
self.r = 40
self.r2 = 152
self.o = []
self.xiabiao = 0
self.zan = 0
self.y = 0
self.b = 0
self.g = None
# 啃食频率
self.t = 1
# 初始化卡牌
self.ka = [
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang),
kapai(jianguo.name, jianguo.yangguang),
kapai(WanDou_sheshou.name, WanDou_sheshou.yangguang),
kapai(xiangrikui.name, xiangrikui.yangguang)
]
# 开始
def start(self):
self.bgsound.play(-1)
# 游戏主循环
while True:
# 设置循环次数
pygame.time.Clock().tick(60)
self.zan += 1
# 如果游戏还没有开始
# 则放置开始界面
if not self.open:
self.showOpen()
self.numder += 1
if self.numder in :
if self.numder == 40:
self.numder = 0
text = self.showFont('Please press the space bar to start')
self.font_rect = text.get_rect()
self.window.blit(text, (WIDTH / 2 - self.font_rect.width / 2, 570))
else:
# 放置开始界面
if self.ci > 0:
# 只运行一次,让背景音乐暂停,让准备音乐播放
self.ci -= 1
self.bgsound.stop()
self.startsound.play(-1)
# 展示背景
self.showBg(self.bgx, self.bgy)
if self.go:
# 如果游戏已经开始
# 放置卡槽
self.showYiXuan()
if self.b == 0:
self.b = 1
for j in range(len(self.yi_kapian)):
self.yi_kapian_rect.append(wu2(self.yi_kapian.show_kapian(self.yi_pos, self.yi_pos)))
for j in range(len(self.yi_kapian)):
# 循环遍历已选择的列表
# 让他们在合适位置放置
self.yi_kapian.show_kapian(self.yi_pos, self.yi_pos)
if self.bgx < -100:
# 将背景重新右移
# 直到 >= -100
self.bgx += 5
else:
# 背景已经移动完毕
if self.y <= 60:
self.window.blit(self.hao_img, self.hao_img_rect)
self.y += 1
elif self.y <= 120:
self.window.blit(self.zhunbei_img, self.hao_img_rect)
self.y += 1
elif self.y <= 180:
self.window.blit(self.kaishi_img, self.hao_img_rect)
self.y += 1
else:
for i in range(5):
self.window.blit(self.tuiche, (self.tuichepos, self.tuichepos))
self.ifstart = True
self.for_show_yi()
if self.yixuanze:
self.g = self.yixuanze.show_zhiwu(self.yix, self.yiy)
# print(self.jiangshi_time)
self.jiangshi_time += 1
# 900 15秒
if self.jiangshi_time % 900 == 0:
self.jiangshi_time = 1
lu = random.randint(0, 4)
self.jiangshi_pos.append(self.jiangshi_init_pos.copy())
self.jiangshiList.append(PuTong(lu + 1))
self.showJiangShi()
if self.y in :
self.zhunbei_sound.play()
else:
if self.bgx > -(self.bg_rect.width - WIDTH):
self.bgx -= 5
else:
self.for_showZhanshi(self.zan)
self.window.blit(self.xuanze, (self.xuanx, self.xuany))
self.showYiXuan()
if self.kay < 0:
self.kay += 3
else:
if self.xuanx < 0:
self.xuanx += 10
else:
if self.ci2 < 1:
self.ci2 += 1
for i in range(len(self.ka)):
self.o.append()
self.r += 60
if self.r == 40 + 60 * 7:
self.r = 40
self.r2 += 92
self.ka_rect_list.append(wu2(self.ka.show_kapian(self.o, self.o)))
for i in range(len(self.ka)):
self.ka.show_kapian(self.o, self.o)
if self.yi_kapian:
for j in range(len(self.yi_kapian)):
self.yi_kapian.show_kapian(self.yi_pos, self.yi_pos)
# 获取事件
for event in pygame.event.get():
# 退出
if event.type == pygame.QUIT:
self.end()
# 获取键盘按下事件
if event.type == pygame.KEYDOWN:
# 按下Esc键
if event.key == pygame.K_ESCAPE:
self.end()
if event.key == pygame.K_SPACE:
if not self.open:
self.open = True
# 获取鼠标点击事件
if event.type == pygame.MOUSEBUTTONDOWN:
if self.zhongzhi:
if self.yixuanze.yang <= self.yangguang:
self.moupos.append(event.pos)
t, t2 = self.ifx_and_y(self.moupos[-1], self.moupos[-1])
if not in self.yipos:
self.yipos.append()
print('已种植', self.yixuanze.name)
if t2 == 90:
lu = 1
elif t2 == 185:
lu = 2
elif t2 == 285:
lu = 3
elif t2 == 385:
lu = 4
elif t2 == 480:
lu = 5
if '豌豆射手' == self.yixuanze.name:
self.yizhong.append(WanDou_sheshou(lu))
elif '向日葵' == self.yixuanze.name:
self.yizhong.append(xiangrikui(lu))
elif '坚果墙' == self.yixuanze.name:
self.yizhong.append(jianguo(lu))
self.yangguang -= self.yixuanze.yang
self.yix, self.yiy = -100, -100
self.zhongzhi = False
self.yixuanze = None
elif self.ifstart:
pos_x, pos_y = event.pos
self.ju.rect = pygame.rect.Rect(pos_x, pos_y, 1, 1)
for i in range(len(self.yi_kapian_rect)):
if pygame.sprite.collide_rect(self.ju, self.yi_kapian_rect):
print('选择卡片', self.yi_kapian.name)
self.zhongzhi = True
self.yixuanze = self.yi_kapian
break
elif self.open:
x, y = event.pos
self.ju.rect = pygame.rect.Rect(x, y, 1, 1)
if self.ka_rect_list:
for i in self.ka_rect_list:
if len(self.yi_kapian) < 6:
if pygame.sprite.collide_rect(self.ju, i):
self.ka_rect_list.pop()
for j in range(len(self.ka_rect_list)):
if self.ka_rect_list == i:
break
else:
j = -1
self.yi_kapian.append(self.ka.pop(j))
# print(self.yi_kapian)
# print(len(self.yi_kapian))
else:
self.go = True
# 获取鼠标弹起事件
if event.type == pygame.MOUSEBUTTONUP:
pass
# 获取鼠标移动事件
if event.type == pygame.MOUSEMOTION:
if self.zhongzhi:
self.yix, self.yiy = event.pos
# 刷新界面
pygame.display.update()
# 判断植物是否僵尸与碰撞
def JiangshiAndZhiwu(self, jiangshi):
for i in range(len(self.yizhong)):
if self.yizhong.lu == jiangshi.lu:
if pygame.sprite.collide_rect(self.yizhong, jiangshi):
self.t += 1
if self.t == 5:
self.yizhong.xue -= jiangshi.gongji
print(self.yizhong.xue)
self.t = 1
return True
# 遍历僵尸列表展示僵尸
def showJiangShi(self):
for i in range(len(self.jiangshiList)):
if not self.JiangshiAndZhiwu(self.jiangshiList):
self.jiangshiList.go(self.jiangshi_pos, self.jiangshi_pos)
self.jiangshi_pos -= 0.5
if self.jiangshi_pos == -100:
self.jiangshi_pos.pop(i)
self.jiangshiList.pop(i)
break
else:
self.jiangshiList.kenshi(self.jiangshi_pos, self.jiangshi_pos)
# 判断植物要放下的位置
def ifx_and_y(self, x, y):
if x <= 245:
newx = 160
elif x <= 320:
newx = 245
elif x <= 400:
newx = 320
elif x <= 475:
newx = 400
elif x <= 565:
newx = 475
elif x <= 650:
newx = 565
elif x <= 725:
newx = 650
elif x <= 800:
newx = 725
else:
newx = 810
if y <= 180:
newy = 90
elif y <= 270:
newy = 185
elif y <= 375:
newy = 285
elif y <= 460:
newy = 385
else:
newy = 480
return (newx, newy)
# 遍历已经种下的植物的列表
def for_show_yi(self):
for i in range(len(self.yizhong)):
if self.yizhong.xue > 0:
self.yizhong.show(self.yipos, self.yipos)
o = []
p = []
for i in range(len(self.yizhong)):
if self.yizhong.xue < 0:
pass
else:
o.append(self.yizhong)
p.append(self.yipos)
self.yizhong = o
self.yipos = p
# 展示已选择卡牌
def showYiXuan(self):
self.window.blit(self.kacao, (self.kax, self.kay))
font = pygame.font.SysFont('Arial', 15)
text = font.render(str(self.yangguang), True, (0, 0, 0))
text_rect = text.get_rect()
text_rect.y = self.kay + 65
text_rect.x = self.kax + 20
self.window.blit(text, text_rect)
# 循环遍历展示僵尸列表并展示
def for_showZhanshi(self, zan):
for zhanshi in self.zhanshi_list:
zhanshi.showzhanShi(self.zhanshi, self.zhanshi, zan)
# 展示背景
def showBg(self, x, y):
self.window.blit(self.bg, (x, y))
# 展示开始界面
def showOpen(self):
self.img = pygame.image.load('imgs/startBg.jpg')
self.window.blit(self.img, (0, 0))
# 展示文字
def showFont(self, text):
font = pygame.font.SysFont('Arial', 15)
text2 = font.render(text, True, TEXT_COLOR)
return text2
# 结束
def end(self):
pygame.quit()
sys.exit()
if __name__ == '__main__':
Main().start()
你好,鱼油!你的代码中存在几个潜在的问题。我会逐个指出并给出相应的建议。
1. **导入模块**
import pygame
import random
import sys
这些导入是正确的,但代码中似乎没有地方使用到 `random` 和 `sys` 模块。如果没有用到,可以删除节省资源。
2. **拼写错误**
WINDOW_RECT = pygame.rect.Rect(0, 0, WIDTH, HEIGHT)
`pygame.rect.Rect` 应该改为 `pygame.Rect`。
3. **类命名规则**
按照 Python 的命名规范,类名应该使用驼峰命名法。
class wu:
...
class wu2:
...
class kapai:
...
可以改为:
class Wu:
...
class Wu2:
...
class KaPai:
...
4. **`show_kapian` 和 `show_zhiwu`方法的问题**
def show_kapian(self, x, y):
self.rect = self.img.get_rect()
self.image = self.images
self.image = pygame.transform.rotozoom(self.image, 0, 0.5)
self.image_rect = self.image.get_rect()
self.image_rect.x = x
self.image_rect.y = y
self.rect.center = self.image_rect.center
self.image_rect.y -= 3
self.text_rect = self.text.get_rect()
self.text_rect.centerx = self.rect.centerx - 5
self.text_rect.centery = self.rect.centery + 27
Main.window.blit(self.img, self.rect)
Main.window.blit(self.image, self.image_rect)
Main.window.blit(self.text, self.text_rect)
return self.rect
def show_zhiwu(self, x, y):
self.image = self.images
rect = self.image.get_rect()
rect.centerx = x
rect.centery = y
Main.window.blit(self.image, rect)
`self.images` 是一个字典,在 `show_kapian` 和 `show_zhiwu` 中你直接赋给 `self.image` 变量,这样会报错。需要从字典中选择一个特定的图像。
5. **更正后的`show_kapian`和`show_zhiwu`方法可以这样实现:**
def show_kapian(self, x, y):
self.rect.topleft = (x, y)# To position the KaPai card
Main.window.blit(self.img, self.rect)
zhiwu_image = self.images# Get the specific plant image
zhiwu_image = pygame.transform.rotozoom(zhiwu_image, 0, 0.5)
zhiwu_rect = zhiwu_image.get_rect(center=(self.rect.centerx, self.rect.top - 3))
Main.window.blit(zhiwu_image, zhiwu_rect)
self.text_rect = self.text.get_rect()
self.text_rect.centerx = self.rect.centerx - 5
self.text_rect.centery = self.rect.centery + 27
Main.window.blit(self.text, self.text_rect)
return self.rect
def show_zhiwu(self, x, y):
zhiwu_image = self.images
rect = zhiwu_image.get_rect(center=(x, y))
Main.window.blit(zhiwu_image, rect)
6. **未定义`Main`类和`window`属性**
需要确保存在一个 `Main` 类并且其中有 `window` 这个类属性,否则会报错。
希望这些建议对你有帮助!如果还有其他问题,随时提问。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2024-9-1 17:05
你好,鱼油!你的代码中存在几个潜在的问题。我会逐个指出并给出相应的建议。
1. **导入模块**
可以给我完整的代码吗? 小甲鱼303 发表于 2024-09-01 17:46
可以给我完整的代码吗?
由于篇幅限制,我这里只能提供部分代码,并指出一些主要的问题。你需要根据提示自行进行完整的修复。
### 主要问题
1. 不完整的代码:很多地方有未定义的变量或列表,例如`IMGs`,`self.bgs`等。
2. 格式问题:缩进和代码样式需要进一步调整。
3. 错误的图像或声音文件引用:需要确保你提供的路径和文件是正确的。
### 改进后的部分代码示例
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 出什么问题了,报错是什么?
还有,下次麻烦使用代码格式发,不然我们运行不了{:10_254:}
页:
[1]