移动绘制的矩形

时间:2016-06-10 19:52:14

标签: python pygame

我让游戏出现并呈现矩形。 当我按下KEYDOWN时,它不会移动矩形,只会使它更长。 我尝试过很多东西。我是Pygame的新手 任何帮助都会很棒。

以下是代码:

import pygame
import time
import random
import math
import sys


pygame.init()

display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)



gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Vertical Pong')
clock = pygame.time.Clock()


def pongBoard(x,y,):
    pygame.draw.rect(gameDisplay,white,(x,y,250,25))


def gameLoop():

    x = 325
    y = 750
    xChange = 0



    inGame = True

    while inGame:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    xChange = -5
                    print("Left")
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    xChange = 5
                    print("Right")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    xChange = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    xChange = 0

        pongBoard(x,y)

        x += xChange






        pygame.display.update()
        clock.tick(60)




gameLoop()
pygame.quit()
quit()

2 个答案:

答案 0 :(得分:0)

问题是这样的: 矩形在不同的坐标处不断重绘,但屏幕没有被绘制在矩形上以掩盖不应该在那里的部分。简而言之,我们需要不断绘制背景。

现在主游戏循环中的代码:

while inGame:
    #This code below draws the background
    pygame.draw.rect(gameDisplay, black, (0, 0, display_width, display_height)) 

就是这样!背景将不断掩盖乒乓球,乒乓球将不断发挥到新的位置!

P.S,有一种更好的方法可以在这里进行箭头键移动:How to get keyboard input in pygame?

答案 1 :(得分:0)

它确实会移动它,但旧的只是停留在那里,使它看起来不会移动但只是增长。一种改变方法是将旧的颜色改为背景颜色

尝试使用此代码: - )

import pygame
import time
import random
import math
import sys


pygame.init()

display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)
red = (123,11,45)



gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Vertical Pong')
clock = pygame.time.Clock()


def pongBoard(x,y,xold):
    pygame.draw.rect(gameDisplay,white,[x,y,250,25])
    pygame.draw.rect(gameDisplay,red,[xold,y,250,25])


def gameLoop():

    x = 325
    y = 750
    xChange = 0



    inGame = True

    while inGame:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    xChange = -50
                    pongBoard(x,y,xold)
                    print("Left")
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    xChange = 50
                    pongBoard(x,y,xold)
                    print("Right")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    xChange = 0
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    xChange = 0
        xold = x

        x += xChange
        xold = x-xChange






        pygame.display.update()
        clock.tick(60)




gameLoop()
pygame.quit()
quit()