如何在pygame中反映屏幕

时间:2018-05-26 12:35:44

标签: python pygame

我想从右边缘开出矩形

如何让这些矩形水平移动? 我尝试从本教程中理解它,但我无法here 所以对此有任何想法 我的小角色鬼 btata.png

import pygame
from pygame.locals import *
import sys
import time
import random

pygame.init()

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('FullMarx')

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

crashed = False
ghostImg = pygame.image.load('btata.png')
clock = pygame.time.Clock()

def ghost(x,y):
    gameDisplay.blit(ghostImg, (x,y))

def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',20)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

def gameOver():
    message_display('Game Over')

def rect():
    pygame.draw.rect(screen, color, (x,y,width,height), thickness)


def event():
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            pygame.quit()
            sys.exit()


def game():

    ghost_width = 50
    x = 20
    y = 180
    isJump = False
    jumpCount = 9
    #x_change = 0 

    thing_startx = random.randrange(0, display_width)
    thing_starty = -300
    thing_speed = 7
    thing_width = 30
    thing_height = 30    

    while True:
        event()
        gameDisplay.fill(white)
        ghost(x,y)

        #____Ghost_Motion___________#
        keys = pygame.key.get_pressed()
        if not(isJump):
            if keys[pygame.K_UP] or keys[pygame.K_SPACE] or keys[pygame.K_w]:
                isJump = True
                walkCount = 0
        else:
            if jumpCount >= -9:
                neg = 1
                if jumpCount < 0:
                    neg = -1
                y -= (jumpCount ** 2) * 0.5 * neg
                jumpCount -= 1
            else:
                isJump = False
                jumpCount = 9
        clock.tick(50)
        #____Ghost_Motion___________#        

        # things(thingx, thingy, thingw, thingh, color)
        things(thing_startx, thing_starty, thing_width, thing_height, black)
        thing_starty += thing_speed
        ghost(x,y)

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0,display_width)

        if y < thing_starty+thing_height:
            print('y crossover')
            if x > thing_startx and x < thing_startx + thing_width or x+ghost_width > thing_startx and x + ghost_width < thing_startx+thing_width:
                print('x crossover')
                gameOver()    

        pygame.display.update()
        #clock.tick(60)
game()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:0)

通过修改其x和y坐标来移动矩形(因此也就是播放器/图像)。

这是一个基本的评论示例,可以帮助您想象:

import pygame
from pygame.locals import *


# Initialize a screen
SCREEN = pygame.display.set_mode((700, 500))
# Fill it with black
SCREEN.fill((0, 0, 0))

# Create a rect that'll represent our player
# Arguments: x, y, width, height
player = pygame.rect.Rect(100, 100, 20, 50)



# Keep track of which direction
# the player is moving in
moving_left = False
moving_right = False
moving_up = False
moving_down = False


# Loop
while True:
    for event in pygame.event.get():
        # Go through every event that pygame
        # records, the 'event queue' and
        # react accordingly

        # User closed the window
        if event.type == QUIT:
            pygame.quit()


        # If the user presses any key, a
        # KEYDOWN event is placed into the
        # event queue, and we detect it here
        if event.type == KEYDOWN:

            # The pressed key is 'd'
            if event.key == K_d:
                moving_right = True

            # The pressed key is 'a'
            if event.key == K_a:
                moving_left = True

            # The pressed key is 'w'
            if event.key == K_w:
                moving_up = True

            # The pressed key is 's'
            if event.key == K_s:
                moving_down = True

        # However, if the user lifts stops
        # pressing the keyboard,
        # a KEYUP event will be placed into
        # the event queue
        if event.type == KEYUP:
            # The unpressed key is 'd'
            if event.key == K_d:
                moving_right = False

            # The unpressed key is 'a'
            if event.key == K_a:
                moving_left = False

            # The unpressed key is 'w'
            if event.key == K_w:
                moving_up = False

            # The unpressed key is 's'
            if event.key == K_s:
                moving_down = False


    # Increment/decrement the players
    # coordinates, its 'position'
    # this is akin to movement
    if moving_left:
        player.x -= 2
    if moving_right:
        player.x += 2
    if moving_up:
        player.y -= 2
    if moving_down:
        player.y += 2


    # Repaint our screen black,
    SCREEN.fill((0, 0, 0))

    # draw our player (the rect)
    # onto the SCREEN, at its coordinates
    # in white.
    # You'd simply use SCREEN.blit
    # here in order to place an image
    pygame.draw.rect(
        SCREEN, (255, 255, 255),
        player
    )

    # refresh the screen to show our changes
    pygame.display.update()

你必须在2D表面上概念化运动,如下所示:

Movement on a 2D game surface