使用Python和turtle库
```python
from turtle import *
def draw_zongzi(length, width):
pendown()
seth(0)
fd(length)
绘制粽子的结
circle(length / 2, 90)
fd(width)
right(45)
fd(length / 2)
right(90)
fd(width)
right(45)
fd(length / 2)
home()
mainloop()
设置画布大小
turtle.setup(400, 400)
画粽子皮
turtle.penup()
turtle.goto(-150, 0)
turtle.pendown()
draw_zongzi(100, 50)
```
使用Python和pygame库
```python
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('zongzi.png') 假设有一个名为zongzi.png的粽子图片
self.rect = self.image.get_rect()
def update(self):
控制粽子方向
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.rect.y -= 5
if keys[pygame.K_DOWN]:
self.rect.y += 5
if keys[pygame.K_LEFT]:
self.rect.x -= 5
if keys[pygame.K_RIGHT]:
self.rect.x += 5
防止粽子跑出边界
if self.rect.top < 0 or self.rect.bottom > 600 or self.rect.left < 0 or self.rect.right > 800:
触底反弹
if self.rect.bottom > 600:
self.rect.bottom = 600
self.rect.y -= 10
if self.rect.top < 0:
self.rect.top = 0
self.rect.y += 10
if self.rect.left < 0:
self.rect.left = 0
self.rect.x -= 10
if self.rect.right > 800:
self.rect.right = 800
self.rect.x += 10
初始化pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
创建精灵组
all_sprites = pygame.sprite.Group()
zongzi = Player()
all_sprites.add(zongzi)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
all_sprites.update()
screen.fill((255, 255, 255))
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
```
使用JavaScript和HTML5 Canvas