如何在此代码中使用pygame在python中移动矩形?

时间:2017-05-27 19:55:51

标签: python pygame

我想用箭头键移动一个矩形,到目前为止我只能绘制矩形,但我没有让keyhandler工作。 我是python和pygame的新手。

这是我的main.py:

import pygame
import time
import random
from Player import *

# Initialize Pygame
pygame.init()
game_over = False

# Color
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (192, 192, 192)

# Setting up the window
surfaceWidth = 800
surfaceHeight = 500

surface = pygame.display.set_mode((surfaceWidth, surfaceHeight))
pygame.display.set_caption("Rectangle Runner")
clock = pygame.time.Clock()

#Player(surface, surface_height, surface_width, size, jump_height, color)
player = Player(surface, surfaceHeight, surfaceWidth, 50, 200, BLACK)

# Game Loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        if game_over == True:
            pygame.quit()
            quit()
    surface.fill(GRAY)
    #Player.draw()
    player.keyHandling()
    player.checkBoundaries()
    player.draw()
    #Obstacle.draw()
    pygame.display.update()
    clock.tick(60)
# This code shall be unreachable
pygame.quit()
quit()

这是Player.py:

import pygame


class Player:

    def __init__(self, surface, surface_height, surface_width, size, jump_height, color):
        self.surface = surface
        self.surface_height = surface_height
        self.surface_width = surface_width
        self.size = size
        self.jump_height = jump_height
        self.color = color

        self.x = (0 + (self.surface_width/10))
        self.y = self.surface_height - self.size

        self.x_move = 0
        self.y_move = 0

        self.rect = pygame.draw.rect(self.surface, self.color, [self.x, self.y, self.size, self.size])


    def keyHandling(self):
        # Draw the player
        #pygame.draw.rect(self.surface, self.color,
                        #[self.x, self.y, self.size, self.size])
        # KeyListener
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    self.y_move = -10
                    #self.y += self.y_move
                    #self.rect.move_ip(0, -10)
                if event.key == pygame.K_LEFT:
                    self.x_move = -5
                    #self.x += self.x_move
                    #self.rect.move_ip(-5, 0)
                if event.key == pygame.K_RIGHT:
                    self.x_move = 5
                    #self.x += self.x_move
                    #self.rect.move_ip(5, 0)
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP:
                    self.y_move = 0
                    #self.y += self.y_move
                    #self.rect.move_ip(0, 10)
                if event.key == pygame.K_LEFT:
                    self.x_move = 0
                if event.key == pygame.K_RIGHT:
                    self.x_move = 0
        self.x += self.x_move
        self.y += self.y_move


    def checkBoundaries(self):
        # Checking for Boundaries
        #if self.y <= self.jump_height:
        #    self.y_move = -10
        if self.y > self.surface_height - self.size:
            self.y_move = 0
        if self.y <= self.jump_height + self.size:
            self.y_move = -10

    def draw(self):
        pygame.draw.rect(self.surface, self.color, [self.x, self.y, self.size, self.size])




        #pygame.draw.rect(self.surface, self.color,[self.x, self.y, self.size, self.size])

我需要帮助。我整天都想着这个。 提前致谢:D

1 个答案:

答案 0 :(得分:0)

代码中的问题是pygame.event.get()是一个生成器。从中获得的每个项目只能获得一次。因此,每次时钟滴答时,您都会抓住事件生成器中的所有内容并将它们全部销毁。接下来,当Player对象迭代pygame.event.get时,它什么都不做,因为生成器已经变空了。请参阅有关发电机的this

您需要做的是捕获main.py模块中的KEYDOWN事件,然后将关键信息传递到Player对象。

使用以下内容替换main.py中的主循环:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        if event.type == pygame.KEYDOWN:
            player.keyDown(event.key)
        if game_over == True:
            pygame.quit()
            quit()
    surface.fill(GRAY)
    player.checkBoundaries()
    player.draw()
    pygame.display.update()
    clock.tick(60)

keyHandling中删除Player.py方法,并使用以下方法替换:

def keyDown(self, key):
    if key == pygame.K_w:
        self.y -= 10
    elif key == pygame.K_a:
        self.x -= 10
    elif key == pygame.K_d:
        self.x += 10

这现在消除了生成器的问题。

Full code