2016-05-04 45 views
1

我要解決下面的代碼的自動更新:如何修復pygame中的分數自動更新?

import pygame 
import time 
import random 

pygame.init() 

pygame.mixer.music.load('song.mp3') 
pygame.mixer.music.play(0) 
white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,155,0) 

display_width = 800 
display_height = 600 

gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('KALM Creation') 


#Score 
score=0 

#img=pygame.image.load("1.png") 
#intro=pygame.image.load("intro.png") 
foodimg=pygame.image.load("food.png") 

#Our Icon For The Game 
icon=pygame.image.load('icon1.jpg') 
pygame.display.set_icon(icon) 


clock = pygame.time.Clock() 

AppleThickness=30 
block_size = 10 
FPS = 30 

smallfont = pygame.font.SysFont("comicsansms", 25) 
medfont = pygame.font.SysFont("comicsansms", 50) 
largefont = pygame.font.SysFont("comicsansms", 80) 

#The score function 

def scoredisplay(scoredef=0): 
    text=smallfont.render("Score :%s" %(scoredef) ,True ,black) 
    gameDisplay.blit(text,[0,0]) 
#The random function of apple 
def randAppleGen(): 
    randAppleX = round(random.randrange(0, display_width-AppleThickness)) 
    randAppleY = round(random.randrange(0, display_height-AppleThickness)) 

    return randAppleX,randAppleY 
randAppleX,randAppleY=randAppleGen() 

#Starting Of the game 
def game_intro(): 

    intro = True 
    while intro:  
     for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      quit() 
     if event.type == pygame.KEYDOWN: 
      if event.key ==pygame.K_c: 
       intro = False 
      if event.key ==pygame.K_q: 
       pygame.quit() 
       quit() 
     gameDisplay.fill(white) 
     message_to_screen("Welcome To Eat it Game", 
          green, 
          -200, 
          size="medium") 
     message_to_screen("This Game is created by KALM Creations", 
          black, 
          -30, 
          size="small") 
     message_to_screen("Eat the food to gain a score!", 
          black, 
          10, 
          size="small") 
     message_to_screen("Avoid the rock , if you hit them you lose!", 
          black, 
          50, 
          size="small") 
     message_to_screen("Press 'C' to play the game or 'Q' to quit.", 
          black, 
          150, 
          size="small") 
     pygame.display.update() 
     clock.tick(15) 
#Snake aka the object which is moving (in green colour) 
def snake(lead_x,lead_y,block_size): 
    pygame.draw.rect(gameDisplay, green, [lead_x,lead_y,block_size,block_size]) 

#Text Size 
def text_objects(text,color, size): 
    if size=="small": 
     textSurface=smallfont.render(text, True ,color) 
    elif size=="medium": 
     textSurface=medfont.render(text, True ,color) 
    elif size=="large": 
     textSurface=largefont.render(text, True ,color) 


    return textSurface,textSurface.get_rect() 

#Message to screen 
def message_to_screen(msg,color,y_displace=0,size="small"): 
    textSurf,textRect=text_objects(msg,color,size) 
    textRect.center = (display_width/2),(display_height/2)+y_displace 
    gameDisplay.blit(textSurf,textRect) 

