Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我试图在我的游戏中改变等级,但不起作用 随机导入 导入pygame pygame.font.init() pygame.init() #音乐代码 death\u Sound=pygame.mixer.Sound('death.wav')) food\u Sound=pygame.mixer.Sound('eat.wav')) music\u Sound=pygame.mixer.Sound('bg.wav')) victory\u Sound=pygame.mixer.Sound('win.wav')) pygame.mixer.music.load('bg.wav') pygame.混音器.音乐.播放(20) 类按钮(): 定义初始化(自): self.textboxs={} #----点击---- def clickedIn(自身、x、y、宽度、高度): 全局鼠标_状态,鼠标_x,鼠标_y 如果鼠标_状态==1,鼠标_x>=x,鼠标_x=y,鼠标_y(x+宽度),或鼠标_状态==1,鼠标_y(y+高度): 返回真值 #----盘旋---- def悬停(自身、x、y、宽度、高度): 全局鼠标_状态,鼠标_x,鼠标_y 如果mouse_state==0,mouse_x>=x,mouse_x=y,mouse_y self.length: self.tail.pop(0) 如果self.x==-1: self.alive=False self.x=0 如果self.x==(大小[0]/比例): self.alive=False self.x=(大小[0]/刻度)-1 如果self.y==-1: self.alive=False self.y=0 如果self.y==(大小[1]-100)/刻度: self.alive=False self.y=((大小[1]-100)/刻度)-1 def重置(自): self.alive=True self.length=1 self.tail.clear() self.x=0 self.y=0 self.xV=0 self.yV=1 self.tick=0 班级食物(): 定义初始化(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) def牵引(自): pygame.draw.rect(屏幕,绿色,((self.x*比例),(self.y*比例)+100,比例,比例)) def更新(自我): 如果snake.x==self.x和snake.y==self.y: self.reset() snake.length+=1 食物和声音播放 def重置(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) 类炸弹(): 定义初始化(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) def牵引(自): pygame.draw.rect(屏幕,黑色,((self.x*比例),(self.y*比例)+100,比例,比例)) def更新(自我): 如果snake.x==self.x和snake.y==self.y: snake.alive=False def重置(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) 开始-=1 def snake_win(): snake.alive=False 胜利之声 类实用工具(): 定义初始化(自): 返回 def牵引(自): text=font.render(“长度:”+str(snake.Length),真,黑色) text_y=text.get_rect().height 如果启动_Python_Function_Loops_Error Handling_Pygame - Fatal编程技术网

Python 我试图在我的游戏中改变等级,但不起作用 随机导入 导入pygame pygame.font.init() pygame.init() #音乐代码 death\u Sound=pygame.mixer.Sound('death.wav')) food\u Sound=pygame.mixer.Sound('eat.wav')) music\u Sound=pygame.mixer.Sound('bg.wav')) victory\u Sound=pygame.mixer.Sound('win.wav')) pygame.mixer.music.load('bg.wav') pygame.混音器.音乐.播放(20) 类按钮(): 定义初始化(自): self.textboxs={} #----点击---- def clickedIn(自身、x、y、宽度、高度): 全局鼠标_状态,鼠标_x,鼠标_y 如果鼠标_状态==1,鼠标_x>=x,鼠标_x=y,鼠标_y(x+宽度),或鼠标_状态==1,鼠标_y(y+高度): 返回真值 #----盘旋---- def悬停(自身、x、y、宽度、高度): 全局鼠标_状态,鼠标_x,鼠标_y 如果mouse_state==0,mouse_x>=x,mouse_x=y,mouse_y self.length: self.tail.pop(0) 如果self.x==-1: self.alive=False self.x=0 如果self.x==(大小[0]/比例): self.alive=False self.x=(大小[0]/刻度)-1 如果self.y==-1: self.alive=False self.y=0 如果self.y==(大小[1]-100)/刻度: self.alive=False self.y=((大小[1]-100)/刻度)-1 def重置(自): self.alive=True self.length=1 self.tail.clear() self.x=0 self.y=0 self.xV=0 self.yV=1 self.tick=0 班级食物(): 定义初始化(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) def牵引(自): pygame.draw.rect(屏幕,绿色,((self.x*比例),(self.y*比例)+100,比例,比例)) def更新(自我): 如果snake.x==self.x和snake.y==self.y: self.reset() snake.length+=1 食物和声音播放 def重置(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) 类炸弹(): 定义初始化(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) def牵引(自): pygame.draw.rect(屏幕,黑色,((self.x*比例),(self.y*比例)+100,比例,比例)) def更新(自我): 如果snake.x==self.x和snake.y==self.y: snake.alive=False def重置(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) 开始-=1 def snake_win(): snake.alive=False 胜利之声 类实用工具(): 定义初始化(自): 返回 def牵引(自): text=font.render(“长度:”+str(snake.Length),真,黑色) text_y=text.get_rect().height 如果启动

