Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pygame.draw.rect和screen_surface.blit()之间有什么区别?_Python_Pygame - Fatal编程技术网

Python pygame.draw.rect和screen_surface.blit()之间有什么区别?

Python pygame.draw.rect和screen_surface.blit()之间有什么区别?,python,pygame,Python,Pygame,我试图使一个红色矩形向右移动,通过使用pygame.move.rect或.blit,我能够完成同样的事情。我能够显示红色矩形,并通过按向右箭头将其向右移动。但是,我应该知道这两个函数之间有什么区别吗?为什么有两个函数基本上做相同的事情 使用pygame.move.rect编写代码 import pygame import sys pygame.init() #obtain the surface and rect for screen screen_surface = pygame.displ

我试图使一个红色矩形向右移动,通过使用pygame.move.rect或.blit,我能够完成同样的事情。我能够显示红色矩形,并通过按向右箭头将其向右移动。但是,我应该知道这两个函数之间有什么区别吗?为什么有两个函数基本上做相同的事情

使用pygame.move.rect编写代码

import pygame
import sys

pygame.init()
#obtain the surface and rect for screen
screen_surface = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Hi")

#Obtain Surface and rect for the rectangle 
red_rectangle = pygame.Surface((600,400))
red_rectangle_rect = red_rectangle.get_rect()

#make the rectangle surface red
red_rectangle.fill((255,0,0))

move_right = False

while True:
   #event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                move_right = True
            print(event.type)
            print(event.key)

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                move_right = False
          

    #rectangle move to right when right arrow is pressed 
    if move_right:
        red_rectangle_rect.x += 10
        print(red_rectangle_rect.x)
        
                
 


    screen_surface.fill((255,255,255))
    
    # the difference between this function and the .blit
    pygame.draw.rect(screen_surface,(255,0,0),red_rectangle_rect)
   

    pygame.display.flip()
用.blit编码

import pygame
import sys

pygame.init()
#obtain the surface and rect for screen
screen_surface = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Hi")

#Obtain Surface and rect for the rectangle 
red_rectangle = pygame.Surface((600,400))
red_rectangle_rect = red_rectangle.get_rect()

#make the rectangle surface red
red_rectangle.fill((255,0,0))

move_right = False

while True:
   #event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                move_right = True
            print(event.type)
            print(event.key)

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                move_right = False
          

    #rectangle move to right when right arrow is pressed 
    if move_right: then 
        red_rectangle_rect.x += 10
        print(red_rectangle_rect.x)
        
                
 


    screen_surface.fill((255,255,255))

 
    #Difference between this and the draw function
    screen_surface.blit(red_rectangle,red_rectangle_rect)

    pygame.display.flip()
blit(图像,(左,上))在给定位置将图像绘制到屏幕上。该函数接受曲面或字符串作为其图像参数。如果image是str,那么将从images/目录加载命名的映像

draw.rect(rect,(r,g,b))绘制矩形的轮廓。取一个矩形

为什么有两个函数基本上做相同的事情

不,它们不一样。绘制均匀彩色矩形时,是一种绘制位图图像的方法

见:

rect(表面、颜色、rect)->rect

在给定曲面上绘制矩形

blit(源、目标、区域=None,特殊标志=0)->Rect

将源曲面绘制到此曲面上

在您的情况下,看起来他们在做相同的事情,因为您的曲面对象均匀地填充了一种颜色。
使用加载位图图像时,行为将完全更改。不能使用
pygame.draw.rect
绘制图像,但可以使用
blit

何时使用
pygame.draw.rect
,请参阅:

当使用blit时,请参见:

所以我想澄清一下


对图像使用blit,对单色矩形使用rect。

问题解决了吗?