# The game run up 
def gameLoop(): 
    score=0 
    gameExit = False 
    gameOver = False 

    lead_x = display_width/2 
    lead_y = display_height/2 

    lead_x_change = 0 
    lead_y_change = 0 

    randAppleX,randAppleY=randAppleGen() 


    while not gameExit: 

     while gameOver == True: 

      gameDisplay.fill(white) 
      message_to_screen("Game over", 
           red, 
           y_displace=-50, 
           size="large") 
      message_to_screen("Press C to play again or Q to quit", 
           black, 
           y_displace=50, 
           size="medium") 

      pygame.display.update() 

      for event in pygame.event.get(): 
       if event.type == pygame.KEYDOWN: 
        if event.key == pygame.K_q: 
         gameExit = True 
         gameOver = False 
        if event.key == pygame.K_c: 
         gameLoop() 


     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       gameExit = True 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_LEFT: 
        lead_x_change = -block_size 
        lead_y_change = 0 
       elif event.key == pygame.K_RIGHT: 
        lead_x_change = block_size 
        lead_y_change = 0 
       elif event.key == pygame.K_UP: 
        lead_y_change = -block_size 
        lead_x_change = 0 
       elif event.key == pygame.K_DOWN: 
        lead_y_change = block_size 
        lead_x_change = 0 

     if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0: 
      gameOver = True 



     gameDisplay.blit(foodimg,(randAppleX,randAppleY)) 

     lead_x += lead_x_change 
     lead_y += lead_y_change 

     gameDisplay.fill(white) 
     gameDisplay.blit(foodimg,(randAppleX,randAppleY)) 

     snake(lead_x,lead_y,block_size) 
     scoredisplay() 
     pygame.display.update() 



     if lead_x > randAppleX and lead_x < randAppleY + AppleThickness or lead_x + block_size>randAppleX and lead_x + block_size< randAppleX + AppleThickness: 
      if lead_y > randAppleY and lead_y< randAppleY + AppleThickness or lead_y + block_size>randAppleY and lead_y + block_size< randAppleY + AppleThickness: 
       randAppleX,randAppleY=randAppleGen() 
       score+=1 
       scoredisplay(score) 
      elif lead_y +block_size >randAppleY and lead_y +block_size < randAppleY: 
       randAppleX,randAppleY=randAppleGen() 
       score+=1 
       scoredisplay(score) 
      ''' 
     Now when i run this game , the score keeps on updating.I want it to do like when the snake collides with 'foodimg', 
     it should add +1 to the score.Also sometimes even if the snake does not touch the food , the food is moved to a random 
     position.How do i fix this bug? 
     ''' 
     scoredisplay(score) 
     pygame.display.update() 

     clock.tick(FPS) 

    pygame.quit() 
    quit() 

game_intro() 
gameLoop() 
#Episode 36 

在這個比分始終處於更新用,我想也是解決這個問題,當蛇不與食品,食品碰撞有時會移動到一個隨機的位置。我該如何解決這兩個錯誤?下面是代碼中使用的2個圖像。
icon1
Food

+0

請包括一些更多的細節。你的代碼很長,大部分都沒有註釋。還請告訴我們你已經嘗試了哪些解決方案,以及哪些解決方案不適用於他們。例如,從第一眼看起來,分數不斷增加,因爲玩家會一直接觸讓他們得分的物體。您是否嘗試保存舊的位置,並將其與新的位置進行比較以檢測移動是否首先發生,然後才更新分數? – CherryDT

+0

我得到了解決方案,謝謝CherryDt。 –

回答

0

你是在一個點上調用scoredisplay()沒有輸入和在其他地方不必要的調用它。下面是修改代碼:

import pygame 
import time 
import random 

pygame.init() 

pygame.mixer.music.load('song.mp3') 
pygame.mixer.music.play(0) 
white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,155,0) 

display_width = 800 
display_height = 600 

gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('KALM Creation') 


#Score 
score=0 

#img=pygame.image.load("1.png") 
#intro=pygame.image.load("intro.png") 
foodimg=pygame.image.load("food.png") 

#Our Icon For The Game 
icon=pygame.image.load('icon1.jpg') 
pygame.display.set_icon(icon) 


clock = pygame.time.Clock() 

AppleThickness=30 
block_size = 10 
FPS = 30 

smallfont = pygame.font.SysFont("comicsansms", 25) 
medfont = pygame.font.SysFont("comicsansms", 50) 
largefont = pygame.font.SysFont("comicsansms", 80) 

#The score function 

def scoredisplay(scoredef=0): 
    text=smallfont.render("Score :%s" %(scoredef) ,True ,black) 
    gameDisplay.blit(text,[0,0]) 
#The random function of apple 
def randAppleGen(): 
    randAppleX = round(random.randrange(0, display_width-AppleThickness)) 
    randAppleY = round(random.randrange(0, display_height-AppleThickness)) 

    return randAppleX,randAppleY 
randAppleX,randAppleY=randAppleGen() 

#Starting Of the game 
def game_intro(): 

    intro = True 
    while intro:  
     for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      quit() 
     if event.type == pygame.KEYDOWN: 
      if event.key ==pygame.K_c: 
       intro = False 
      if event.key ==pygame.K_q: 
       pygame.quit() 
       quit() 
     gameDisplay.fill(white) 
     message_to_screen("Welcome To Eat it Game", 
          green, 
          -200, 
          size="medium") 
     message_to_screen("This Game is created by KALM Creations", 
          black, 
          -30, 
          size="small") 
     message_to_screen("Eat the food to gain a score!", 
          black, 
          10, 
          size="small") 
     message_to_screen("Avoid the rock , if you hit them you lose!", 
          black, 
          50, 
          size="small") 
     message_to_screen("Press 'C' to play the game or 'Q' to quit.", 
          black, 
          150, 
          size="small") 
     pygame.display.update() 
     clock.tick(15) 
