前几天没什么事,用Python的Pygame写了个小游戏,但是今天出现了bug。
以下为我的代码:
import pygame
from pygame import *
from random import choice,randint
from sys import exit
class BackGround(sprite.Sprite):
def __init__(self):
global ww,wh
super().__init__()
self.image=Surface((ww,wh))
self.image.fill((10,150,70))
self.rect=self.image.get_rect()
self.rect.x=0
self.rect.y=0
class Player(sprite.Sprite):
def __init__(self):
global ww,wh
super().__init__()
self.image=Surface((50,50))
self.image.fill((0,255,255))
self.rect=self.image.get_rect()
self.rect.center=((ww//2,wh//2))
class Number(sprite.Sprite):
def __init__(self,s:str,x:int,y:int,index:int):
super().__init__()
self.image=font.Font(None,1000).render(s,True,(0,0,0),None)
self.image=transform.scale(self.image,(50,100))
self.rect=self.image.get_rect()
self.rect.center=(x,y)
self.index=index
self.name=s
def DeleteNumber(self):
self.index-=1
def Move(self):
global ww, wh
global NumberList, NumberCnt
global AllSprites
global Domain
keys = key.get_pressed()
if keys[K_a]:
self.rect.x += 1
elif keys[K_d]:
self.rect.x -= 1
elif keys[K_w]:
self.rect.y += 1
elif keys[K_s]:
self.rect.y -= 1
if self.rect.right < 0 - Domain or self.rect.left > ww + Domain or self.rect.bottom < 0 - Domain or self.rect.top > wh + Domain:
AllSprites.remove(NumberList[self.index])
NumberList.pop(self.index)
NumberCnt -= 1
if not self.index >= len(NumberList):
for i in range(self.index, len(NumberList), 1):
NumberList[i].DeleteNumber()
def Get(self):
global P
global AllSprites
global NumberList,NumberCnt
global PlayerThings
if sprite.collide_rect(NumberList[self.index],P):
keys=key.get_pressed()
if keys[K_g]:
PlayerThings[self.name]+=1
AllSprites.remove(NumberList[self.index])
del NumberList[self.index]
NumberCnt -= 1
if not self.index >= len(NumberList):
for i in range(self.index, len(NumberList), 1):
NumberList[i].DeleteNumber()
def update(self):
self.Move()
self.Get()
init()
ww=1000
wh=600
scr=display.set_mode((ww,wh))
display.set_caption("数学之美")
AllSprites=sprite.Group()
BG=BackGround()
P=Player()
AllSprites.add(BG)
AllSprites.add(P)
scr.fill((0,0,0))
display.update()
fps=175
clock=time.Clock()
NumberList=list()
NumberCnt=0
FpsTime=0
Domain=150
MaxNumberCnt=7
PlayerThings=dict()
AllThings="11111111111111111111111112222223+-*/"
for i in "".join(list(set(list(AllThings)))):
PlayerThings[i]=0
run=True
while run:
scr.fill((0,0,0))
for event in pygame.event.get():
if event.type==QUIT:
run=False
if event.type==KEYDOWN:
if event.key==K_ESCAPE:
run=False
if FpsTime/fps==3:
FpsTime=0
if NumberCnt<MaxNumberCnt:
t=Number(choice(AllThings),randint(0-Domain,ww+Domain),randint(0-Domain,wh+Domain),len(NumberList))
NumberList.append(t)
AllSprites.add(t)
NumberCnt+=1
AllSprites.draw(scr)
AllSprites.update()
clock.tick(fps)
display.update()
FpsTime+=1
quit()
exit()
报错内容:
Traceback (most recent call last):
File "C:\Users\浪矢秀一\Desktop\数学之美\main.py", line 113, in <module>
AllSprites.update()
File "C:\Users\浪矢秀一\Desktop\数学之美\venv\Script\Lib\site-packages\pygame\sprite.py", line 555, in update
sprite.update(*args, **kwargs)
File "C:\Users\浪矢秀一\Desktop\数学之美\main.py", line 72, in update
self.Get()
File "C:\Users\浪矢秀一\Desktop\数学之美\main.py", line 60, in Get
if sprite.collide_rect(NumberList[self.index],P):
~~~~~~~~~~^^^^^^^^^^^^
IndexError: list index out of range
Process finished with exit code 1
代码逻辑比较乱,自己也看不出什么来,可能是删除精灵时出现了问题,但个人感觉逻辑上没问题,不知道错在哪,希望大家指出,万分感谢。