英文:
Adding a highscore
问题
我对Python编程和编程一般都还不太熟悉,但我一直在做一个学校项目。在这个项目中,你需要编写一个游戏。现在,我已经做得相当远了,但我一直在尝试添加一个高分,但它就是无法正常工作,这是我的代码:
```python
import pygame
import time
import random
pygame.init()
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
dis_width = 400
dis_height = 500
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('SNELIAS')
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 8
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def Your_score(score):
value = score_font.render("你的分数:" + str(score), True, yellow)
dis.blit(value, [0, 0])
def High_score(score):
value = score_font.render("最高分:" + str(score), True, yellow)
dis.blit(value, [0, 0])
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width / 6, dis_height / 3])
def gameLoop():
game_over = False
game_close = False
x1 = dis_width / 2
y1 = dis_height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
Score_high = 0
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
image = pygame.image.load("snakegrnd.jpeg")
screenUpdate = pygame.transform.scale(image, (dis_width, dis_height))
while not game_over:
dis.blit(screenUpdate, (0, 0))
while game_close == True:
message("你输了!按C-重新开始或Q-退出", white)
Your_score(Length_of_snake - 1)
if High_score < Length_of_snake - 1:
High_score = Length_of_snake - 1
High_score(High_score)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
if x1 + x1_change > dis_width:
x1 = 0
if x1 + x1_change < 0:
x1 = dis_width
if y1 + y1_change > dis_height:
y1 = 0
if y1 + y1_change < 0:
y1 = dis_height
x1 += x1_change
y1 += y1_change
pygame.draw.rect(dis, red, [foodx, foody, snake_block, snake_block])
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
our_snake(snake_block, snake_List)
Your_score(Length_of_snake - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
如果有人有任何想法,请让我知道,因为所有涉及到高分的部分都没有起作用。如果您有其他修改,请随意提出。
英文:
I am pretty new to coding in Python and coding in general and ive been working on a schoolproject. In this project you need to code a game. Now im am pretty far already but ive been trying to add a highscore but its just not coming to work, here is my code:
import pygame
import time
import random
pygame.init()
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
dis_width = 400
dis_height = 500
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('SNELIAS')
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 8
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def Your_score(score):
value = score_font.render("Your Score: " + str(score), True, yellow)
dis.blit(value, [0, 0])
def High_score(score):
value = score_font.render("Highscore: " + str(score), True, yellow)
dis.blit(value, [0, 0])
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width / 6, dis_height / 3])
def gameLoop():
game_over = False
game_close = False
x1 = dis_width / 2
y1 = dis_height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
Score_high = 0
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
image = pygame.image.load("snakegrnd.jpeg")
screenUpdate = pygame.transform.scale(image, (dis_width, dis_height))
while not game_over:
dis.blit(screenUpdate, (0, 0))
while game_close == True:
message("You Lost! Press C-Play Again or Q-Quit", white)
Your_score(Length_of_snake - 1)
if High_score < Length_of_snake - 1:
High_score = Length_of_snake - 1
High_score(High_score)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
if x1 + x1_change > dis_width:
x1 = 0
if x1 + x1_change < 0:
x1 = dis_width
if y1 + y1_change > dis_height:
y1 = 0
if y1 + y1_change < 0:
y1 = dis_height
x1 += x1_change
y1 += y1_change
pygame.draw.rect(dis, red, [foodx, foody, snake_block, snake_block])
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
our_snake(snake_block, snake_List)
Your_score(Length_of_snake - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
gameLoop()
if anyone has an idea please let me know because all the parts where highscore is placed has done nothing. If you have additional changes be my guest.
i have tried copying the code of "your score" because that does work.
答案1
得分: 1
请将以下部分替换为:
while game_close == True:
message("You Lost! Press C-Play Again or Q-Quit", white)
Your_score(Length_of_snake - 1)
if Score_high < Length_of_snake - 1:
Score_high = Length_of_snake - 1
High_score(Score_high)
pygame.display.update()
英文:
Make sure you're using the right variables
At the beginning of gameLoop
you define a variable called Score_high
. Then later you a variable named High_score
which is the function you defined earlier not the Score_high
variable. Try changing:
while game_close == True:
message("You Lost! Press C-Play Again or Q-Quit", white)
Your_score(Length_of_snake - 1)
if High_score < Length_of_snake - 1:
High_score = Length_of_snake - 1
High_score(High_score)
pygame.display.update()
to:
while game_close == True:
message("You Lost! Press C-Play Again or Q-Quit", white)
Your_score(Length_of_snake - 1)
if Score_high < Length_of_snake - 1:
Score_high = Length_of_snake - 1
High_score(Score_high)
pygame.display.update()
(This is a great example of why good naming practices are extremely important. You should name the function that shows the highscore something like show_highscore
, and name the variable that holds the highscore highscore
.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论