import pygame, random # 加载库
from pygame.locals import *
pygame.init()
pygame.display.set_caption("炸渔船")
canvas = pygame.display.set_mode((700, 500))
bg=pygame.image.load("bg.png")
bg=pygame.transform.scale(bg,(700,500))
class Hero(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
#加载图片,修改大小,获取矩形,设置中心位置
self.image=[pygame.image.load("hero1.png"),
pygame.image.load("hero2.png")]
# 列表的创建,列表元素的调用,列表的索引,列表切片
self.image=[pygame.transform.scale(self.image[0],(100,100)),
pygame.transform.scale(self.image[1],(100,100))
]
self.rect=self.image[0].get_rect()
self.rect.center=350,135
class Bullet(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load("bullet.png")
self.image=pygame.transform.scale(self.image,(30,100))
self.rect=self.image.get_rect()
self.rect.center=100,100
self.speed=[0,5]
def xianshi(self):
canvas.blit(self.image,self.rect)
danjia=pygame.sprite.Group()
def handleEvent(): # 点叉叉关闭游戏界面
for event in pygame.event.get():
if event.type == QUIT:
quit()
life=0
score=0
nnn=0
yuchuan=Hero()
while True:
canvas.fill((255, 255, 255))
canvas.blit(bg,(0,0))
font=pygame.font.SysFont("kaiti",20)
text=font.render("当前生命值:"+str(life)+"分数:"+str(score),True,(255,0,0))
canvas.blit(text,(0,0))
canvas.blit(yuchuan.image[nnn],yuchuan.rect)
kkk=pygame.key.get_pressed()
if kkk[K_LEFT]:
nnn=1
yuchuan.rect.left-=5
if kkk[K_RIGHT]:
nnn=0
yuchuan.rect.left+=5
if kkk[K_SPACE]:
bullet=Bullet()
bullet.rect.center=yuchuan.rect.left+50,yuchuan.rect.top+150
danjia.add(bullet)
for d in danjia:
d.xianshi()
d.rect=d.rect.move(d.speed)
# d.rect.center=yuchuan.rect.center
#类属性的调用
handleEvent()
pygame.display.update()
import pygame
import random
import time
# 初始化 Pygame
pygame.init()
# 游戏窗口设置
WIDTH, HEIGHT = 640, 480
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇")
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# 蛇与食物设置
SNAKE_SIZE = 20
snake = [[100, 100], [80, 100], [60, 100]] # 初始蛇身坐标
food = [random.randrange(10, (WIDTH // SNAKE_SIZE)) * SNAKE_SIZE,
random.randrange(10, (HEIGHT // SNAKE_SIZE)) * SNAKE_SIZE]
direction = "RIGHT" # 初始方向
score = 10
# 游戏主循环
clock = pygame.time.Clock()
running = True
game_over = False
while running:
clock.tick(5) # 控制游戏速度
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != "DOWN":
direction = "UP"
elif event.key == pygame.K_DOWN and direction != "UP":
direction = "DOWN"
elif event.key == pygame.K_LEFT and direction != "RIGHT":
direction = "LEFT"
elif event.key == pygame.K_RIGHT and direction != "LEFT":
direction = "RIGHT"
if not game_over:
# 移动蛇头
head = snake[0].copy()
if direction == "UP":
head[1] -= SNAKE_SIZE
elif direction == "DOWN":
head[1] += SNAKE_SIZE
elif direction == "LEFT":
head[0] -= SNAKE_SIZE
elif direction == "RIGHT":
head[0] += SNAKE_SIZE
# 检测碰撞
if (head[0] < 0 or head[0] >= WIDTH or
head[1] < 0 or head[1] >= HEIGHT or
head in snake[1:]):
game_over = True
snake.insert(0, head)
# 吃食物逻辑
if head == food:
score += 10
food = [random.randrange(1, (WIDTH // SNAKE_SIZE)) * SNAKE_SIZE,
random.randrange(1, (HEIGHT // SNAKE_SIZE)) * SNAKE_SIZE]
else:
snake.pop()
# 绘制画面
window.fill(BLACK)
# 绘制蛇身
for pos in snake:
pygame.draw.rect(window, GREEN, pygame.Rect(pos[0], pos[1], SNAKE_SIZE, SNAKE_SIZE))
# 绘制食物
pygame.draw.rect(window, RED, pygame.Rect(food[0], food[1], SNAKE_SIZE, SNAKE_SIZE))
# 显示分数
font = pygame.font.Font(None, 36)
text = font.render(f"Score: {score}", True, WHITE)
window.blit(text, (10, 10))
# 游戏结束显示
if game_over:
font = pygame.font.Font(None, 72)
text = font.render("GAME OVER!", True, RED)
window.blit(text, (WIDTH // 2 - 140, HEIGHT // 2 - 40))
pygame.display.flip()
pygame.quit()
python炸鱼船
Chinese Red Guest2025-05-26 16:52