#Snake aka the object which is moving (in green colour) 
def snake(lead_x,lead_y,block_size): 
    pygame.draw.rect(gameDisplay, green, [lead_x,lead_y,block_size,block_size]) 

#Text Size 
def text_objects(text,color, size): 
    if size=="small": 
     textSurface=smallfont.render(text, True ,color) 
    elif size=="medium": 
     textSurface=medfont.render(text, True ,color) 
    elif size=="large": 
     textSurface=largefont.render(text, True ,color) 


    return textSurface,textSurface.get_rect() 

#Message to screen 
def message_to_screen(msg,color,y_displace=0,size="small"): 
    textSurf,textRect=text_objects(msg,color,size) 
    textRect.center = (display_width/2),(display_height/2)+y_displace 
    gameDisplay.blit(textSurf,textRect) 

# The game run up 
def gameLoop(): 
    score=0 
    gameExit = False 
    gameOver = False 

    lead_x = display_width/2 
    lead_y = display_height/2 

    lead_x_change = 0 
    lead_y_change = 0 

    randAppleX,randAppleY=randAppleGen() 


    while not gameExit: 

     while gameOver == True: 

      gameDisplay.fill(white) 
      message_to_screen("Game over", 
           red, 
           y_displace=-50, 
           size="large") 
      message_to_screen("Press C to play again or Q to quit", 
           black, 
           y_displace=50, 
           size="medium") 

      pygame.display.update() 

      for event in pygame.event.get(): 
       if event.type == pygame.KEYDOWN: 
        if event.key == pygame.K_q: 
         gameExit = True 
         gameOver = False 
        if event.key == pygame.K_c: 
         gameLoop() 


     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       gameExit = True 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_LEFT: 
        lead_x_change = -block_size 
        lead_y_change = 0 
       elif event.key == pygame.K_RIGHT: 
        lead_x_change = block_size 
        lead_y_change = 0 
       elif event.key == pygame.K_UP: 
        lead_y_change = -block_size 
        lead_x_change = 0 
       elif event.key == pygame.K_DOWN: 
        lead_y_change = block_size 
        lead_x_change = 0 

     if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0: 
      gameOver = True 



     gameDisplay.blit(foodimg,(randAppleX,randAppleY)) 

     lead_x += lead_x_change 
     lead_y += lead_y_change 

     gameDisplay.fill(white) 
     gameDisplay.blit(foodimg,(randAppleX,randAppleY)) 

     snake(lead_x,lead_y,block_size) 
     pygame.display.update() 



     if lead_x > randAppleX and lead_x < randAppleY + AppleThickness or lead_x + block_size>randAppleX and lead_x + block_size< randAppleX + AppleThickness: 
      if lead_y > randAppleY and lead_y< randAppleY + AppleThickness or lead_y + block_size>randAppleY and lead_y + block_size< randAppleY + AppleThickness: 
       randAppleX,randAppleY=randAppleGen() 
       score+=1 
      elif lead_y +block_size >randAppleY and lead_y +block_size < randAppleY: 
       randAppleX,randAppleY=randAppleGen() 
       score+=1 

     scoredisplay(score) 
     pygame.display.update() 

     clock.tick(FPS) 

    pygame.quit() 
    quit() 

game_intro() 
gameLoop() 
#Episode 36 

如果你想如果沒有收集,建立具有計數每滴答定時器來移動目標。如果計數器高於某個數字,則移動目標,如果玩家收集目標並及時重置計數器。

希望這有助於:)

+0

Thanks.Can你可以告訴我如何在遊戲中添加敵人跟隨蛇,所以當蛇與敵人碰撞時,遊戲結束。 –

+0

有很多方法可以做到這一點,你必須自己解決:)。我推薦的一件事是學習類。當程序包含諸如蛇(玩家)和敵人(AI)等不同的對象時,類可以極大地簡化代碼。 –