I wrote several small fishing games in Python, giving you a must-have product for fishing at work and school in 2023! (Source code attached)


Preface

Get a complete set of python tutorials, 3263 study notes, source code, and project practice, all at hand, you don’t have to worry about python~~~

Click to receive benefits for Python learning materials

Insert image description here


For example:

  • Super Mary
  • Tetris
  • Plants vs. Zombies
  • Snake
  • tank battle
  • Contra
  • whack-a-mole
  • backgammon
  • Kunkun plays basketball

Among them, the most classic ones are Super Mario, Snake, Tank Battle, and Kunkun playing basketball. I just don’t dare to play more.
Insert image description here
As for the fishing game, I must share it. Come out, work diligently is called labor reward, and fishing is the real money

1. Super Mario

Super Mario is a classic 2D platform game that can be implemented using Python and the Pygame library. The following is a simple sample code for the Super Mario game. Follow me to get the source code:

Realization effect:

Insert image description here

import pygame

pygame.init()

# 设置游戏窗口大小
win = pygame.display.set_mode((500, 480))

# 设置游戏标题
pygame.display.set_caption("Super Mario")

# 加载图片资源
walkRight = [pygame.image.load('images/pygame_right_1.png'),
             pygame.image.load('images/pygame_right_2.png'),
             pygame.image.load('images/pygame_right_3.png'),
             pygame.image.load('images/pygame_right_4.png'),
             pygame.image.load('images/pygame_right_5.png'),
             pygame.image.load('images/pygame_right_6.png')]
walkLeft = [pygame.image.load('images/pygame_left_1.png'),
            pygame.image.load('images/pygame_left_2.png'),
            pygame.image.load('images/pygame_left_3.png'),
            pygame.image.load('images/pygame_left_4.png'),
            pygame.image.load('images/pygame_left_5.png'),
            pygame.image.load('images/pygame_left_6.png')]
bg = pygame.image.load('images/pygame_bg.jpg')
char = pygame.image.load('images/pygame_stand.png')

# 设置游戏角色的初始坐标和速度
x = 50
y = 400
width = 64
height = 64
vel = 5

# 设置游戏角色左右移动的初始状态
isJump = False
jumpCount = 10
left = False
right = False
walkCount = 0

