在Pygame中移动一个矩形

时间:2015-08-17 23:55:28

标签: python pygame

我正在尝试制作游戏并遇到问题...我无法移动矩形,它也不会给我一个错误代码?...我认为问题是它一直使矩形结束在while循环中重复...但我不知道如何解决这个问题。

#! /usr/bin/env python

import os
import random
import pygame
import math
import sys

os.environ["SDL_VIDEO_CENTERED"] = "1"

screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("LEVEL 2 = Find the Correct Square!")

clock = pygame.time.Clock()

class Player(object):
    def __init__(self):
        self.rect = pygame.draw.rect(screen, (0, 0, 128), (64, 54, 16, 16))

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 1
        if key[pygame.K_LEFT]:
           self.rect.move(-1, 0)
        if key[pygame.K_RIGHT]:
           self.rect.move(1, 0)
        if key[pygame.K_UP]:
           self.rect.move(0, -1)
        if key[pygame.K_DOWN]:
           self.rect.move(0, 1)

    def draw(self, surface):
        pygame.draw.rect(screen, (0, 0, 128), (64, 54, 16, 16))

pygame.init()

player = Player()
clock = pygame.time.Clock()

running = True       
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
            running = False

        screen.fill((255, 255, 255))

        player.draw(screen)
        player.handle_keys()
        pygame.display.update()

        clock.tick(40)

4 个答案:

答案 0 :(得分:4)

您需要对Player课程进行一些更改。您需要将self.rect重新分配给self.rect.move()的结果,或使用原位变体self.rect.move_ip()

class Player(object):
    def __init__(self):
        self.rect = pygame.rect.Rect((64, 54, 16, 16))

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 1
        if key[pygame.K_LEFT]:
           self.rect.move_ip(-1, 0)
        if key[pygame.K_RIGHT]:
           self.rect.move_ip(1, 0)
        if key[pygame.K_UP]:
           self.rect.move_ip(0, -1)
        if key[pygame.K_DOWN]:
           self.rect.move_ip(0, 1)

    def draw(self, surface):
        pygame.draw.rect(screen, (0, 0, 128), self.rect)

对于每个按键,矩形将移动一次。如果你想按住一个键时继续移动,你需要使用主循环的一部分

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
            running = False

    screen.fill((255, 255, 255))

    player.draw(screen)
    player.handle_keys()
    pygame.display.update()

    clock.tick(40)

答案 1 :(得分:0)

move只返回一个新的矩形而不更改变量,因此请使用move_ip移动到位。

pygame.draw.rect(surface, (0, 0, 128), self.rect) # draw the rect at a variable location

答案 2 :(得分:0)

我修改了你的代码,以便1)它的工作原理(必不可少!:)和2)可以用[Esc]和QUIT退出脚本,而不是卡在里面(也很重要!)我也给了生命到你的'dist'变量(未使用)来设置10个像素的位移。然而,还有一些重要的事情需要补充,例如:检查并阻止小矩形离开窗口等。

import os
import random
import pygame
from pygame.locals import * # Constants
import math
import sys

os.environ["SDL_VIDEO_CENTERED"] = "1"

screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("LEVEL 2 = Find the Correct Square!")

clock = pygame.time.Clock()

class Player(object):
    def __init__(self):
        self.rect = pygame.draw.rect(screen, (0, 0, 128), (64, 54, 16, 16))
        self.dist = 10

    def handle_keys(self):
        for e in pygame.event.get():
            if e.type == QUIT:
                pygame.quit(); exit()
            elif e.type == KEYDOWN:
                key = e.key
                if key == K_LEFT:
                    self.draw_rect(-1, 0)
                elif key == K_RIGHT:
                    self.draw_rect(1, 0)
                elif key == K_UP:
                    self.draw_rect(0, -1)
                elif key == K_DOWN:
                    self.draw_rect(0, 1)
                elif key == K_ESCAPE:
                    pygame.quit(); exit()

    def draw_rect(self,x,y):
        screen.fill((255, 255, 255))
        self.rect = self.rect.move(x*self.dist, y*self.dist); pygame.draw.rect(screen, (0, 0, 128), self.rect)
        pygame.display.update()

    def draw(self, surface):
        pygame.draw.rect(screen, (0, 0, 128), (64, 54, 16, 16))

pygame.init()

player = Player()
#clock = pygame.time.Clock()
screen.fill((255, 255, 255))
player.draw(screen)
pygame.display.update()

while True:
  player.handle_keys()

答案 3 :(得分:-1)

import os
import random
import pygame
import math
import sys

os.environ["SDL_VIDEO_CENTERED"] = "1"

screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("LEVEL 2 = Find the Correct Square!")

clock = pygame.time.Clock()

class Player(object):
    def __init__(self):
        self.rect = pygame.draw.rect(screen, (0, 0, 128), (64, 54, 16, 16))

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 1
        if key[pygame.K_LEFT]:
           self.rect.move(-1, 0)
        if key[pygame.K_RIGHT]:
           self.rect.move(1, 0)
        if key[pygame.K_UP]:
           self.rect.move(0, -1)
        if key[pygame.K_DOWN]:
           self.rect.move(0, 1)

    def draw(self, surface):
        pygame.draw.rect(screen, (0, 0, 128), (64, 54, 16, 16))

pygame.init()

player = Player()
clock = pygame.time.Clock()

running = True       
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
            running = False

        screen.fill((255, 255, 255))

        player.draw(screen)
        player.handle_keys()
        pygame.display.update()

        clock.tick(40)import os