Python 我试图在我的游戏中改变等级,但不起作用 随机导入 导入pygame pygame.font.init() pygame.init() #音乐代码 death\u Sound=pygame.mixer.Sound('death.wav')) food\u Sound=pygame.mixer.Sound('eat.wav')) music\u Sound=pygame.mixer.Sound('bg.wav')) victory\u Sound=pygame.mixer.Sound('win.wav')) pygame.mixer.music.load('bg.wav') pygame.混音器.音乐.播放(20) 类按钮(): 定义初始化(自): self.textboxs={} #----点击---- def clickedIn(自身、x、y、宽度、高度): 全局鼠标_状态,鼠标_x,鼠标_y 如果鼠标_状态==1,鼠标_x>=x,鼠标_x=y,鼠标_y(x+宽度),或鼠标_状态==1,鼠标_y(y+高度): 返回真值 #----盘旋---- def悬停(自身、x、y、宽度、高度): 全局鼠标_状态,鼠标_x,鼠标_y 如果mouse_state==0,mouse_x>=x,mouse_x=y,mouse_y self.length: self.tail.pop(0) 如果self.x==-1: self.alive=False self.x=0 如果self.x==(大小[0]/比例): self.alive=False self.x=(大小[0]/刻度)-1 如果self.y==-1: self.alive=False self.y=0 如果self.y==(大小[1]-100)/刻度: self.alive=False self.y=((大小[1]-100)/刻度)-1 def重置(自): self.alive=True self.length=1 self.tail.clear() self.x=0 self.y=0 self.xV=0 self.yV=1 self.tick=0 班级食物(): 定义初始化(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) def牵引(自): pygame.draw.rect(屏幕,绿色,((self.x*比例),(self.y*比例)+100,比例,比例)) def更新(自我): 如果snake.x==self.x和snake.y==self.y: self.reset() snake.length+=1 食物和声音播放 def重置(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) 类炸弹(): 定义初始化(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) def牵引(自): pygame.draw.rect(屏幕,黑色,((self.x*比例),(self.y*比例)+100,比例,比例)) def更新(自我): 如果snake.x==self.x和snake.y==self.y: snake.alive=False def重置(自): self.x=random.randrange((大小[0]/刻度)-1) self.y=random.randrange((大小[1]-100)/刻度)-1) 开始-=1 def snake_win(): snake.alive=False 胜利之声 类实用工具(): 定义初始化(自): 返回 def牵引(自): text=font.render(“长度:”+str(snake.Length),真,黑色) text_y=text.get_rect().height 如果启动,python,function,loops,error-handling,pygame,Python,Function,Loops,Error Handling,Pygame,而不是递归调用game\u state.main\u game2()则需要设置game\u state.state==“main\u game2”: class实用工具(): # [...] def牵引(自): text=font.render(“长度:”+str(snake.Length),真,黑色) text_y=text.get_rect().height 如果启动main_game2中的代码,则该代码似乎与main_game2中的代码相同,只是main_game2添加了炸弹。这是正确的吗

而不是递归调用
game\u state.main\u game2()
则需要设置
game\u state.state==“main\u game2”

class实用工具():
# [...]
def牵引(自):
text=font.render(“长度:”+str(snake.Length),真,黑色)
text_y=text.get_rect().height

如果启动
main_game2
中的代码,则该代码似乎与
main_game2
中的代码相同,只是
main_game2
添加了炸弹。这是正确的吗?是的,因为这个想法是在每个关卡后逐渐增加更难的机制,而不是改变游戏状态。您将其作为函数调用,但在任何时候都不会更改“self.state='main_game',因此它总是设置为该值。因此,状态管理器总是将其视为“主游戏”,这使得它现在变得更有意义,但我不确定当我试图使其触发时,我将如何为selfstate切换到下一个游戏状态创造条件,特别是当游戏结束/玩家在工具更新中达到获胜所需的分数时获胜,检查分数并将游戏状态更改为main_game2。之后我应该做什么?
import random
import pygame
pygame.font.init()
pygame.init()


# MUSIC CODE

death_Sound = pygame.mixer.Sound('death.wav')
food_Sound = pygame.mixer.Sound('eat.wav')
music_Sound = pygame.mixer.Sound('bg.wav')
victory_Sound = pygame.mixer.Sound('win.wav')

pygame.mixer.music.load('bg.wav')
pygame.mixer.music.play(20)

