如何在pygame中为形状添加移动?

时间:2016-11-13 04:35:04

标签: python python-2.7 pygame

我正在使用python 2.7试图使用pygame模块制作一个简单的游戏。在程序中它创建了一个矩形,而我正在做的就是让它在按下按键时移动。我相信问题在于玩家。移动'部分在我的代码中,但文档在pygames网站上很差。任何帮助表示赞赏。

import pygame
import random
import time

pygame.init()

white = (255,255,255)
black = (0,0,0)

displayWidth = 800
displayHeight = 800

FPS = 30
clock = pygame.time.Clock()

blockWidth = 50
blockHeight = 50

pygame.display.set_caption('Test Game')
screen = pygame.display.set_mode([displayWidth, displayHeight])
background = pygame.Surface(screen.get_size())
background.fill((white))
background = background.convert()
screen.blit(background, (0,0))
global xStart, yStart
xStart = 400
yStart = 400
global player
player = pygame.draw.rect(screen, black, ([xStart,yStart,blockWidth,blockHeight]))
pygame.display.update()
def mainloop():
    global x, y
    x = xStart
    y = yStart
    mainloop = True
    pygame.display.update()
    while mainloop == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                mainloop = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    mainloop = False
                if event.key == pygame.K_UP:
                    player.move(x, y + 10)
                    pygame.display.update()
                if event.key == pygame.K_DOWN:
                    player.move(x, y - 10)
                    pygame.display.update()
                if event.key == pygame.K_LEFT:
                    player.move(x - 10, y)
                    pygame.display.update()
                if event.key == pygame.K_RIGHT:
                    player.move(x + 10, y)
                    pygame.display.update()
            clock.tick(FPS)
            pygame.display.flip()

mainloop()
pygame.quit()

1 个答案:

答案 0 :(得分:0)

查找教程(即Program Arcade Games With Python And Pygame)因为你有很多事情需要改变。

PyGame是低级库,你必须自己做所有事情。在while循环中,您必须清除屏幕或绘制背景并一次又一次地绘制播放器。

修改后的代码。

您必须了解pygame.Rectpygame.Surface,(pygame.Sprite),events等。

import pygame
import random
import time

# --- constants --- (UPPER_CASE names)

WHITE = (255, 255, 255)
BLACK = (0  ,   0,   0)

DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 800

FPS = 30

BLOCK_WIDTH = 50
BLOCK_HEIGHT = 50

# --- functions --- (lower_case names)

def run():

    mainloop = True

    speed_x = 0
    speed_y = 0

    while mainloop:

        # --- events ---

        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                mainloop = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    mainloop = False
                # - start moving -
                elif event.key == pygame.K_UP:
                    speed_y = -10
                elif event.key == pygame.K_DOWN:
                    speed_y = 10
                elif event.key == pygame.K_LEFT:
                    speed_x = -10
                elif event.key == pygame.K_RIGHT:
                    speed_x = 10
            #elif event.type == pygame.KEYUP:
            #    # - stop moving -
            #    if event.key == pygame.K_UP:
            #        speed_y = 0
            #    elif event.key == pygame.K_DOWN:
            #        speed_y = 0
            #    elif event.key == pygame.K_LEFT:
            #        speed_x = 0
            #    elif event.key == pygame.K_RIGHT:
            #        speed_x = 0            

        # --- updates ---

        player_rect.move_ip(speed_x, speed_y)

        # --- draws ---

        screen.blit(background, background_rect)
        screen.blit(player, player_rect)                    
        pygame.display.flip()

        clock.tick(FPS)

# --- main ---

pygame.init()
pygame.display.set_caption('Test Game')

screen = pygame.display.set_mode( (DISPLAY_WIDTH, DISPLAY_HEIGHT) )
screen_rect = screen.get_rect()

background_rect = screen_rect
background = pygame.Surface(background_rect.size)
background.fill(WHITE)
background = background.convert()

player_rect = pygame.Rect(400, 400, BLOCK_WIDTH, BLOCK_HEIGHT)
player = pygame.Surface(player_rect.size)
player.fill(BLACK)

clock = pygame.time.Clock()

run()

pygame.quit()