Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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 如何存储flappy bird game';将分数输入sql数据库_Python_Mysql_Sql_Pygame - Fatal编程技术网

Python 如何存储flappy bird game';将分数输入sql数据库

Python 如何存储flappy bird game';将分数输入sql数据库,python,mysql,sql,pygame,Python,Mysql,Sql,Pygame,我是pygame的初学者(使用PythonIdle),并制作了一个简单的游戏作为我的学校项目。它需要以某种方式与SQL数据库连接,我将分数与球员姓名一起存储在数据库中。但在完成一轮游戏并输入上述信息后,它在调用所需函数(GUI挂起)后不会重新启动,并继续要求输入玩家的姓名(在空闲状态下)。此外,该信息不会存储在SQL数据库中。我尝试将分数保存在文本文件中并打印输出,但同样的问题也出现了。代码如下: import pygame import random import mysql.connecto

我是pygame的初学者(使用PythonIdle),并制作了一个简单的游戏作为我的学校项目。它需要以某种方式与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()
导入pygame
随机输入
将mysql.connector作为c导入
#初始化pygame中的模块
pygame.init()
SCREEN=pygame.display.set_模式((500750))#设置显示
#背景
BACKGROUND\u IMAGE=pygame.IMAGE.load('BACKGROUND.jpg')
#鸟
BIRD_IMAGE=pygame.IMAGE.load('bird1.png')
鸟x=50
鸟_y=300
伯德·你的变化=0
def显示器(x,y):
屏幕。blit(鸟_图像,(x,y))
#障碍物
障碍物宽度=70
障碍物高度=random.randint(150450)
障碍物颜色=(211253117)
障碍物X变化=-4
障碍物x=500
def显示障碍物(高度):
pygame.draw.rect(屏幕,障碍物颜色,(障碍物x,0,障碍物宽度,高度))
底部障碍物高度=635-高度-150
pygame.draw.rect(屏幕,障碍物颜色,(障碍物x,635,障碍物宽度,-底部障碍物高度))
#碰撞检测
def碰撞检测(障碍物x、障碍物高度、鸟y、底部障碍物高度):

如果障碍物=50且障碍物则问题在于您没有完全重置游戏状态。因此,当新游戏开始时,玩家身下仍有障碍物

也许可以让您的
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

回答问题时忘记删除sql函数下的三个引号。仍然显示相同的问题。请研究使用参数化查询的正确方法。然后,请使用一些自动格式化程序-这将极大地帮助您识别问题。嗯,除了短绒,像派林或弗雷克8。然后,您应该尝试调试,尤其是单步调试代码,看看与预期有什么不同。将断点设置为在检测到冲突后播放,然后检查程序流…它仍然显示相同的输出。@Ira-什么输出?请将其复制到问题中。我根据您的建议进行了更改,但输出显示的问题与我之前在问题中提到的相同。