class Button():
    def __init__(self):
        self.textBoxes = {}
        
    #----Clicked In----
    def clickedIn(self,x,y,width,height):
        global mouse_state, mouse_x, mouse_y
        if mouse_state == 1 and mouse_x >= x and mouse_x <= (x + width) and mouse_y >= y and mouse_y <= (y + height):
            return True

    #----Clicked Out----
    def clickedOut(self,x,y,width,height):
        global mouse_state, mouse_x, mouse_y
        if mouse_state == 1 and mouse_x < x or mouse_state == 1 and mouse_x > (x + width) or mouse_state == 1 and mouse_y < y or mouse_state == 1 and mouse_y > (y + height):
            return True

    #----Hovering----
    def hovering(self,x,y,width,height):
        global mouse_state, mouse_x, mouse_y
        if mouse_state == 0 and mouse_x >= x and mouse_x <= (x + width) and mouse_y >= y and mouse_y <= (y + height):
            return True
    
    #----Click Button----
    def clickButton(self,x,y,width,height,normalColor,hoverColor,textFont,text,textColor,stateHolding = False,stateVariable = 0,state = 1):
        if not self.clickedIn(x,y,width,height) and not self.hovering(x,y,width,height):
            pygame.draw.rect(screen,normalColor,(x,y,width,height))
        elif self.hovering(x,y,width,height):
            pygame.draw.rect(screen,hoverColor,(x,y,width,height))
        if stateHolding == True and stateVariable == state:
            pygame.draw.rect(screen,hoverColor,(x,y,width,height))
        buttonText = textFont.render(text,True,textColor)
        buttonText_x = buttonText.get_rect().width
        buttonText_y = buttonText.get_rect().height
        screen.blit(buttonText,(((x + (width / 2)) - (buttonText_x / 2)),((y + (height / 2)) - (buttonText_y / 2))))
        if self.clickedIn(x,y,width,height):
            return True

WHITE = (255,255,255)
GREY = (127,127,127)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
DGREEN = (0,127,0)



font = pygame.font.SysFont('Comic Sans MS',20)

# SCREEN/IMAGE CODE
size = (600,700)
screen_width = 600
screen_height = 700
screen = pygame.display.set_mode(size)
ready_text = pygame.image.load('start.png')

pygame.display.set_caption("Snake")
done = False

clock = pygame.time.Clock()
start = 3
scale = 30






screen.blit(ready_text,(screen_width/2 - 183.5,screen_height/2 -67))

class Snake():
    def __init__(self):
        self.alive = True
        self.length = 1
        self.tail = []
        self.x = 0
        self.y = 0
        self.xV = 0
        self.yV = 1
        self.tick = 0
    
    def draw(self):
        for section in self.tail:
            pygame.draw.rect(screen,WHITE,(((section[0]) * scale),((section[1]) * scale) + 100,scale,scale))
    
    def update(self):
        if self.alive == True:
            if self.tick == 10:
                self.x += self.xV
                self.y += self.yV
                for segment in self.tail:
                    if segment[0] == self.x and segment[1] == self.y:
                        self.alive = False
                self.tick = 0
                self.tail.append((self.x,self.y))
            else:
                self.tick += 1
            while len(self.tail) > self.length:
                self.tail.pop(0)
        if self.x == -1:
            self.alive = False
            self.x = 0
        if self.x == (size[0] / scale):
            self.alive = False
            self.x = (size[0] / scale) - 1
        if self.y == -1:
            self.alive = False
            self.y = 0
        if self.y == (size[1] - 100) / scale:
            self.alive = False
            self.y = ((size[1] - 100) / scale) - 1
        
    def reset(self):
        self.alive = True
        self.length = 1
        self.tail.clear()
        self.x = 0
        self.y = 0
        self.xV = 0
        self.yV = 1
        self.tick = 0
        

    

class Food():
    def __init__(self):
        self.x = random.randrange((size[0] / scale) - 1)
        self.y = random.randrange(((size[1] - 100) / scale) - 1)
    
    def draw(self):
        pygame.draw.rect(screen,GREEN,((self.x * scale),(self.y * scale) + 100,scale,scale))
    
    def update(self):
        if snake.x == self.x and snake.y == self.y:
            self.reset()
            snake.length += 1
            food_Sound.play()
           
            
    
    def reset(self):
        self.x = random.randrange((size[0] / scale) - 1)
        self.y = random.randrange(((size[1] - 100) / scale) - 1)
        
class Bombs():
    def __init__(self):
        self.x = random.randrange((size[0] / scale) - 1)
        self.y = random.randrange(((size[1] - 100) / scale) - 1)
    
    def draw(self):
        pygame.draw.rect(screen,BLACK,((self.x * scale),(self.y * scale) + 100,scale,scale))
    
    def update(self):
        if snake.x == self.x and snake.y == self.y:
            snake.alive = False
            
            
    def reset(self):
        self.x = random.randrange((size[0] / scale) - 1)
        self.y = random.randrange(((size[1] - 100) / scale) - 1)


