如何将flappy bird游戏的分数存储到sql数据库中

Posted

技术标签:

【中文标题】如何将flappy bird游戏的分数存储到sql数据库中【英文标题】:how to store flappy bird game's score into sql database 【发布时间】:2020-12-08 00:56:43 【问题描述】:

我是pygame的初学者(使用python idle)并制作了一个Flappy Bird的简单游戏作为我的学校项目。它需要以某种方式与 SQL 数据库连接,我将分数与玩家姓名一起存储在其中。但是在打完一局并输入上述信息后,调用所需的函数并没有重新启动(GUI挂起)并不断要求输入玩家的名字(空闲)。此外,信息不会存储在 SQL 数据库中。我尝试将分数保存在文本文件中并打印输出,但同样的问题也在发生。代码如下:

import pygame
import random
import mysql.connector as c

# Initialising the modules in pygame
pygame.init()

SCREEN = pygame.display.set_mode((500, 750))  # Setting the display

# background
BACKGROUND_IMAGE = pygame.image.load('background.jpg')

#  BIRD
BIRD_IMAGE = pygame.image.load('bird1.png')
bird_x = 50
bird_y = 300
bird_y_change = 0

def display_bird(x, y):
    SCREEN.blit(BIRD_IMAGE, (x, y))

# OBSTACLES
OBSTACLE_WIDTH = 70
OBSTACLE_HEIGHT = random.randint(150,450)
OBSTACLE_COLOR = (211, 253, 117)
OBSTACE_X_CHANGE = -4
obstacle_x = 500

def display_obstacle(height):
    pygame.draw.rect(SCREEN, OBSTACLE_COLOR, (obstacle_x, 0, OBSTACLE_WIDTH, height))
    bottom_obstacle_height = 635 - height - 150
    pygame.draw.rect(SCREEN, OBSTACLE_COLOR, (obstacle_x, 635, OBSTACLE_WIDTH, -bottom_obstacle_height))

# COLLISION DETECTION
def collision_detection (obstacle_x, obstacle_height, bird_y, bottom_obstacle_height):
    if obstacle_x >= 50 and obstacle_x <= (50 + 64):
        if bird_y <= obstacle_height or bird_y >= (bottom_obstacle_height - 64):
            return True
    return False

# SCORE
score = 0
SCORE_FONT = pygame.font.Font('freesansbold.ttf', 32)


