【问题标题】:Trying to get the direction printed continuosly until another key is pressed试图连续打印方向,直到按下另一个键
【发布时间】:2020-04-18 18:34:50
【问题描述】:

我已经制作了这个蛇游戏(在教程的帮助下)并改变了竞技场的形状,我想打印连续按下的键的方向,直到蛇改变方向,如果这有意义的话。我的目标是重新制作蛇的路径,以便在游戏结束时跟踪蛇的整体运动(如果您有更好的想法,请提供帮助)。作为第一步,我认为“while”函数对此很有用,但是,它似乎不起作用。如果有人可以提供帮助,那就太好了。一切顺利。

import pygame
import time
import random
import threading
pygame.init()
from random import choice
blue = (0,0,255)
purple = (155, 0 , 155)
green = (0,225,200)
red = (255,0,0)
black = (0,0,0)
white = (255, 255, 255)
brown = (200, 180, 0)
dbrown = (180, 180, 0)
cream = (255, 255, 180)
screen = pygame.display.init()
dis_width = 840
dis_height = 640


dis=pygame.display.set_mode((dis_width,dis_height))
pygame.display.set_caption('patricks best attempt at making a snake game')

clock = pygame.time.Clock()

snake_block = 10
dis_bed = 200 - 240 
clock = pygame.time.Clock()
snake_speed = 20

font_style = pygame.font.SysFont(None, 50)
xchc= choice([i for i in range(1,840) if i not in [200,210,220,230,240]])
score_font = pygame.font.SysFont("bahnschrift", 35)
def Your_score(score):
    value = score_font.render("your score" + str(score), True, black)
    dis.blit(value, [0,0])
def our_snake(snake_block, snake_list):
    for x in snake_list:
        pygame.draw.rect(dis,blue, [x[0], x[1], snake_block, snake_block])

def message(msg, color):
    mesg = font_style.render(msg, True, color)
    dis.blit(mesg, [dis_width/3, dis_height/2])





def gameLoop(): 
    game_over = False
    game_close = False

    x1 = 10
    y1 = dis_height -10

    x1_change = 0
    y1_change = 0 

    snake_List = []
    Length_of_snake = 1

    foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
    foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
    while not game_over:


        while game_close == True:
            dis.fill(white)
            message("You lost q to quit, c to play", red)
            Your_score(Length_of_snake - 1)
            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
                    while event.key == pygame.K_LEFT:
                        print("Left")
                        if event.key != pygame.K_LEFT:
                            break 
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block
                    y1_change = 0
                    print ("right")
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block
                    x1_change = 0
                    print ("up")
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block
                    x1_change = 0
                    print ("down")


        if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
            game_close = True

        elif 0 <= y1 and y1 <= 310 and 200 <= x1 and x1 <= 420:
            game_close = True
        elif 0 <= y1 and y1 <= 40 and 100 <= x1 and x1 <= 180:
            game_close = True
        elif 0 <= y1 and y1 <= 100 and 520 <= x1 and x1 <= 700:
            game_close = True
        elif 560 <= y1 and y1 <= 640 and 640 <= x1 and x1 <= 840:
            game_close = True
        elif 520 <= y1 and y1 <= 640 and 260 <= x1 and x1 <= 510:
            game_close = True




        x1 += x1_change
        y1 += y1_change
        dis.fill(cream)

        pygame.draw.rect(dis,black,[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)

        pygame.draw.rect(dis, blue, [x1, y1, snake_block, snake_block])
        pygame.draw.rect(dis, purple, [200,0,240,320])
        pygame.draw.rect(dis, brown, [100, 0, 80, 40])
        pygame.draw.rect(dis, dbrown, [520, 0, 180, 100])
        pygame.draw.rect(dis, brown, [640, 560, 200, 80])
        pygame.draw.rect(dis, dbrown, [260, 520, 260, 120])    
        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(80, 640 - snake_block)/10.0) * 10.0
            Length_of_snake += 1
        elif foodx in range(200,440) and foody in range(0,320):
            foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
            foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
            Length_of_snake += 0
        elif foodx in range(100, 180) and foody in range(0,40):
            foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
            foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
            Length_of_snake += 0
        elif foodx in range(520, 700) and foody in range(0,100):
            foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
            foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
            Length_of_snake += 0
        elif foodx in range(640, 840) and foody in range(560, 640):
            foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
            foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
            Length_of_snake += 0
        elif foodx in range(260, 520) and foody in range(520, 640):
            foodx = round(random.randrange(0,dis_width - snake_block)/10.0) * 10.0
            foody = round(random.randrange(80, 640 - snake_block)/10.0) * 10.0
            Length_of_snake += 0

        clock.tick(snake_speed)




    pygame.quit()
    quit()

gameLoop()

【问题讨论】:

  • 请不要称它为“while函数”。 while 是关键字,用于声明while-loop 语句。

标签: python while-loop pygame


【解决方案1】:

[...]我想打印连续按下的键的方向,直到蛇改变方向[...]

你每次定位时都要print方向。

从事件循环中删除所有print 指令:

def gameLoop(): 
    # [...]

    while not game_over:
        # [...]

        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
                    print("Left")
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block
                    y1_change = 0
                    print ("right")
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block
                    x1_change = 0
                    print ("up")
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block
                    x1_change = 0
                    print ("down")

printx1y1时的方向变化:

def gameLoop(): 
    # [...]

    while not game_over:
        # [...]

        x1 += x1_change
        y1 += y1_change
        if x1_change < 0:
            print("Left")
        elif x1_change > 0:
            print ("right")
        elif y1_change < 0:
           print ("up")
        elif y1_change < 0:
           print ("down")

【讨论】:

    猜你喜欢
    • 2021-03-02
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多