start -= 1

def snake_win():
    snake.alive = False
    victory_Sound.play()

class Utility():
    def __init__(self):
        return
    
    def draw(self):
        text = font.render("Length: " + str(snake.length),True,BLACK)
        text_y = text.get_rect().height
        if start <= 0:
            snake.alive = False
        if (str(snake.length)) == '3':
            snake_win()
            if button.clickButton((size[0] / 2) - 75,25,150,50,GREEN,DGREEN,font,"You won!",WHITE,snake_win):
                snake.reset()
                food.reset()
                game_state.main_game2()
            return
        screen.blit(text,(90,(50 - (text_y / 2))))
        text = font.render("Alive: " + str(snake.alive),True,BLACK)
        text_y = text.get_rect().height
        screen.blit(text,(size[0] - 210,(50 - (text_y / 2))))
        pygame.draw.line(screen,BLACK,(0,100),(size[0],100),7)
        if snake.alive == False:
            death_Sound.play()
            if button.clickButton((size[0] / 2) - 75,25,150,50,GREEN,DGREEN,font,"Play Again",WHITE):
                snake.reset()
                food.reset()
                bombs.reset()
                utility.reset()
        for i in range(int(size[0] / scale) - 1):
            pygame.draw.line(screen,BLACK,(0,(100 + (i * scale) + scale)),(size[0],(100 + (i * scale) + scale)),3)
            pygame.draw.line(screen,BLACK,(((i * scale) + scale),100),(((i * scale) + scale),size[1]),3)
            
    def update(self):
        return

# general callback
button = Button()
snake = Snake()
food = Food()
utility = Utility()
bombs = Bombs()



class GameState():
    def __init__(self):
        self.state = 'intro'
        global mouse_state, mouse_x, mouse_y
        
    def intro(self):
        global mouse_state, mouse_x, mouse_y
        screen.fill(GREY)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.state = 'main_game'
                
                
                
        screen.blit(ready_text,(screen_width/2 - 183.5,screen_height/2 -67))
        pygame.display.flip()
        
    def main_game(self):
        global mouse_state, mouse_x, mouse_y

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_state = 1
                pygame.mouse.set_pos(mouse_x,mouse_y + 1)
            else:
                mouse_state = 0
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                    snake.yV = 0
                    snake.xV = -1
                if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                    snake.yV = 0
                    snake.xV = 1
                if event.key == pygame.K_UP or event.key == pygame.K_w:
                    snake.xV = 0
                    snake.yV = -1
                if event.key == pygame.K_DOWN or event.key == pygame.K_s:
                    snake.xV = 0
                    snake.yV = 1
        
        mouse_x = pygame.mouse.get_pos()[0]
        mouse_y = pygame.mouse.get_pos()[1]
        
        pygame.display.set_caption("Snake, FPS: " + str(clock.get_fps()))
        
        screen.fill(GREY)
        
        
        
        food.draw()
        snake.draw()
        utility.draw()
        snake.update()
        food.update()
        utility.update()
        
        pygame.display.flip()
        
    def main_game2(self):
        global mouse_state, mouse_x, mouse_y

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_state = 1
                pygame.mouse.set_pos(mouse_x,mouse_y + 1)
            else:
                mouse_state = 0
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                    snake.yV = 0
                    snake.xV = -1
                if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                    snake.yV = 0
                    snake.xV = 1
                if event.key == pygame.K_UP or event.key == pygame.K_w:
                    snake.xV = 0
                    snake.yV = -1
                if event.key == pygame.K_DOWN or event.key == pygame.K_s:
                    snake.xV = 0
                    snake.yV = 1
        
        mouse_x = pygame.mouse.get_pos()[0]
        mouse_y = pygame.mouse.get_pos()[1]
        
        pygame.display.set_caption("Snake, FPS: " + str(clock.get_fps()))
        
        screen.fill(GREY)
        
        
        
        food.draw()
        snake.draw()
        utility.draw()
        bombs.draw()
        snake.update()
        food.update()
        utility.update()
        bombs.update()
        
        pygame.display.flip()
        
        
    def state_manager(self):
        global mouse_state, mouse_x, mouse_y
        
        if self.state == 'intro':
            self.intro()
        if self.state == 'main_game':
            self.main_game()
        if self.state == 'main_game2':
            self.main_game2()

                    
game_state = GameState()
while not done:
    game_state.state_manager()
    clock.tick(50)

pygame.quit()