def sql():
    m=c.connect(host="localhost",user="root",password= "password",database="flappy 
    bird")
    my=m.cursor()
    n=input('enter your name')
    r = maximum
    my.execute("insert into table(name, score) values(,'')".format(n,r))
    m.commit()
    my.execute("select * from table")
    for i in my:
      print(i)

def score_display(score):
    display = SCORE_FONT.render(f"Score: score", True, (255,255,255))
    SCREEN.blit(display, (10, 10))

# START SCREEN
startFont = pygame.font.Font('freesansbold.ttf', 32)
def start():
   # displays: "press space bar to start)
    display = startFont.render(f"PRESS SPACE BAR TO START", True, (255, 255, 255))
    SCREEN.blit(display, (20, 200))
    pygame.display.update()

# GAME OVER SCREEN
# This list will hold all of the scores
score_list = [0]

game_over_font1 = pygame.font.Font('freesansbold.ttf', 64)
game_over_font2 = pygame.font.Font('freesansbold.ttf', 32)

def game_over():
   # check for the maximum score
   global maximum
   maximum = max(score_list)
   #  "game over"
   display1 = game_over_font1.render(f"GAME OVER", True, (200,35,35))
   SCREEN.blit(display1, (50, 300))
   # shows your current score and your max score
   display2 = game_over_font2.render(f"SCORE: score MAX SCORE: maximum", True, 
   (255, 255, 255))
   SCREEN.blit(display2, (50, 400))
   #  If your new score is the same as the maximum then u reached a new high score
   if score == maximum:
    display3 = game_over_font2.render(f"NEW HIGH SCORE!!", True, (200,35,35))
    SCREEN.blit(display3, (80, 100))

running = True
# waiting is going to refer to our end or start screen
waiting = True
# set collision to false in the beginning so that we only see the start screen in 
the beginning
collision = False

while running:

    SCREEN.fill((0, 0, 0))

    # display the background image
    SCREEN.blit(BACKGROUND_IMAGE, (0, 0))

    # we will be sent into this while loop at the beginning and ending of each game
    while waiting:
        if collision:
            # If collision is True (from the second time onwards) we will see both the end screen and the start screen
            game_over()
            sql()
            start()
        
        
        
        else:
            # This refers to the first time the player is starting the game
            start()

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    #  If we press the space bar we will exit out of the waiting while loop and start to play the game
                    # we will also reset some of the variables such as the score and the bird's Y position and the obstacle's starting position
                    score = 0
                    bird_y = 300
                    obstacle_x = 500
                    #  to exit out of the while loop
                    waiting = False

            if event.type == pygame.QUIT:
                # in case we exit out make both running and waiting false
                waiting = False
                running = False

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            # If you press exit you exit out of the while loop and pygame quits
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                #  if you press spacebar you will move up
                bird_y_change = -6

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_SPACE:
                # when u release space bar you will move down automatically
                bird_y_change = 3

    # moving the bird vertically
    bird_y += bird_y_change
    # setting boundaries for the birds movement
    if bird_y <= 0:
        bird_y = 0
    if bird_y >= 571:
        bird_y = 571

    # Moving the obstacle
    obstacle_x += OBSTACE_X_CHANGE

    # COLLISION
    collision = collision_detection(obstacle_x, OBSTACLE_HEIGHT, bird_y, OBSTACLE_HEIGHT 
  + 150)

    if collision:
        # if a collision does occur we are gonna add that score to our list of scores and make waiting True
        score_list.append(score)
        waiting = True

    # generating new obstacles
    if obstacle_x <= -10:
        obstacle_x = 500
        OBSTACLE_HEIGHT = random.randint(200, 400)
        score += 1
    # displaying the obstacle
    display_obstacle(OBSTACLE_HEIGHT)

    # displaying the bird
    display_bird(bird_x, bird_y)

    # display the score
    score_display(score)


    # Update the display after each iteration of the while loop
    pygame.display.update()

# Quit the program

pygame.quit()

请告诉我问题以及如何解决。谢谢!

【问题讨论】:

提问时忘记去掉sql函数下的三个引号。仍然显示同样的问题。 请查看使用参数化查询的正确方法。然后,请使用一些自动格式化程序 - 这将极大地帮助您识别问题。好吧,除了 linter,比如 pylint 或 flake8。然后你应该尝试调试,尤其是单步调试你的代码,看看与你的期望相比有什么不同。在检测到碰撞后设置断点,播放,然后检查您的程序流程... 【参考方案1】:

问题是您没有完全重置游戏状态。所以当新游戏开始时,立即仍然玩家下方有障碍物。

也许让您的 start() 函数也重置所有这些字段:

def start():
    # displays: "press space bar to start)
    display = startFont.render(f"PRESS SPACE BAR TO START", True, (255, 255, 255))
    SCREEN.blit(display, (20, 200))
    pygame.display.update()

    # reset for new game
    score = 0
    bird_x = 50
    bird_y = 300
    bird_y_change = 0
    OBSTACLE_WIDTH = 70
    OBSTACLE_HEIGHT = random.randint(150,450)
    OBSTACLE_COLOR = (211, 253, 117)
    OBSTACE_X_CHANGE = -4
    obstacle_x = 500

【讨论】:

@Ira - 什么输出?请将其复制到问题中。 我按照您的建议进行了更改,但输出显示的问题与我之前在问题中提到的问题相同。

以上是关于如何将flappy bird游戏的分数存储到sql数据库中的主要内容,如果未能解决你的问题,请参考以下文章

Lua制作flappy bird

vb写程序:flappy bird

自主学习Flappy Bird游戏

如何在android studio项目中为类似于flappy bird的简单2D游戏保存高分?

前端flappy bird游戏练习

自己动手写游戏:Flappy Bird