Ich möchte die automatische Aktualisierung des folgenden Codes beheben:Wie behebt man die automatische Aktualisierung der Partitur in 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
dies in der Partitur ist immer unter updation, möchte ich dieses Problem beheben auch, dass, wenn die Schlange mit dem Essen nicht kollidieren, das Essen Bewegen Sie sich manchmal in eine zufällige Position. Wie behebe ich diese zwei Fehler? Hier sind die 2 Bilder im Code verwendet.
icon1
Food
Bitte ein paar weitere Details angeben. Ihr Code ist lang und größtenteils unkommentiert. Bitte teilen Sie uns auch mit, welche Lösungen Sie bereits ausprobiert haben und was nicht. Zum Beispiel, auf den ersten Blick scheint es, dass die Punktzahl zunimmt, weil Ihr Spieler das Objekt berührt, das ihm Punkte gibt. Haben Sie versucht, die alte Position zu speichern und mit der neuen zu vergleichen, um festzustellen, ob überhaupt Bewegung aufgetreten ist, und nur dann die Punktezahl zu aktualisieren? – CherryDT
Ich habe die Lösung, danke trotzdem CherryDt. –