# 定义游戏循环
run = True
while run:
    pygame.time.delay(50)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and x > vel:
        x -= vel
        left = True
        right = False
    elif keys[pygame.K_RIGHT] and x < 500 - vel - width:
        x += vel
        left = False
        right = True
    else:
        left = False
        right = False
        walkCount = 0

    if not isJump:
        if keys[pygame.K_SPACE]:
            isJump = True
            left = False
            right = False
            walkCount = 0
    else:
        if jumpCount >= -10:
            y -= (jumpCount * abs(jumpCount)) * 0.2
            jumpCount -= 1
        else:
            jumpCount = 10
            isJump = False

    # 绘制游戏背景和角色
    # 先绘制背景,再绘制角色,确保角色出现在背景前面
    win.blit(bg, (0, 0))
    if walkCount + 1 >= 18:
        walkCount = 0
    if left:
        win.blit(walkLeft[walkCount // 3], (x, y))
        walkCount += 1
    elif right:
        win.blit(walkRight[walkCount // 3], (x, y))
        walkCount += 1
    else:
        win.blit(char, (x, y))

    pygame.display.update()

# 关闭游戏窗口并退出
pygame.quit()

This is just a very simple sample code, you can further improve it and add new characters, enemies, scenes, etc. to achieve richer game functions.

2. Desert whack-a-mole

Desert whack-a-mole is a casual and entertaining game that can be implemented using Python and Pygame libraries. The following is a simple example code for a desert whack-a-mole game. Follow me to get the source code:

Realization effect:
Insert image description here

import pygame
import random
import time


def draw_mole(x, y):
    global mole_img
    win.blit(mole_img, (x, y))


pygame.init()

# 设置游戏窗口大小
win = pygame.display.set_mode((500, 500))

# 设置游戏标题
pygame.display.set_caption("Desert Mole")

# 加载图片资源
bg = pygame.image.load('images/mole_bg.jpg')
mole_img = pygame.image.load('images/mole.png')

# 设置游戏角色的初始坐标和速度
text_font = pygame.font.Font(None, 40)
score = 0
x = 100
y = 100
mole_x = random.randint(50, 400) 
mole_y = random.randint(100, 360)
vel = 5

# 设置游戏循环
run = True
while run:
    pygame.time.delay(50)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= vel
    elif keys[pygame.K_RIGHT]:
        x += vel
    elif keys[pygame.K_UP]:
        y -= vel
    elif keys[pygame.K_DOWN]:
        y += vel

    # 每隔一段时间更新地鼠位置
    if time.time() % 3 == 0:
        mole_x = random.randint(50, 400) 
        mole_y = random.randint(100, 360)
    
    # 碰撞检测,如果角色和地鼠坐标重合,则得分
    if mole_x - 20 < x < mole_x + 20 and mole_y - 20 < y < mole_y + 20:
        score += 1
    
    # 绘制游戏背景和角色以及地鼠
    # 先绘制背景,再绘制地鼠,最后绘制角色,确保角色出现在最前面
    win.blit(bg, (0, 0))
    draw_mole(mole_x, mole_y)
    pygame.draw.circle(win, (255, 255, 255), (x, y), 20, 0)
    
    # 绘制分数统计文本
    text = text_font.render("Score: " + str(score), True, (255, 255, 255))
    win.blit(text, (10, 10))

    pygame.display.update()

# 关闭游戏窗口并退出
pygame.quit()

This is just a very simple sample code, you can further improve it, add new elements, etc., to achieve richer game functions.

3. Greedy Snake

Snake is a classic mini-game that can be implemented using Python and Pygame libraries. The following is a simple sample code for the Snake game. Follow me to get the source code:

Realization effect:
Insert image description here

import pygame
import random

pygame.init()

# 设置游戏窗口大小
win_size = (500, 500)
win = pygame.display.set_mode(win_size)

# 设置游戏标题
pygame.display.set_caption("Snake")

# 设置游戏元素的大小和速度
fps = 60
snake_size = 10
food_size = 10
vel = 10

# 设置游戏字符颜色
white = (255, 255, 255)
black = (0, 0, 0)

# 设置游戏角色初始位置和长度
x = 250
y = 250
snake_list = [[x, y], [x-vel, y], [x-(2*vel), y]]
snake_length = 3

# 设置食物初始位置
food_x = round(random.randrange(0, win_size[0]-snake_size) / 10.0) * 10.0
food_y = round(random.randrange(0, win_size[1]-snake_size) / 10.0) * 10.0

# 定义游戏循环
run = True
clock = pygame.time.Clock()

while run:
    # 设置游戏帧率
    clock.tick(fps)

    # 获取键盘事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # 获取键盘的输入值
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= vel
    if keys[pygame.K_RIGHT]:
        x += vel
    if keys[pygame.K_UP]:
        y -= vel
    if keys[pygame.K_DOWN]:
        y += vel

    # 绘制游戏背景和食物
    win.fill(black)
    pygame.draw.rect(win, white, [food_x, food_y, food_size, food_size])

    # 绘制蛇
    snake = []
    snake.append(x)
    snake.append(y)
    snake_list.append(snake)
    if len(snake_list) > snake_length:
        del snake_list[0]

    for seg in snake_list[:-1]:
        if seg == snake:
            run = False

    for s in snake_list:
        pygame.draw.rect(win, white, [s[0], s[1], snake_size, snake_size])

    # 判断蛇是否吃到食物
    if x == food_x and y == food_y:
        food_x = round(random.randrange(0, win_size[0]-snake_size) / 10.0) * 10.0
        food_y = round(random.randrange(0, win_size[1]-snake_size) / 10.0) * 10.0
        snake_length += 1

    # 更新游戏窗口
    pygame.display.update()

# 退出游戏
pygame.quit()

This is just a very simple sample code, you can further improve it and add new elements (such as obstacles, levels, sound effects, etc.) to achieve richer game functions.

4. Tank battle

Tank Battle is a classic game that can also be implemented using Python and the Pygame library. The following is a simple example code for a tank battle game. Follow me to get the source code:

Realization effect:
Insert image description here

import pygame
import random

# 设置游戏窗口大小
win_size = (800, 600)
win = pygame.display.set_mode(win_size)
pygame.display.set_caption("Tank Game")

# 加载游戏元素
bg_img = pygame.image.load("background.png")
player_img = pygame.image.load("player_tank.png")
enemy_img = pygame.image.load("enemy_tank.png")
bullet_img = pygame.image.load("bullet.png")

# 设置游戏元素的大小和速度
player_size = player_img.get_rect().size
player_x = (win_size[0] - player_size[0]) // 2
player_y = win_size[1] - player_size[1]
player_speed = 5

enemy_size = enemy_img.get_rect().size
enemy_list = []
enemy_speed = 3

bullet_size = bullet_img.get_rect().size
bullet_speed = 7

# 定义游戏角色类
class Tank:
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.speed = player_speed
    
    def move_left(self):
        self.x -= self.speed
        if self.x < 0:
            self.x = 0
    
    def move_right(self):
        self.x += self.speed
        if self.x > win_size[0] - player_size[0]:
            self.x = win_size[0] - player_size[0]
    
    def draw(self):
        win.blit(self.img, (self.x, self.y))

# 定义子弹类
class Bullet:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.img = bullet_img
    
    def move(self):
        self.y -= bullet_speed
    
    def draw(self):
        win.blit(self.img, (self.x, self.y))

# 初始化玩家坦克和敌方坦克
player_tank = Tank(player_x, player_y, player_img)
for i in range(5):
    enemy_x = random.randint(0, win_size[0] - enemy_size[0])
    enemy_y = random.randint(0, win_size[1] // 2)
    enemy_tank = Tank(enemy_x, enemy_y, enemy_img)
    enemy_list.append(enemy_tank)

# 定义游戏循环
run = True
clock = pygame.time.Clock()

while run:
    clock.tick(60)

    # 获取键盘事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    # 获取键盘的输入值
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player_tank.move_left()
    if keys[pygame.K_RIGHT]:
        player_tank.move_right()
    if keys[pygame.K_SPACE]:
        bullet_x = player_tank.x + player_size[0] // 2 - bullet_size[0] // 2
        bullet_y = player_tank.y
        bullet = Bullet(bullet_x, bullet_y)
        bullets.append(bullet)

    # 移动子弹
    for bullet in bullets:
        bullet.move()
        if bullet.y < 0:
            bullets.remove(bullet)

    # 移动敌方坦克
    for enemy_tank in enemy_list:
        enemy_tank.y += enemy_speed
        if enemy_tank.y > win_size[1]:
            enemy_tank.y = 0
            enemy_tank.x = random.randint(0, win_size[0] - enemy_size[0])

    # 检测子弹是否击中敌方坦克
    for bullet in bullets:
        for enemy_tank in enemy_list:
            if bullet.x > enemy_tank.x and bullet.x < enemy_tank.x + enemy_size[0]:
                if bullet.y > enemy_tank.y and bullet.y < enemy_tank.y + enemy_size[1]:
                    bullets.remove(bullet)
                    enemy_list.remove(enemy_tank)

    # 绘制游戏元素
    win.blit(bg_img, (0, 0))
    for bullet in bullets:
        bullet.draw()
    for enemy_tank in enemy_list:
        enemy_tank.draw()
    player_tank.draw()

    # 更新游戏窗口
    pygame.display.update()

# 退出游戏
pygame.quit()

This is just a very simple sample code, you can further improve it and add new elements (such as sound effects, health points, levels, etc.) to achieve richer game functions.

5. Backgammon

Backgammon is a very classic game that can also be written in Python. The following is a basic backgammon game sample code, follow me to get the source code:

Realization effect:
Insert image description here

import pygame
import sys

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLOCK_SIZE = 40
MARGIN = 40
WIDTH = BLOCK_SIZE * 15 + MARGIN * 2
HEIGHT = BLOCK_SIZE * 15 + MARGIN * 2

# 初始化 pygame
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('五子棋')

# 画棋盘
def draw_board():
    for i in range(15):
        pygame.draw.line(screen, BLACK, [MARGIN + i * BLOCK_SIZE, MARGIN], 
                         [MARGIN + i * BLOCK_SIZE, HEIGHT - MARGIN], 1)
        pygame.draw.line(screen, BLACK, [MARGIN, MARGIN + i * BLOCK_SIZE], 
                        [WIDTH - MARGIN, MARGIN + i * BLOCK_SIZE], 1)

# 画棋子
def draw_piece(board):
    for row in range(15):
        for col in range(15):
            if board[row][col] == 1:
                pygame.draw.circle(screen, BLACK, [MARGIN + col * BLOCK_SIZE, MARGIN + row * BLOCK_SIZE], BLOCK_SIZE // 2 -2)
            elif board[row][col] == 2:
                pygame.draw.circle(screen, WHITE, [MARGIN + col * BLOCK_SIZE, MARGIN + row * BLOCK_SIZE], BLOCK_SIZE // 2 -2)

def is_win(board):
    # 横向连续五子
    for row in range(15):
        for col in range(11):
            if board[row][col] == board[row][col+1] == board[row][col+2] == board[row][col+3] == board[row][col+4] and board[row][col] != 0:
                return True
    # 纵向连续五子
    for row in range(11):
        for col in range(15):
            if board[row][col] == board[row+1][col] == board[row+2][col] == board[row+3][col] == board[row+4][col] and board[row][col] != 0:
                return True
    # 右斜向连续五子
    for row in range(11):
        for col in range(11):
            if board[row][col] == board[row+1][col+1] == board[row+2][col+2] == board[row+3][col+3] == board[row+4][col+4] and board[row][col] != 0:
                return True
    # 左斜向连续五子
    for row in range(11):
        for col in range(4, 15):
            if board[row][col] == board[row+1][col-1] == board[row+2][col-2] == board[row+3][col-3] == board[row+4][col-4] and board[row][col] != 0:
                return True
    return False

def main():
    board = [[0 for i in range(15)] for j in range(15)]
    turn = 1
    draw_board()
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                x, y = pygame.mouse.get_pos()
                col = (x - MARGIN) // BLOCK_SIZE
                row = (y - MARGIN) // BLOCK_SIZE
                if board[row][col] == 0:
                    board[row][col] = turn
                    if is_win(board):
                        print('Player %d wins!' % (turn))
                        sys.exit()
                    if turn == 1:
                        turn = 2
                    else:
                        turn =1
                    draw_piece(board)
                    pygame.display.flip()
main()

This is a very simple sample code. You can further improve and beautify the code according to your own needs.

Insert image description here

↓ ↓ ↓ Add the business card below to find me and get the source code and cases directly ↓ ↓ ↓

Guess you like

Origin blog.csdn.net/weixin_45841831/article/details/133657659