pygame的五个小游戏

那年仲夏 提交于 2020-04-05 21:56:22

pygame窗口

代码参考:

import pygame
   from pygame.locals import *
   import sys
   def hello_world():
      pygame.init()
      pygame.display.set_mode((640,480))
      pygame.display.set_caption("hello world")
      while True:
          for event in pygame.event.get():
              if event.type==QUIT:
                  pygame.quit()
                  sys.exit()
              pygame.display.update()
   if __name__=="__main__":
      hello_world()

运行效果:

一,贪吃蛇

代码参考:

import pygame,sys,time,random
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
playSurface = pygame.display.set_mode((640,480))
pygame.display.set_caption('贪吃蛇游戏')

# 定义一些颜色
redColour = pygame.Color(255,0,0)
blackColour = pygame.Color(0,0,0)
whiteColour = pygame.Color(255,255,255)
greyColour = pygame.Color(150,150,150)
# 初始化了一些程序中用到的变量
snakePosition = [100, 100]
snakeSegments = [[100, 100], [80, 100], [60, 100]]
raspberryPosition = [300, 300] #位置
raspberrySpawned = 1   #是否吃到树莓
direction = 'right'
changeDirection = direction

def gameOver():
    gameOverFont = pygame.font.Font('simfang.ttf',72)
    gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
    gameOverRect = gameOverSurf.get_rect()
    gameOverRect.midtop = (320, 10)
    playSurface.blit(gameOverSurf, gameOverRect)
    pygame.display.flip()
    time.sleep(5)
    pygame.quit()
    sys.exit()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:

           if event.key == K_RIGHT or event.key == ord('d'):
              changeDirection = 'right'
           if event.key == K_LEFT or event.key == ord('a'):
              changeDirection = 'left'
           if event.key == K_UP or event.key == ord('w'):
              changeDirection = 'up'
           if event.key == K_DOWN or event.key == ord('s'):
              changeDirection = 'down'
           if event.key == K_ESCAPE:
              pygame.event.post(pygame.event.Event(QUIT))
    if changeDirection == 'right' and not direction == 'left':
        direction = changeDirection
    if changeDirection == 'left' and not direction == 'right':
        direction = changeDirection
    if changeDirection == 'up' and not direction == 'down':
        direction = changeDirection
    if changeDirection == 'down' and not direction == 'up':
        direction = changeDirection
        # 根据方向移动蛇头的坐标
    if direction == 'right':
           snakePosition[0] += 20
    if direction == 'left':
            snakePosition[0] -= 20
    if direction == 'up':
            snakePosition[1] -= 20
    if direction == 'down':
            snakePosition[1] += 20
    #将蛇的身体增加一节,同时将这节放在蛇的头部
    snakeSegments.insert(0, list(snakePosition))
    #检查蛇头头部的xy坐标是否等于树莓(玩家的目标点)的坐标
    if snakePosition[0] == raspberryPosition[0] and snakePosition[1] ==raspberryPosition[1]:
       raspberrySpawned = 0
    else:
        snakeSegments.pop()
    # 增加一个新的树莓到游戏界面中
    if raspberrySpawned == 0:
        x = random.randrange(1,32)
        y = random.randrange(1, 24)
        raspberryPosition = [int(x * 20), int(y * 20)]
    raspberrySpawned = 1
    playSurface.fill(blackColour)
    for position in snakeSegments:
        pygame.draw.rect(playSurface, whiteColour, Rect(position[0], position[1], 20, 20))
    pygame.draw.rect(playSurface, redColour, Rect(raspberryPosition[0], raspberryPosition[1], 20, 20))
    pygame.display.flip()
    if snakePosition[0] > 620 or snakePosition[0] < 0:
        gameOver()
    if snakePosition[1] > 460 or snakePosition[1] < 0:
        gameOver()
    for snakeBody in snakeSegments[1:]:
        if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
            gameOver()
    fpsClock.tick(10)

运行效果:

二,坦克大战

