专栏导读
🌸 欢迎来到Python办公自动化专栏—Python处理办公问题,解放您的双手 📕 此外还有python基础专栏:请点击——>Python基础学习专栏 求订阅 文章作者技术和水平有限,如果文中出现错误,希望大家能指正🙏 ❤️ 欢迎各位佬关注! ❤️
背景介绍
今天我们要用Python的Pygame库来创建一个简单但有趣的小汽车游戏!在这个游戏中,你将控制一辆红色小车躲避从上方驶来的蓝色敌车,看看你能坚持多久并获得多高的分数。
游戏概述
这个游戏包含以下元素:
玩家控制的红色小车 随机出现的蓝色敌车 道路边界和中间的虚线 计分系统和难度递增机制
库的安装
库 用途 安装 pygame 游戏库 pip install pygame -i https://pypi.tuna.tsinghua.edu.cn/simple/
代码解析
1. 初始化设置
首先我们导入必要的库,初始化Pygame,并设置游戏窗口的大小为400x600像素。
import pygame
import sys
import random
pygame. init( )
WIDTH, HEIGHT = 400 , 600
screen = pygame. display. set_mode( ( WIDTH, HEIGHT) )
pygame. display. set_caption( "升级版赛车游戏" )
2. 玩家车辆设置
car_width, car_height = 50 , 100
car_x = WIDTH // 2 - car_width // 2
car_y = HEIGHT - car_height - 20
car_speed = 5
3. 敌车设置
敌车大小与玩家车辆相同,初始位置随机出现在屏幕顶部上方。
enemy_width, enemy_height = 50 , 100
enemy_x = random. randint( 40 , WIDTH - 40 - enemy_width)
enemy_y = - enemy_height
enemy_speed = 5
4. 游戏元素
line_height = 40
line_width = 5
line_gap = 20
lines = [ y for y in range ( 0 , HEIGHT, line_height + line_gap) ]
GRAY = ( 50 , 50 , 50 )
WHITE = ( 255 , 255 , 255 )
RED = ( 200 , 0 , 0 )
BLUE = ( 0 , 100 , 255 )
BLACK = ( 0 , 0 , 0 )
5. 游戏主循环
running = True
while running:
screen. fill( GRAY)
for event in pygame. event. get( ) :
if event. type == pygame. QUIT:
running = False
6. 道路和车辆绘制
for i in range ( len ( lines) ) :
pygame. draw. rect( screen, WHITE, ( WIDTH // 2 - line_width // 2 , lines[ i] , line_width, line_height) )
lines[ i] += 5
if lines[ i] > HEIGHT:
lines[ i] = - line_height - line_gap
pygame. draw. rect( screen, WHITE, ( 30 , 0 , 5 , HEIGHT) )
pygame. draw. rect( screen, WHITE, ( WIDTH - 35 , 0 , 5 , HEIGHT) )
pygame. draw. rect( screen, RED, car_rect)
pygame. draw. rect( screen, BLUE, enemy_rect)
7. 玩家控制
玩家可以使用左右方向键控制车辆移动,但不能超出道路边界。
keys = pygame. key. get_pressed( )
if keys[ pygame. K_LEFT] and car_x > 40 :
car_x -= car_speed
if keys[ pygame. K_RIGHT] and car_x < WIDTH - car_width - 40 :
car_x += car_speed
8. 敌车移动和计分
# 移动敌人车
enemy_y += enemy_speed
if enemy_y > HEIGHT:
enemy_y = -enemy_height
enemy_x = random.randint(40, WIDTH - 40 - enemy_width)
score += 1 # 每次成功避开一个敌人 +1分
9. 碰撞检测
car_rect = pygame. Rect( car_x, car_y, car_width, car_height)
enemy_rect = pygame. Rect( enemy_x, enemy_y, enemy_width, enemy_height)
if car_rect. colliderect( enemy_rect) :
running = False
10. 难度递增
frame_count += 1
if frame_count % 300 == 0 :
enemy_speed += 1
level += 1
11. 游戏结束画面
screen. fill( BLACK)
game_over_text = font. render( "游戏结束" , True , RED)
final_score = font. render( f"最终得分: { score} " , True , WHITE)
screen. blit( game_over_text, ( WIDTH // 2 - 60 , HEIGHT // 2 - 40 ) )
screen. blit( final_score, ( WIDTH // 2 - 80 , HEIGHT // 2 + 10 ) )
pygame. display. update( )
pygame. time. wait( 3000 )
完整代码
import pygame
import sys
import random
pygame. init( )
WIDTH, HEIGHT = 400 , 600
screen = pygame. display. set_mode( ( WIDTH, HEIGHT) )
pygame. display. set_caption( "升级版赛车游戏" )
car_width, car_height = 50 , 100
car_x = WIDTH // 2 - car_width // 2
car_y = HEIGHT - car_height - 20
car_speed = 5
enemy_width, enemy_height = 50 , 100
enemy_x = random. randint( 40 , WIDTH - 40 - enemy_width)
enemy_y = - enemy_height
enemy_speed = 5
score = 0
font = pygame. font. SysFont( 'simhei' , 36 )
level = 1
frame_count = 0
line_height = 40
line_width = 5
line_gap = 20
lines = [ y for y in range ( 0 , HEIGHT, line_height + line_gap) ]
GRAY = ( 50 , 50 , 50 )
WHITE = ( 255 , 255 , 255 )
RED = ( 200 , 0 , 0 )
BLUE = ( 0 , 100 , 255 )
BLACK = ( 0 , 0 , 0 )
clock = pygame. time. Clock( )
running = True
while running:
screen. fill( GRAY)
for event in pygame. event. get( ) :
if event. type == pygame. QUIT:
running = False
for i in range ( len ( lines) ) :
pygame. draw. rect( screen, WHITE, ( WIDTH // 2 - line_width // 2 , lines[ i] , line_width, line_height) )
lines[ i] += 5
if lines[ i] > HEIGHT:
lines[ i] = - line_height - line_gap
keys = pygame. key. get_pressed( )
if keys[ pygame. K_LEFT] and car_x > 40 :
car_x -= car_speed
if keys[ pygame. K_RIGHT] and car_x < WIDTH - car_width - 40 :
car_x += car_speed
enemy_y += enemy_speed
if enemy_y > HEIGHT:
enemy_y = - enemy_height
enemy_x = random. randint( 40 , WIDTH - 40 - enemy_width)
score += 1
car_rect = pygame. Rect( car_x, car_y, car_width, car_height)
enemy_rect = pygame. Rect( enemy_x, enemy_y, enemy_width, enemy_height)
if car_rect. colliderect( enemy_rect) :
running = False
pygame. draw. rect( screen, WHITE, ( 30 , 0 , 5 , HEIGHT) )
pygame. draw. rect( screen, WHITE, ( WIDTH - 35 , 0 , 5 , HEIGHT) )
pygame. draw. rect( screen, RED, car_rect)
pygame. draw. rect( screen, BLUE, enemy_rect)
score_text = font. render( f"得分: { score} " , True , WHITE)
screen. blit( score_text, ( 10 , 10 ) )
frame_count += 1
if frame_count % 300 == 0 :
enemy_speed += 1
level += 1
level_text = font. render( f"等级: { level} " , True , WHITE)
screen. blit( level_text, ( WIDTH - 130 , 10 ) )
pygame. display. update( )
clock. tick( 60 )
screen. fill( BLACK)
game_over_text = font. render( "游戏结束" , True , RED)
final_score = font. render( f"最终得分: { score} " , True , WHITE)
screen. blit( game_over_text, ( WIDTH // 2 - 60 , HEIGHT // 2 - 40 ) )
screen. blit( final_score, ( WIDTH // 2 - 80 , HEIGHT // 2 + 10 ) )
pygame. display. update( )
pygame. time. wait( 3000 )
pygame. quit( )
sys. exit( )
总结
希望对初学者有帮助 致力于办公自动化的小小程序员一枚 希望能得到大家的【一个免费关注】!感谢 求个 🤞 关注 🤞 求个 ❤️ 喜欢 ❤️ 求个 👍 收藏 👍