|
发表于 2022-5-23 12:13:50
|
显示全部楼层
这是我以前写的, 你参考一下
- import pygame
- import sys
- from pygame.constants import *
- def change_pos(position, width, height):
- if position.right > width:
- position.right = width
-
- if position.bottom > height:
- position.bottom = height
- pygame.init()
- # size = width, height = (600, 400)
- size = width, height = (1536, 864)
- speed = [5, 0]
- bg = (255, 255, 255)
- clock = pygame.time.Clock()
- screen = pygame.display.set_mode(size, RESIZABLE)
- pygame.display.set_caption("初次见面,请多多关照!")
- # post the turtle
- turtle = pygame.image.load(r"D:\user\Desktop\pyPractice\game\turtle\turtle.png").convert_alpha()
- position = turtle.get_rect()
- for i in range(position.width):
- for j in range(position.height):
- at = turtle.get_at((i, j))
- if at[0] > 100 and at[2] > 200 and at[1] > 100:
- at[3] = 0
- turtle.set_at((i, j),at)
- turtle_right = pygame.transform.rotate(turtle, 90)
- turtle_top = pygame.transform.rotate(turtle, 90*2)
- turtle_left = pygame.transform.rotate(turtle, 90*3)
- turtle_bottom = turtle
- turtle = turtle_top
- fullscreen = False
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
-
- if event.type == VIDEORESIZE:
- if (event.size[0] < 200):
- event.size = (200, event.size[1])
- size = event.size
- width, height = size
- screen = pygame.display.set_mode(size, RESIZABLE)
- change_pos(position, width, height)
-
- if (event.size[1] < 200):
- event.size = (event.size[0], 200)
- size = event.size
- width, height = size
- screen = pygame.display.set_mode(size, RESIZABLE)
- change_pos(position, width, height)
- else:
- size = event.size
- width, height = size
- screen = pygame.display.set_mode(size, RESIZABLE)
- change_pos(position, width, height)
- if event.type == KEYDOWN:
- if event.key == pygame.K_F11:
- fullscreen = not fullscreen
- if fullscreen:
- # size = width, height = pygame.display.list_modes()[0]
- size = width, height = (1536, 864)
- screen = pygame.display.set_mode(size, pygame.HWSURFACE)
- else:
- size = width, height = (600, 400)
- screen = pygame.display.set_mode(size)
- change_pos(position, width, height)
- position = position.move(speed)
- if position.right > width:
- turtle = turtle_right
- position = turtle_rect = turtle.get_rect()
- position.right = width
- speed = [0, 5]
- if position.bottom > height:
- turtle = turtle_bottom
- position = turtle_rect = turtle.get_rect()
- position.right = width
- position.bottom = height
- speed = [-5, 0]
-
- if position.left < 0:
- turtle = turtle_left
- position = turtle_rect = turtle.get_rect()
- position.bottom = height
- speed = [0, -5]
-
- if position.top < 0:
- turtle = turtle_top
- position = turtle_rect = turtle.get_rect()
- speed = [5, 0]
- screen.fill(bg)
- screen.blit(turtle, position)
- pygame.display.flip()
- pygame.time.delay(10)
- clock.tick(200)
复制代码 |
|