代码参考:

 import os,sys,pygame
 from pygame.locals import *
 def control_tank(event):
     speed=[x,y]=[0,0]
     speed_offset=1
     if event.type==pygame.KEYDOWN:
         if event.key==pygame.K_LEFT:
             speed[0]-=speed_offset
         if event.key==pygame.K_RIGHT:
             speed[0]=speed_offset
         if event.key==pygame.K_UP:
             speed[1]-=speed_offset
         if event.key==pygame.K_DOWN:
             speed[1]=speed_offset
     if event.type==pygame.KEYUP:
         if event.type in[pygame.K_UP,pygame.K_DOWN,pygame.K_RIGHT,pygame.K_LEFT]:
             speed=[0,0]
     return speed
 def play_tank():
     pygame.init()
     window_size=Rect(0,0,640,480)
     speed=[1,1]
     color_white=(255,255,255)
     screen=pygame.display.set_mode(window_size.size)
     pygame.display.set_caption("坦克大战")
     tank_image=pygame.image.load('tankU.bmp')
     back_image=pygame.image.load('back_image.jpg')
     tank_rect=tank_image.get_rect()
     while True:
         for event in pygame.event.get():
             if event.type==pygame.QUIT:
                 pygame.quit()
                 sys.exit()
             cur_speed=control_tank(event)
             tank_rect=tank_rect.move(cur_speed).clamp(window_size)
             screen.blit(back_image,(0,0))
             screen.blit(tank_image,tank_rect)
             pygame.display.update()
 if __name__ == "__main__":
     play_tank()

运行效果:

三,打飞机游戏

代码参考:

import  pygame
from sys import exit
from pygame.locals import *
import random

# 定义类
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800

TYPE_SMALL = 1
TYPE_MIDDLE = 2
TYPE_BIG = 3

# 子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self,bullet_img,init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = bullet_img
        self.rect = self.image.get_rect()
        self.rect.midbottom = init_pos
        self.speed = 10

    def move(self):
        self.rect.top -= self.speed
# 玩家类
class Player(pygame.sprite.Sprite):
    def __init__(self, plane_img, player_rect, init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = []                                 # 用来存储玩家飞机图片的列表
        for i in range(len(player_rect)):
            self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha())
        self.rect = player_rect[0]                  # 初始化图片所在的矩形
        self.rect.topleft = init_pos                # 初始化矩形的左上角坐标
        self.speed = 8                              # 初始化玩家飞机速度,这里是一个确定的值
        self.bullets = pygame.sprite.Group()        # 玩家飞机所发射的子弹的集合
        self.is_hit = False                         # 玩家是否被击中

    def shoot(self,bullet_img):
        bullet = Bullet(bullet_img, self.rect.midtop)
        self.bullets.add(bullet)

    def moveUp(self):
        if self.rect.top <= 0:
            self.rect.top = 0
        else:
            self.rect.top -= self.speed

    def moveDown(self):
        if self.rect.top >= SCREEN_HEIGHT - self.rect.height:
            self.rect.top = SCREEN_HEIGHT - self.rect.height
        else:
            self.rect.top += self.speed

    def moveLeft(self):
        if self.rect.left <= 0:
            self.rect.left = 0
        else:
            self.rect.left -= self.speed

    def moveRight(self):
        if self.rect.left >= SCREEN_WIDTH - self.rect.width:
            self.rect.left = SCREEN_WIDTH - self.rect.width
        else:
            self.rect.left += self.speed

# 敌人类
class Enemy(pygame.sprite.Sprite):
    def __init__(self, enemy_img, enemy_down_imgs, init_pos):
         pygame.sprite.Sprite.__init__(self)
         self.image = enemy_img              #正常的图像
         self.rect = self.image.get_rect()
         self.rect.topleft = init_pos
         self.down_imgs = enemy_down_imgs      # 爆炸的图像
         self.speed = 2
         self.down_index = 0


    def move(self):
        self.rect.top += self.speed

# 初始化游戏
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption('飞机大战')

# 载入游戏音乐
bullet_sound = pygame.mixer.Sound('sound/bullet.wav')
enemy1_down_sound = pygame.mixer.Sound('sound/enemy1_down.wav')
game_over_sound = pygame.mixer.Sound('sound/game_over.wav')
bullet_sound.set_volume(0.3)
enemy1_down_sound.set_volume(0.3)
game_over_sound.set_volume(0.3)
pygame.mixer.music.load('sound/game_music.wav')
pygame.mixer.music.play(-1,0.0)
pygame.mixer.music.set_volume(0.25)

# 载入背景图
background = pygame.image.load('image/background.png').convert()
game_over = pygame.image.load('image/gameover.png')

filename = 'image/shoot.png'
plane_img = pygame.image.load(filename)

# 设置玩家相关参数
player_rect = []
player_rect.append(pygame.Rect(0,99,102,126))   #玩家精灵图片区域
player_rect.append(pygame.Rect(165,360,102,126))
player_rect.append(pygame.Rect(165,234,102,126))
player_rect.append(pygame.Rect(330,624,102,126))
player_rect.append(pygame.Rect(330,498,102,126))
player_rect.append(pygame.Rect(432,624,102,126))
player_pos = [200,600]
player = Player(plane_img,player_rect,player_pos)

#自定义子弹对象使用的surface参数
bullet_rect = pygame.Rect(1004,987,9,21)
bulllt_img = plane_img.subsurface(bullet_rect)

# 定义敌机对象使用的surface参数
enemy1_rect = pygame.Rect(534,612,57,43)
enemy1_img = plane_img.subsurface(enemy1_rect)
enemy1_down_imgs = []
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267,347,57,43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(873,697,57,43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(267,296,57,43)))
enemy1_down_imgs.append(plane_img.subsurface(pygame.Rect(930,697,57,43)))

enemies1 = pygame.sprite.Group()

# 存储被击毁的飞机,用来渲染精灵动画
enemies_down = pygame.sprite.Group()

shoot_frequency = 0
enemy_frequency = 0

player_down_index = 16

score = 0

clock = pygame.time.Clock()

running = True

while running:
    # 控制游戏最大频率为60
    clock.tick(60)
    # 控制发射子弹频率,并发射子弹
    if not player.is_hit:
        if shoot_frequency % 15 == 0:
            bullet_sound.play()
            player.shoot(bulllt_img)
        shoot_frequency += 1
        if shoot_frequency >= 15:
            shoot_frequency = 0

    # 生成敌机
    if enemy_frequency % 50 == 0:
        enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0]
        enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos)
        enemies1.add(enemy1)
    enemy_frequency += 1
    if enemy_frequency >= 100:
        enemy_frequency = 0

    # 移动子弹,若超出窗口范围则删除
    for bullet in player.bullets:
        bullet.move()
        if bullet.rect.bottom < 0:
            player.bullets.remove(bullet)

    # 移动敌机,若超出窗口范围则删除
    for enemy in enemies1:
        enemy.move()
        # 判断玩家是否被击中
        if pygame.sprite.collide_circle(enemy, player):
            enemies_down.add(enemy)
            enemies1.remove(enemy)
            player.is_hit = True
            game_over_sound.play()
            break
        if enemy.rect.top > SCREEN_HEIGHT:
            enemies1.remove(enemy)

    # 将被击中的敌机对象添加到击毁敌机Group中,用来渲染击毁动画
    enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1)
    for enemy_down in enemies1_down:
        enemies_down.add(enemy_down)

    # 绘制背景
    screen.fill(0)
    screen.blit(background, (0, 0))

    # 绘制玩家飞机
    #if not player.is_hit:
     #   screen.blit(player.image[player.img_index], player.rect)
      #  # 更换图片索引将飞机有动画效果
       # player.img_index = shoot_frequency // 8
    #else:
     #   player.img_index = player_down_index // 8
      #  screen.blit(player.image[player.img_index], player.rect)
       # player_down_index += 1
        #if player_down_index > 47:
         #   running = False

    # 绘制玩家飞机
    if not player.is_hit:
        screen.blit(player.image[0], player.rect)  # 将正常飞机画出来
    else:
        # 玩家飞机被击中后的效果处理
        screen.blit(player.image[1], player.rect)  # 将爆炸的飞机画出来
        running = False

    # 绘制击毁动画
    for enemy_down in enemies_down:
        if enemy_down.down_index == 0:
            enemy1_down_sound.play()
        if enemy_down.down_index > 7:
            enemies_down.remove(enemy_down)
            score += 1000
            continue
        screen.blit(enemy_down.down_imgs[enemy_down.down_index // 2], enemy_down.rect)
        enemy_down.down_index += 1

    # 绘制子弹和敌机
    player.bullets.draw(screen)
    enemies1.draw(screen)

    # 绘制得分
    score_font = pygame.font.Font(None, 36)
    score_text = score_font.render(str(score), True, (128, 128, 128))
    text_rect = score_text.get_rect()
    text_rect.topleft = [10, 10]
    screen.blit(score_text, text_rect)


    #更新屏幕
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           pygame.quit()
           exit()

    # 监听键盘事件
    key_pressed = pygame.key.get_pressed()
    # 若玩家被击中,则无效
    if not player.is_hit:
        if key_pressed[K_w] or key_pressed[K_UP]:
            player.moveUp()
        if key_pressed[K_s] or key_pressed[K_DOWN]:
            player.moveDown()
        if key_pressed[K_a] or key_pressed[K_LEFT]:
            player.moveLeft()
        if key_pressed[K_d] or key_pressed[K_RIGHT]:
            player.moveRight()


font = pygame.font.Font(None, 48)
text = font.render('Score: '+ str(score), True, (255, 0, 0))
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery + 24
screen.blit(game_over, (0, 0))
screen.blit(text, text_rect)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    pygame.display.update()

运行效果:

4,2048游戏

代码参考:

import random
import sys
import pygame
from pygame.locals import *

PIXEL = 150
SCORE_PIXEL = 100
SIZE = 4

# 地图的类
class Map:

    def __init__(self, size):

        self.size = size
        self.score = 0
        self.map = [[0 for i in range(size)] for i in range(size)]
        self.add()
        self.add()

       # 新增24,有1/4概率
    def add(self):
        while True:
            p = random.randint(0, self.size * self.size - 1)
            if self.map[p // self.size][p % self.size] == 0:
                x = (random.randint(0, 3) > 0 and 2) or 4
                self.map[p // self.size][p % self.size] = x
                self.score += x
                break

        # 地图向左靠拢,其他方向的靠拢可以通过适当旋转实现,返回地图是否更新
    def adjust(self):
        changed = False
        for a in self.map:
            b = []
            last = 0
            for v in a:
                if v != 0:
                    if v == last:
                        b.append(b.pop() << 1)
                        last = 0
                    else:
                        b.append(v)
                        last = v
            b += [0] * (self.size - len(b))
            for i in range(self.size):
                if a[i] != b[i]:
                    changed = True
            a[:] = b
        return changed

       # 逆时针旋转地图90    def rotate90(self):
        self.map = [[self.map[c][r] for c in range(self.size)] for r in reversed(range(self.size))]


       # 判断游戏结束
    def over(self):
        for r in range(self.size):
            for c in range(self.size):
                 if self.map[r][c] == 0:
                    return False
        for r in range(self.size):
            for c in range(self.size - 1):
                 if self.map[r][c] == self.map[r][c + 1]:
                    return False
        return True



    def moveUp(self):

        self.rotate90()

        if self.adjust():
            self.add()

        self.rotate90()

        self.rotate90()

        self.rotate90()

    def moveRight(self):

        self.rotate90()

        self.rotate90()

        if self.adjust():
            self.add()

        self.rotate90()

        self.rotate90()

    def moveDown(self):

        self.rotate90()

        self.rotate90()

        self.rotate90()

        if self.adjust():
            self.add()

        self.rotate90()

    def moveLeft(self):

        print('90')

        if self.adjust():
            self.add()

# 更新屏幕
def show(map):
    for i in range(SIZE):
        for j in range(SIZE):
            # 背景颜色块
            screen.blit(map.map[i][j] == 0 and block[(i + j) % 2] or block[2 + (i + j) % 2], (PIXEL * j, PIXEL * i))
            # 数值显示
            if map.map[i][j] != 0:
                map_text = map_font.render(str(map.map[i][j]), True, (106, 90, 205))
                text_rect = map_text.get_rect()
                text_rect.center = (PIXEL * j + PIXEL / 2, PIXEL * i + PIXEL / 2)
                screen.blit(map_text, text_rect)
    # 分数显示
    screen.blit(score_block, (0, PIXEL * SIZE))
    score_text = score_font.render((map.over() and "Game over with score " or "Score: ") + str(map.score), True,
                                   (106, 90, 205))
    score_rect = score_text.get_rect()
    score_rect.center = (PIXEL * SIZE / 2, PIXEL * SIZE + SCORE_PIXEL / 2)
    screen.blit(score_text, score_rect)
    pygame.display.update()

map = Map(SIZE)
pygame.init()
screen = pygame.display.set_mode((PIXEL * SIZE, PIXEL * SIZE + SCORE_PIXEL))
pygame.display.set_caption('2048')
block = [pygame.Surface((PIXEL, PIXEL)) for i in range(4)]
# 设置颜色
block[0].fill((152, 251, 152))
block[1].fill((240, 255, 255))
block[2].fill((0, 255, 127))
block[3].fill((225, 255, 255))
score_block = pygame.Surface((PIXEL * SIZE, SCORE_PIXEL))
score_block.fill((245, 245, 245))
# 设置字体
map_font = pygame.font.Font(None, PIXEL )
score_font = pygame.font.Font(None, SCORE_PIXEL)
clock = pygame.time.Clock()
show(map)

while not map.over():
    # 12为实验参数
    clock.tick(12)
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    keys = pygame.key.get_pressed()
    if keys[K_w] or keys[K_UP]:
        map.moveUp()
    elif keys[K_s] or keys[K_DOWN]:
        map.moveDown()
    elif keys[K_a] or keys[K_LEFT]:
        map.moveLeft()
    elif keys[K_d] or keys[K_RIGHT]:
        map.moveRight()
    show(map)

# 游戏结束
pygame.time.delay(3000)

运行效果:

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!