Commit aeb409ff by BellCodeEditor

save project

parent 50bdeb0c
Showing with 226 additions and 73 deletions
import pygame
from pygame import locals
import random
from typing import Dict, List, Sequence, Tuple, TypeVar, Union, overload
# 初始化pygame,为使用硬件做准备
pygame.init()
from pygame.math import Vector2
from ._common import _Coordinate, _CanBeRect
# 创建一个窗口
screen = pygame.display.set_mode((660, 480))
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
_K = TypeVar("_K")
_V = TypeVar("_V")
# 背景
background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') # 头 朝右
food = pygame.image.load('apple.png') # 食物 苹果
body = pygame.image.load('body.png') # 身体
left = pygame.image.load('left.png') # 头 朝左
up = pygame.image.load('up.png') # 头 朝上
down = pygame.image.load('down.png') # 头 朝下
class Rect(object):
x: int
y: int
top: int
left: int
bottom: int
right: int
topleft: Tuple[int, int]
bottomleft: Tuple[int, int]
topright: Tuple[int, int]
bottomright: Tuple[int, int]
midtop: Tuple[int, int]
midleft: Tuple[int, int]
midbottom: Tuple[int, int]
midright: Tuple[int, int]
center: Tuple[int, int]
centerx: int
centery: int
size: Tuple[int, int]
width: int
height: int
w: int
h: int
__hash__: None # type: ignore
@overload
def __init__(
self, left: float, top: float, width: float, height: float
) -> None: ...
@overload
def __init__(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> None: ...
@overload
def __init__(
self,
left_top_width_height: Union[
Rect, Tuple[float, float, float, float], List[float]
],
) -> None: ...
@overload
def __getitem__(self, i: int) -> int: ...
@overload
def __getitem__(self, s: slice) -> List[int]: ...
def copy(self) -> Rect: ...
@overload
def move(self, x: float, y: float) -> Rect: ...
@overload
def move(self, move_by: _Coordinate) -> Rect: ...
@overload
def move_ip(self, x: float, y: float) -> None: ...
@overload
def move_ip(self, move_by: _Coordinate) -> None: ...
@overload
def inflate(self, x: float, y: float) -> Rect: ...
@overload
def inflate(self, inflate_by: _Coordinate) -> Rect: ...
@overload
def inflate_ip(self, x: float, y: float) -> None: ...
@overload
def inflate_ip(self, inflate_by: _Coordinate) -> None: ...
@overload
def update(self, left: float, top: float, width: float, height: float) -> None: ...
@overload
def update(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> None: ...
@overload
def update(
self,
left_top_width_height: Union[
Rect, Tuple[float, float, float, float], List[float]
],
) -> None: ...
@overload
def clamp(self, rect: Union[_CanBeRect, Rect]) -> Rect: ...
@overload
def clamp(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> Rect: ...
@overload
def clamp(self, left: float, top: float, width: float, height: float) -> Rect: ...
@overload
def clamp_ip(self, rect: Union[_CanBeRect, Rect]) -> None: ...
@overload
def clamp_ip(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> None: ...
@overload
def clamp_ip(
self, left: float, top: float, width: float, height: float
) -> None: ...
@overload
def clip(self, rect: Union[_CanBeRect, Rect]) -> Rect: ...
@overload
def clip(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> Rect: ...
@overload
def clip(self, left: float, top: float, width: float, height: float) -> Rect: ...
@overload
def clipline(
self, x1: float, x2: float, x3: float, x4: float
) -> Union[Tuple[Tuple[int, int], Tuple[int, int]], Tuple[()]]: ...
@overload
def clipline(
self, first_coordinate: _Coordinate, second_coordinate: _Coordinate
) -> Union[Tuple[Tuple[int, int], Tuple[int, int]], Tuple[()]]: ...
@overload
def clipline(
self, values: Union[Tuple[float, float, float, float], List[float]]
) -> Union[Tuple[Tuple[int, int], Tuple[int, int]], Tuple[()]]: ...
@overload
def clipline(
self, coordinates: Union[Tuple[_Coordinate, _Coordinate], List[_Coordinate]]
) -> Union[Tuple[Tuple[int, int], Tuple[int, int]], Tuple[()]]: ...
@overload
def union(self, rect: Union[_CanBeRect, Rect]) -> Rect: ...
@overload
def union(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> Rect: ...
@overload
def union(self, left: float, top: float, width: float, height: float) -> Rect: ...
@overload
def union_ip(self, rect: Union[_CanBeRect, Rect]) -> None: ...
@overload
def union_ip(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> None: ...
@overload
def union_ip(
self, left: float, top: float, width: float, height: float
) -> None: ...
def unionall(self, rect: Sequence[Union[_CanBeRect, Rect]]) -> Rect: ...
def unionall_ip(self, rect_sequence: Sequence[Union[_CanBeRect, Rect]]) -> None: ...
@overload
def fit(self, rect: Union[_CanBeRect, Rect]) -> Rect: ...
@overload
def fit(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> Rect: ...
@overload
def fit(self, left: float, top: float, width: float, height: float) -> Rect: ...
def normalize(self) -> None: ...
@overload
def __contains__(self, rect: Union[_CanBeRect, Rect, int]) -> bool: ...
@overload
def __contains__(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> bool: ...
@overload
def __contains__(self, left: float, top: float, width: float, height: float) -> bool: ...
@overload
def contains(self, rect: Union[_CanBeRect, Rect]) -> bool: ...
@overload
def contains(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> bool: ...
@overload
def contains(
self, left: float, top: float, width: float, height: float
) -> bool: ...
@overload
def collidepoint(self, x: float, y: float) -> bool: ...
@overload
def collidepoint(self, x_y: Union[List[float], Tuple[float, float]]) -> bool: ...
@overload
def colliderect(self, rect: Union[_CanBeRect, Rect]) -> bool: ...
@overload
def colliderect(
self,
left_top: Union[List[float], Tuple[float, float], Vector2],
width_height: Union[List[float], Tuple[float, float], Vector2],
) -> bool: ...
@overload
def colliderect(
self, left: float, top: float, width: float, height: float
) -> bool: ...
def collidelist(self, rect_list: Sequence[Union[Rect, _CanBeRect]]) -> int: ...
def collidelistall(
self, rect_list: Sequence[Union[Rect, _CanBeRect]]
) -> List[int]: ...
# Also undocumented: the dict collision methods take a 'values' argument
# that defaults to False. If it is False, the keys in rect_dict must be
# Rect-like; otherwise, the values must be Rects.
@overload
def collidedict(
self, rect_dict: Dict[_CanBeRect, _V], values: bool = ...
) -> Tuple[_CanBeRect, _V]: ...
@overload
def collidedict(
self, rect_dict: Dict[_K, "Rect"], values: bool
) -> Tuple[_K, "Rect"]: ...
@overload
def collidedictall(
self, rect_dict: Dict[_CanBeRect, _V], values: bool = ...
) -> List[Tuple[_CanBeRect, _V]]: ...
@overload
def collidedictall(
self, rect_dict: Dict[_K, "Rect"], values: bool
) -> List[Tuple[_K, "Rect"]]: ...
x, y = 240, 120
position = [(180, 90), (180, 120), (210, 120), (x, y)]
setheading = "right"
snake_head = right
apple_x = 360
apple_y = 300
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != "left":
setheading = 'right'
snake_head = right
if event.key == locals.K_LEFT and setheading != "right":
setheading = 'left'
snake_head = left
if event.key == locals.K_UP and setheading != "down":
setheading = 'up'
snake_head = up
if event.key == locals.K_DOWN and setheading != "up":
setheading = 'down'
snake_head = down
# 设置贪吃蛇的头部坐标
if setheading == "right":
x += 30
elif setheading == "left":
x -= 30
elif setheading == "up":
y -= 30
else:
y += 30
position.append((x, y))
if apple_x == x and apple_y == y:
apple_x = random.randint(0,660)
apple_y = random.randint(0,480)
position.pop(0)
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去
screen.blit(snake_head, position[-1])
# 将贪吃蛇的身体画上去
for i in range(len(position)-1):
screen.blit(body, position[i])
# 将果实画上去
screen.blit(food, (apple_x, apple_y))
# 刷新画面
pygame.display.update()
FPSCLOCK.tick(3)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment