【问题标题】:can't get counter moving无法逆向移动
【发布时间】:2023-01-30 01:55:19
【问题描述】:

///////////////////////////// [solved]

【问题讨论】:

    标签: pygame


    【解决方案1】:

    您必须在每一帧中重新绘制场景。您必须在应用程序循环中进行绘图。典型的 PyGame 应用程序循环必须:

    import pygame, sys
    import random
    from pygame.locals import *
    
    pygame.init()
    screen = pygame.display.set_mode((800, 800))
    pygame.display.set_caption("board")
    exit = False
    
    grey = (224,224, 224)
    green = (204, 255, 204)
    blue = (204, 255, 255)
    purple = (204, 204, 255)
    black = (0, 0, 0)
    white = (255, 255, 255)
    rows = 10
    cols = 14
    colors = [grey, green, blue, purple]
    
    
    def display_board(screen_width, screen_height, square_size, margin):
        counter_pos = [0, 0]
        direction = 'right'
    
        fields = []
        for row in range(rows):
            rect = pygame.Rect(margin, margin + row * (square_size + 2), square_size, square_size)
            fields.append((random.choice(colors), rect))
            rect = pygame.Rect(margin + (cols-1) * (square_size + 2), margin + row * (square_size + 2), square_size, square_size)
            fields.append(( random.choice(colors), rect))
        for col in range(1, cols-1):
            rect = pygame.Rect(margin + col * (square_size + 2), margin, square_size, square_size)
            fields.append((random.choice(colors), rect))
            rect = pygame.Rect(margin + col * (square_size + 2), margin + (rows-1) * (square_size + 2), square_size, square_size)
            fields.append(( random.choice(colors), rect))
    
        running = True
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if direction == 'right':
                        counter_pos[0] += 1
                        if counter_pos[0] >= cols - 1:
                            direction = 'down'
                    elif direction == 'down':
                        counter_pos[1] += 1
                        if counter_pos[1] >= rows - 1:
                            direction = 'left'
                    elif direction == 'left':
                        counter_pos[0] -= 1
                        if counter_pos[0] <= 0:
                            direction = 'up'
                    elif direction == 'up':
                        counter_pos[1] -= 1
                        if counter_pos[1] <= 0:
                            direction = 'right'
    
            screen.fill(0)
            for color, rect in fields:
                pygame.draw.rect(screen, color, rect)
            pygame.draw.rect(screen, (255, 255, 255), (margin + counter_pos[0] * (2 + square_size), margin + counter_pos[1] * (square_size + 2), square_size, square_size))
            pygame.display.update()
    
    display_board(800, 600, 50, 20)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多