Python 如何在pygame中反映屏幕 我想从右边画出矩形
如何使这些矩形水平移动? 我试图从本教程中理解它,但我无法理解 有什么想法吗 我的小角色幽灵Python 如何在pygame中反映屏幕 我想从右边画出矩形,python,pygame,Python,Pygame,如何使这些矩形水平移动? 我试图从本教程中理解它,但我无法理解 有什么想法吗 我的小角色幽灵 导入pygame 从pygame.locals导入* 导入系统 导入时间 随机输入 pygame.init() 显示宽度=800 显示高度=600 gameDisplay=pygame.display.set_模式((800600)) pygame.display.set_标题('FullMarx') 黑色=(0,0,0) 白色=(255255) 红色=(255,0,0) 崩溃=错误 ghostImg
导入pygame
从pygame.locals导入*
导入系统
导入时间
随机输入
pygame.init()
显示宽度=800
显示高度=600
gameDisplay=pygame.display.set_模式((800600))
pygame.display.set_标题('FullMarx')
黑色=(0,0,0)
白色=(255255)
红色=(255,0,0)
崩溃=错误
ghostImg=pygame.image.load('btata.png')
clock=pygame.time.clock()
def重影(x,y):
游戏显示.blit(鬼影,(x,y))
定义事物(thingx、thingy、thingw、thingh、颜色):
pygame.draw.rect(游戏显示,颜色,[thingx,thingy,thingw,thingh])
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font('freesansbold.ttf',20)
TextSurf,TextRect=text\u对象(text,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
def gameOver():
信息显示(“游戏结束”)
def rect():
pygame.draw.rect(屏幕、颜色、(x、y、宽度、高度)、厚度)
def事件():
对于pygame.event.get()中的事件:
如果event.type==QUIT或(event.type==KEYDOWN和event.key==K_ESCAPE):
pygame.quit()
sys.exit()
def game():
重影宽度=50
x=20
y=180
isJump=False
跳数=9
#x_变化=0
thing\u startx=random.randrange(0,显示宽度)
事情开始=-300
速度=7
宽度=30
物体高度=30
尽管如此:
事件()
游戏显示。填充(白色)
幽灵(x,y)
#____鬼魂运动___________#
keys=pygame.key.get_pressed()
如果不是(isJump):
如果键[pygame.K_UP]或键[pygame.K_SPACE]或键[pygame.K_w]:
isJump=True
步行次数=0
其他:
如果跳线计数>=-9:
负=1
如果跳线计数<0:
负=-1
y-=(跳线计数**2)*0.5*负
跳转计数-=1
其他:
isJump=False
跳数=9
时钟滴答(50)
#____鬼魂运动
#东西(东西,东西,东西,东西,东西,颜色)
事物(事物开始,事物开始,事物宽度,事物高度,黑色)
物体开始速度+=物体速度
幽灵(x,y)
如果事物开始>显示高度:
物体起点=0-物体高度
thing\u startx=random.randrange(0,显示宽度)
如果y<物体起点+物体高度:
打印('y交叉')
如果x>thing\u startx和xthing\u startx和x+ghost\u width
通过修改矩形的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表面上的运动进行概念化,如下所示:
不要在帖子中添加垃圾。必须将
东西\u startx
位置设置为大于屏幕宽度的值,并在每一帧中从中减去速度。
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()