获取python整套完整教程,3263学习笔记,源码,项目实战,全部在手,python不愁~~~
例如:
其中最经典的莫过于,超级玛丽,贪吃蛇,坦克大战,以及坤坤打篮球简直不敢多玩一下
对于摸鱼小游戏,必须分享出来,勤勤恳恳工作叫劳动报酬,摸鱼才是真正的赚钱
超级玛丽是一个经典的2D平台游戏,它可以使用Python以及Pygame库来实现。以下是一个简单的超级玛丽游戏示例代码,获取源码关注我:
实现效果:
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()
这只是一个非常简单的示例代码,你可以进一步完善,添加新的角色、敌人、场景等,以实现更加丰富的游戏功能。
沙漠打地鼠是一种休闲娱乐小游戏,可以利用Python和Pygame库来实现。以下是一个简单的沙漠打地鼠游戏示例代码,获取源码关注我:
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()
这只是一个非常简单的示例代码,你可以进一步完善,添加新的元素等,以实现更加丰富的游戏功能。
贪吃蛇是一种经典的小游戏,可以使用Python和Pygame库来实现。以下是一个简单的贪吃蛇游戏示例代码,获取源码关注我:
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()
这只是一个非常简单的示例代码,你可以进一步完善,添加新的元素(如障碍物、关卡、音效等),以实现更加丰富的游戏功能。
坦克大战是一款经典的游戏,也可以使用Python和Pygame库来实现。以下是一个简单的坦克大战游戏示例代码,获取源码关注我:
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()
这只是一个非常简单的示例代码,你可以进一步完善,添加新的元素(如音效、生命值、关卡等),以实现更加丰富的游戏功能。
五子棋是一款非常经典的游戏,也可以使用Python来编写。以下是一个基本的五子棋游戏示例代码,获取源码关注我:
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()
这是一个非常简单的示例代码,你可以根据自己的需求,进一步完善、美化该代码。