'''
»òÐíÄã»á¶Ô position.left = width - turtle_rect.width ¸Ðµ½ÒÉ»ó,ÒòΪÄã×Ðϸ¿´Ò»ÏÂÉÏͼÓÒ²àÎÚ¹êµÄÄÇÁ½ÖÖ״̬,Ã÷ÏÔ
TwidthºÍTheightÊDz»Ò»Ñù¶Ô°É!ÄÇΪʲôÎÚ¹êÒÀÈ»¿ÉÒÔÕý³£ÅÀÐÐÄØ???
ÕâÊÇÒòΪ position = turtle_rect = turtle.get_rect() ÕâÐдúÂëÓÃÀ´»ñÈ¡ÎÚ¹êÐýתºó״̬µÄÔµ¹Ê,ÎÞÂÛÄãÔõÑùÐýתÎÚ¹ê,
Ëü¶¼ÄÜÎÚ¹êµÄTwidthºÍTheight,ÄÇôÉÏÃæµÄÎÊÌâ¾Í½â¾öÁË
×¢: Twidth ´ú±í turtle.width, Theight ´ú±í turtle.height
'''
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
bg = (255,255,255)
size = width,height = 600,400
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Turtle')
speed = [5,0]
turtle = pygame.image.load('turtle.png')
position = turtle.get_rect()
turtle_top = pygame.transform.rotate(turtle, 180) # ´°¿Ú¶¥²¿
turtle_right = pygame.transform.rotate(turtle, 90)# ´°¿ÚÓÒ²à
turtle_bottom = turtle # ´°¿Úµ×²¿
turtle_left = pygame.transform.rotate(turtle, 270)# ´°¿Ú×ó²à
turtle = turtle_top #³õʼ״̬Ϊ´°¿Ú¶¥²¿
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
position = position.move(speed)
if position.right > width: #Óɶ¥²¿×ªÏòÓÒ²à
turtle = turtle_right #½øÈëÓÒ²à״̬
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width # L
speed = [0,5]#ÏòÏÂÒƶ¯
if position.bottom > height: #ÓÉÓÒ²àתÏòµ×²¿
turtle = turtle_bottom #½øÈëµ×²¿×´Ì¬
position = turtle_rect = turtle.get_rect()
position.left = width - turtle_rect.width # L
position.top = height - turtle_rect.height# T
speed = [-5,0]#Ïò×óÒƶ¯
if position.left < 0: #Óɵײ¿×ªÏò×ó²à
turtle = turtle_left #½øÈë×ó²à״̬
position = turtle_rect = turtle.get_rect()
position.top = height - turtle_rect.height# T
speed = [0,-5]#ÏòÉÏÒƶ¯
if position.top < 0: #ÓÉ×ó²àתÏò¶¥²¿
turtle = turtle_top #½øÈ붥²¿×´Ì¬
speed = [5,0] #ÏòÓÒÒƶ¯
screen.fill(bg)
screen.blit(turtle, position)
pygame.display.update()
pygame.time.delay(35)
СºÚÎÝ|ÊÖ»ú°æ|Archiver|ÓãC¹¤×÷ÊÒ ( ÔÁICP±¸18085999ºÅ-1 | ÔÁ¹«Íø°²±¸ 44051102000585ºÅ)
GMT+8, 2024-5-1 05:15
Powered by Discuz! X3.4
© 2001-2023 Discuz! Team.