大家好,我是小F~

经常听到有朋友说,学习编程是一件非常枯燥无味的事情。其实,大家有没有认真想过,可能是我们的学习方法不对?

比方说,你有没有想过,可以通过打游戏来学编程?

今天我想跟大家分享30个Python小游戏,教你如何通过边打游戏边学编程!

相关文件及代码都已上传,公众号回复【游戏】即可获取。

接下来就一起来看看吧~

1、飞机大战

源码分享:

import random
import pygame
from objects import Background, Player, Enemy, Bullet, Explosion, Fuel, \Powerup, Button, Message, BlinkingTextpygame.init()
SCREEN = WIDTH, HEIGHT = 288, 512info = pygame.display.Info()
width = info.current_w
height = info.current_hif width >= height:win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
else:win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)clock = pygame.time.Clock()
FPS = 60# COLORS **********************************************************************WHITE = (255, 255, 255)
BLUE = (30, 144,255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 20)# IMAGES **********************************************************************plane_img = pygame.image.load('Assets/plane.png')
logo_img = pygame.image.load('Assets/logo.png')
fighter_img = pygame.image.load('Assets/fighter.png')
clouds_img = pygame.image.load('Assets/clouds.png')
clouds_img = pygame.transform.scale(clouds_img, (WIDTH, 350))home_img = pygame.image.load('Assets/Buttons/homeBtn.png')
replay_img = pygame.image.load('Assets/Buttons/replay.png')
sound_off_img = pygame.image.load("Assets/Buttons/soundOffBtn.png")
sound_on_img = pygame.image.load("Assets/Buttons/soundOnBtn.png")# BUTTONS *********************************************************************home_btn = Button(home_img, (24, 24), WIDTH // 4 - 18, HEIGHT//2 + 120)
replay_btn = Button(replay_img, (36,36), WIDTH // 2  - 18, HEIGHT//2 + 115)
sound_btn = Button(sound_on_img, (24, 24), WIDTH - WIDTH // 4 - 18, HEIGHT//2 + 120)# FONTS ***********************************************************************game_over_font = 'Fonts/ghostclan.ttf'
tap_to_play_font = 'Fonts/BubblegumSans-Regular.ttf'
score_font = 'Fonts/DalelandsUncialBold-82zA.ttf'
final_score_font = 'Fonts/DroneflyRegular-K78LA.ttf'game_over_msg = Message(WIDTH//2, 230, 30, 'Game Over', game_over_font, WHITE, win)
score_msg = Message(WIDTH-50, 28, 30, '0', final_score_font, RED, win)
final_score_msg = Message(WIDTH//2, 280, 30, '0', final_score_font, RED, win)
tap_to_play_msg = tap_to_play = BlinkingText(WIDTH//2, HEIGHT-60, 25, "Tap To Play",tap_to_play_font, WHITE, win)# SOUNDS **********************************************************************player_bullet_fx = pygame.mixer.Sound('Sounds/gunshot.wav')
click_fx = pygame.mixer.Sound('Sounds/click.mp3')
collision_fx = pygame.mixer.Sound('Sounds/mini_exp.mp3')
blast_fx = pygame.mixer.Sound('Sounds/blast.wav')
fuel_fx = pygame.mixer.Sound('Sounds/fuel.wav')pygame.mixer.music.load('Sounds/Defrini - Spookie.mp3')
pygame.mixer.music.play(loops=-1)
pygame.mixer.music.set_volume(0.1)# GROUPS & OBJECTS ************************************************************bg = Background(win)
p = Player(144, HEIGHT - 100)enemy_group = pygame.sprite.Group()
player_bullet_group = pygame.sprite.Group()
enemy_bullet_group = pygame.sprite.Group()
explosion_group = pygame.sprite.Group()
fuel_group = pygame.sprite.Group()
powerup_group = pygame.sprite.Group()# FUNCTIONS *******************************************************************def shoot_bullet():x, y = p.rect.center[0], p.rect.yif p.powerup > 0:for dx in range(-3, 4):b = Bullet(x, y, 4, dx)player_bullet_group.add(b)p.powerup -= 1else:b = Bullet(x-30, y, 6)player_bullet_group.add(b)b = Bullet(x+30, y, 6)player_bullet_group.add(b)player_bullet_fx.play()def reset():enemy_group.empty()player_bullet_group.empty()enemy_bullet_group.empty()explosion_group.empty()fuel_group.empty()powerup_group.empty()p.reset(p.x, p.y)# VARIABLES *******************************************************************level = 1
plane_destroy_count = 0
plane_frequency = 5000
start_time = pygame.time.get_ticks()moving_left = False
moving_right = Falsehome_page = True
game_page = False
score_page = Falsescore = 0
sound_on = Truerunning = True
while running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE or event.key == pygame.K_q:running = Falseif event.type == pygame.KEYDOWN and game_page:if event.key == pygame.K_LEFT:moving_left = Trueif event.key == pygame.K_RIGHT:moving_right = Trueif event.key == pygame.K_SPACE:shoot_bullet()if event.type == pygame.MOUSEBUTTONDOWN:if home_page:home_page = Falsegame_page = Trueclick_fx.play()elif game_page:x, y = event.posif p.rect.collidepoint((x,y)):shoot_bullet()elif x <= WIDTH // 2:moving_left = Trueelif x > WIDTH // 2:moving_right = Trueif event.type == pygame.KEYUP:moving_left = Falsemoving_right = Falseif event.type == pygame.MOUSEBUTTONUP:moving_left = Falsemoving_right = Falseif home_page:win.fill(BLACK)win.blit(logo_img, (30, 80))win.blit(fighter_img, (WIDTH//2 - 50, HEIGHT//2))pygame.draw.circle(win, WHITE, (WIDTH//2, HEIGHT//2 + 50), 50, 4)tap_to_play_msg.update()if score_page:win.fill(BLACK)win.blit(logo_img, (30, 50))game_over_msg.update()final_score_msg.update(score)if home_btn.draw(win):home_page = Truegame_page = Falsescore_page = Falsereset()click_fx.play()plane_destroy_count = 0level = 1score = 0if replay_btn.draw(win):score_page = Falsegame_page = Truereset()click_fx.play()plane_destroy_count = 0score = 0if sound_btn.draw(win):sound_on = not sound_onif sound_on:sound_btn.update_image(sound_on_img)pygame.mixer.music.play(loops=-1)else:sound_btn.update_image(sound_off_img)pygame.mixer.music.stop()if game_page:current_time = pygame.time.get_ticks()delta_time = current_time - start_timeif delta_time >= plane_frequency:if level == 1:type = 1elif level == 2:type = 2elif level == 3:type = 3elif level == 4:type = random.randint(4, 5)elif level == 5:type = random.randint(1, 5)x = random.randint(10, WIDTH - 100)e = Enemy(x, -150, type)enemy_group.add(e)start_time = current_timeif plane_destroy_count:if plane_destroy_count % 5 == 0 and level < 5:level += 1plane_destroy_count = 0p.fuel -= 0.05bg.update(1)win.blit(clouds_img, (0, 70))p.update(moving_left, moving_right, explosion_group)p.draw(win)player_bullet_group.update()player_bullet_group.draw(win)enemy_bullet_group.update()enemy_bullet_group.draw(win)explosion_group.update()explosion_group.draw(win)fuel_group.update()fuel_group.draw(win)powerup_group.update()powerup_group.draw(win)enemy_group.update(enemy_bullet_group, explosion_group)enemy_group.draw(win)if p.alive:player_hit = pygame.sprite.spritecollide(p, enemy_bullet_group, False)for bullet in player_hit:p.health -= bullet.damagex, y = bullet.rect.centerexplosion = Explosion(x, y, 1)explosion_group.add(explosion)bullet.kill()collision_fx.play()for bullet in player_bullet_group:planes_hit = pygame.sprite.spritecollide(bullet, enemy_group, False)for plane in planes_hit:plane.health -= bullet.damageif plane.health <= 0:x, y = plane.rect.centerrand = random.random()if rand >= 0.9:power = Powerup(x, y)powerup_group.add(power)elif rand >= 0.3:fuel = Fuel(x, y)fuel_group.add(fuel)plane_destroy_count += 1blast_fx.play()x, y = bullet.rect.centerexplosion = Explosion(x, y, 1)explosion_group.add(explosion)bullet.kill()collision_fx.play()player_collide = pygame.sprite.spritecollide(p, enemy_group, True)if player_collide:x, y = p.rect.centerexplosion = Explosion(x, y, 2)explosion_group.add(explosion)x, y = player_collide[0].rect.centerexplosion = Explosion(x, y, 2)explosion_group.add(explosion)p.health = 0p.alive = Falseif pygame.sprite.spritecollide(p, fuel_group, True):p.fuel += 25if p.fuel >= 100:p.fuel = 100fuel_fx.play()if pygame.sprite.spritecollide(p, powerup_group, True):p.powerup += 2fuel_fx.play()if not p.alive or p.fuel <= -10:if len(explosion_group) == 0:game_page = Falsescore_page = Truereset()score += 1score_msg.update(score)fuel_color = RED if p.fuel <= 40 else GREENpygame.draw.rect(win, fuel_color, (30, 20, p.fuel, 10), border_radius=4)pygame.draw.rect(win, WHITE, (30, 20, 100, 10), 2, border_radius=4)pygame.draw.rect(win, BLUE, (30, 32, p.health, 10), border_radius=4)pygame.draw.rect(win, WHITE, (30, 32, 100, 10), 2, border_radius=4)win.blit(plane_img, (10, 15))pygame.draw.rect(win, WHITE, (0,0, WIDTH, HEIGHT), 5, border_radius=4)clock.tick(FPS)pygame.display.update()pygame.quit()

2、愤怒的墙

源码分享:

import pygame
import randomfrom objects import Player, Bar, Ball, Block, ScoreCard, Message, Particle, generate_particlespygame.init()
SCREEN = WIDTH, HEIGHT = 288, 512info = pygame.display.Info()
width = info.current_w
height = info.current_hif width >= height:win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
else:win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)clock = pygame.time.Clock()
FPS = 45# COLORSRED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (54, 69, 79)
c_list = [RED, BLACK, WHITE]# Fontspygame.font.init()
score_font = pygame.font.Font('Fonts/BubblegumSans-Regular.ttf', 50)# Soundscoin_fx = pygame.mixer.Sound('Sounds/coin.mp3')
death_fx = pygame.mixer.Sound('Sounds/death.mp3')
move_fx = pygame.mixer.Sound('Sounds/move.mp3')# backgroundsbg_list = []
for i in range(1,5):if i == 2:ext = "jpeg"else:ext = "jpg"img = pygame.image.load(f"Assets/Backgrounds/bg{i}.{ext}")img = pygame.transform.scale(img, (WIDTH, HEIGHT))bg_list.append(img)home_bg = pygame.image.load(f"Assets/Backgrounds/home.jpeg")bg = home_bg# objects
bar_group = pygame.sprite.Group()
ball_group = pygame.sprite.Group()
block_group = pygame.sprite.Group()
destruct_group = pygame.sprite.Group()
win_particle_group = pygame.sprite.Group()
bar_gap = 120particles = []p = Player(win)
score_card = ScoreCard(140, 40, win)# Functionsdef destroy_bird():x, y = p.rect.centerfor i in range (50):c = random.choice(c_list)particle = Particle(x,y, 1,c, win)destruct_group.add(particle)def win_particles():for x,y in [(40, 120), (WIDTH - 20, 240), (15, HEIGHT - 30)]:for i in range(10):particle = Particle (x,y, 2, WHITE, win)win_particle_group.add(particle)# Messages
title_font = "Fonts/Robus-BWqOd.otf"
dodgy = Message(134, 90, 100, "Angry",title_font, WHITE, win)
walls = Message(164, 145, 80, "Walls",title_font, WHITE, win)tap_to_play_font = "Fonts/DebugFreeTrial-MVdYB.otf"
tap_to_play = Message(144, 400, 32, "TAP TO PLAY",tap_to_play_font, WHITE, win)
tap_to_replay = Message(144, 400, 30, "Tap to Replay",tap_to_play_font, WHITE, win)# Variablesbar_width_list = [i for i in range (40,150,10)]
bar_frequency = 1200
bar_speed = 4
touched = False
pos = None
home_page = True
score_page = False
bird_dead = False
score = 0
high_score = 0
move_left = False
move_right = True
prev_x = 0
p_count = 0running = True
while running:win.blit(bg, (0,0))for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE:running = Falseif event.type == pygame.MOUSEBUTTONDOWN and (home_page or score_page):home_page = Falsescore_page = Falsewin_particle_group.empty()bg = random.choice(bg_list)particles = []last_bar = pygame.time.get_ticks() - bar_frequencynext_bar = 0bar_speed = 4bar_frequency = 1200bird_dead = Falsescore = 0p_count = 0score_list = []for _ in range(15):x = random.randint(30, WIDTH - 30)y = random.randint(60, HEIGHT - 60)max = random.randint(8,16)b = Block(x,y,max, win)block_group.add(b)if event.type == pygame.MOUSEBUTTONDOWN and not home_page:if p.rect.collidepoint(event.pos):touched = Truex, y = event.posoffset_x = p.rect.x - xif event.type == pygame.MOUSEBUTTONUP and not home_page:touched = Falseif event.type == pygame.MOUSEMOTION and not home_page:if touched:x, y = event.posif move_right and prev_x > x:move_right = Falsemove_left = Truemove_fx.play()if move_left and  prev_x < x:move_right = Truemove_left = Falsemove_fx.play()prev_x = xp.rect.x =  x + offset_xif home_page:bg = home_bgparticles = generate_particles(p, particles, WHITE, win)dodgy.update()walls.update()tap_to_play.update()p.update()elif score_page:bg = home_bgparticles = generate_particles(p, particles, WHITE, win)tap_to_replay.update()p.update()score_msg.update()score_point.update()if p_count % 5 == 0:win_particles()p_count += 1win_particle_group.update()else:next_bar = pygame.time.get_ticks()if next_bar - last_bar >= bar_frequency and not bird_dead:bwidth = random.choice(bar_width_list)b1prime = Bar(0,0,bwidth+3,GRAY, win)b1 = Bar(0,-3,bwidth,WHITE,win)b2prime = Bar(bwidth+bar_gap+3, 0, WIDTH - bwidth - bar_gap, GRAY, win)b2 = Bar(bwidth+bar_gap, -3, WIDTH - bwidth - bar_gap, WHITE, win)bar_group.add(b1prime)bar_group.add(b1)bar_group.add(b2prime)bar_group.add(b2)color = random.choice(["red", "white"])pos = random.choice([0,1])if pos == 0:x = bwidth + 12elif pos == 1:x = bwidth + bar_gap - 12ball = Ball(x, 10, 1, color, win)ball_group.add(ball)last_bar = next_barfor ball in ball_group:if ball.rect.colliderect(p):if ball.color == "white":ball.kill()coin_fx.play()score += 1if score > high_score:high_score += 1score_card.animate = Trueelif ball.color == "red":if not bird_dead:death_fx.play()destroy_bird()bird_dead = Truebar_speed = 0if pygame.sprite.spritecollide(p, bar_group, False):if not bird_dead:death_fx.play()destroy_bird()bird_dead = Truebar_speed = 0block_group.update()bar_group.update(bar_speed)ball_group.update(bar_speed)if bird_dead:destruct_group.update()score_card.update(score)if not bird_dead:particles = generate_particles(p, particles, WHITE, win)p.update()if score and score % 10 == 0:rem = score // 10if rem not in score_list:score_list.append(rem)bar_speed += 1bar_frequency -= 200if bird_dead and len(destruct_group) == 0:score_page = Truefont =  "Fonts/BubblegumSans-Regular.ttf"if score < high_score:score_msg = Message(144, 60, 55, "Score",font, WHITE, win)else:score_msg = Message(144, 60, 55, "New High",font, WHITE, win)score_point = Message(144, 110, 45, f"{score}", font, WHITE, win)if score_page:block_group.empty()bar_group.empty()ball_group.empty()p.reset()clock.tick(FPS)pygame.display.update()pygame.quit()

3、圆弧冲刺

源码分享:

import random
import pygamefrom objects import Player, Balls, Dot, Shadow, Particle, Message, BlinkingText, Buttonpygame.init()
SCREEN = WIDTH, HEIGHT = 288, 512
CENTER = WIDTH //2, HEIGHT // 2info = pygame.display.Info()
width = info.current_w
height = info.current_hif width >= height:win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
else:win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)clock = pygame.time.Clock()
FPS = 60# COLORS **********************************************************************RED = (255,0,0)
GREEN = (0,177,64)
BLUE = (30, 144,255)
ORANGE = (252,76,2)
YELLOW = (254,221,0)
PURPLE = (155,38,182)
AQUA = (0,103,127)
WHITE = (255,255,255)
BLACK = (0,0,0)color_list = [RED, GREEN, BLUE, ORANGE, YELLOW, PURPLE]
color_index = 0
color = color_list[color_index]# FONTS ***********************************************************************title_font = "Fonts/Aladin-Regular.ttf"
tap_to_play_font = "Fonts/BubblegumSans-Regular.ttf"
score_font = "Fonts/DalelandsUncialBold-82zA.ttf"
game_over_font = "Fonts/ghostclan.ttf"# MESSAGES ********************************************************************arc = Message(WIDTH-90, 200, 80, "Arc", title_font, WHITE, win)
dash = Message(80, 300, 60, "Dash", title_font, WHITE, win)
tap_to_play = BlinkingText(WIDTH//2, HEIGHT-60, 20, "Tap To Play", tap_to_play_font, WHITE, win)
game_msg = Message(80, 150, 40, "GAME", game_over_font, BLACK, win)
over_msg = Message(210, 150, 40, "OVER!", game_over_font, WHITE, win)
score_text = Message(90, 230, 20, "SCORE", None, BLACK, win)
best_text = Message(200, 230, 20, "BEST", None, BLACK, win)score_msg = Message(WIDTH-60, 50, 50, "0", score_font, WHITE, win)
final_score_msg = Message(90, 280, 40, "0", tap_to_play_font, BLACK, win)
high_score_msg = Message(200, 280, 40, "0", tap_to_play_font, BLACK, win)# SOUNDS **********************************************************************score_fx = pygame.mixer.Sound('Sounds/point.mp3')
death_fx = pygame.mixer.Sound('Sounds/dead.mp3')
score_page_fx = pygame.mixer.Sound('Sounds/score_page.mp3')pygame.mixer.music.load('Sounds/hk.mp3')
pygame.mixer.music.play(loops=-1)
pygame.mixer.music.set_volume(0.5)# Button imageshome_img = pygame.image.load('Assets/homeBtn.png')
replay_img = pygame.image.load('Assets/replay.png')
sound_off_img = pygame.image.load("Assets/soundOffBtn.png")
sound_on_img = pygame.image.load("Assets/soundOnBtn.png")# Buttonshome_btn = Button(home_img, (24, 24), WIDTH // 4 - 18, 390)
replay_btn = Button(replay_img, (36,36), WIDTH // 2  - 18, 382)
sound_btn = Button(sound_on_img, (24, 24), WIDTH - WIDTH // 4 - 18, 390)# GAME VARIABLES **************************************************************MAX_RAD = 120
rad_delta = 50# OBJECTS *********************************************************************ball_group = pygame.sprite.Group()
dot_group = pygame.sprite.Group()
shadow_group = pygame.sprite.Group()
particle_group = pygame.sprite.Group()
p = Player(win)ball_positions = [(CENTER[0]-105, CENTER[1]), (CENTER[0]+105, CENTER[1]),(CENTER[0]-45, CENTER[1]), (CENTER[0]+45, CENTER[1]),(CENTER[0], CENTER[1]-75), (CENTER[0], CENTER[1]+75)]
for index, pos in enumerate(ball_positions):if index in (0,1):type_ = 1inverter = 5if index in (2,3):type_ = 1inverter = 3if index in (4,5):type_ = 2inverter = 1ball = Balls(pos, type_, inverter, win)ball_group.add(ball)dot_list = [(CENTER[0], CENTER[1]-MAX_RAD+3), (CENTER[0]+MAX_RAD-3, CENTER[1]),(CENTER[0], CENTER[1]+MAX_RAD-3), (CENTER[0]-MAX_RAD+3, CENTER[1])]
dot_index = random.choice([1,2,3,4])
dot_pos = dot_list[dot_index-1]
dot = Dot(*dot_pos, win)
dot_group.add(dot)shadow = Shadow(dot_index, win)
shadow_group.add(shadow)# VARIABLES *******************************************************************clicked = False
num_clicks = 0
player_alive = True
sound_on = Truescore = 0
highscore = 0home_page = True
game_page = False
score_page = Falserunning = True
while running:win.fill(color)for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE or \event.key == pygame.K_q:running = Falseif event.type == pygame.MOUSEBUTTONDOWN and home_page:home_page = Falsegame_page = Truescore_page = Falserad_delta = 50clicked = Truescore = 0num_clicks = 0player_alive = Trueif event.type == pygame.MOUSEBUTTONDOWN and game_page:if not clicked:clicked = Truefor ball in ball_group:if num_clicks % ball.inverter == 0:ball.dtheta *= -1p.set_move(dot_index)num_clicks += 1if num_clicks % 5 == 0:color_index += 1if color_index > len(color_list) - 1:color_index = 0color = color_list[color_index]if event.type == pygame.MOUSEBUTTONDOWN and game_page:clicked = Falseif home_page:for radius in [30, 60, 90, 120]:pygame.draw.circle(win, (0,0,0), CENTER, radius, 8)pygame.draw.circle(win, (255,255,255), CENTER, radius, 5)pygame.draw.rect(win, color, [CENTER[0]-10, CENTER[1]-MAX_RAD, MAX_RAD+50, MAX_RAD])pygame.draw.rect(win, color, [CENTER[0]-MAX_RAD, CENTER[1]-10, MAX_RAD, MAX_RAD+50])arc.update()dash.update()tap_to_play.update()if score_page:game_msg.update()over_msg.update()score_text.update(shadow=False)best_text.update(shadow=False)final_score_msg.update(score, shadow=False)high_score_msg.update(highscore, shadow=False)if home_btn.draw(win):home_page = Truescore_page = Falsegame_page = Falsescore = 0score_msg = Message(WIDTH-60, 50, 50, "0", score_font, WHITE, win)if replay_btn.draw(win):home_page = Falsescore_page = Falsegame_page = Trueplayer_alive = Truescore = 0score_msg = Message(WIDTH-60, 50, 50, "0", score_font, WHITE, win)p = Player(win)if sound_btn.draw(win):sound_on = not sound_onif sound_on:sound_btn.update_image(sound_on_img)pygame.mixer.music.play(loops=-1)else:sound_btn.update_image(sound_off_img)pygame.mixer.music.stop()if game_page:for radius in [30 + rad_delta, 60 + rad_delta, 90 + rad_delta, 120 + rad_delta]:if rad_delta > 0:radius -= 1rad_delta -= 1pygame.draw.circle(win, (0,0,0), CENTER, radius, 5)pygame.draw.rect(win, color, [CENTER[0]-10, CENTER[1]-MAX_RAD, 20, MAX_RAD*2])pygame.draw.rect(win, color, [CENTER[0]-MAX_RAD, CENTER[1]-10, MAX_RAD*2, 20])if rad_delta <= 0:p.update(player_alive, color, shadow_group)shadow_group.update()ball_group.update()dot_group.update()particle_group.update()score_msg.update(score)for dot in dot_group:if dot.rect.colliderect(p):dot.kill()score_fx.play()score += 1if highscore <= score:highscore = scoreif pygame.sprite.spritecollide(p, ball_group, False) and player_alive:death_fx.play()x, y = p.rect.centerfor i in range(20):particle = Particle(x, y, WHITE, win)particle_group.add(particle)player_alive = Falsep.reset()if p.can_move and len(dot_group) == 0 and player_alive:dot_index = random.randint(1,4)dot_pos = dot_list[dot_index-1]dot = Dot(*dot_pos, win)dot_group.add(dot)shadow_group.empty()shadow = Shadow(dot_index, win)shadow_group.add(shadow)if not player_alive and len(particle_group) == 0:game_page = Falsescore_page = Truedot_group.empty()shadow_group.empty()for ball in ball_group:ball.reset()score_page_fx.play()pygame.draw.rect(win, WHITE, (0, 0, WIDTH, HEIGHT), 5, border_radius=10)clock.tick(FPS)pygame.display.update()pygame.quit()

4、行星游戏

源码分享:

import random
import pygame
from pygame.locals import KEYDOWN, QUIT, K_ESCAPE, K_SPACE, K_q, K_efrom objects import Rocket, Asteroid, Bullet, Explosion### SETUP *********************************************************************
SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 500, 500pygame.mixer.init()
pygame.init()
clock = pygame.time.Clock()
win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Asteroids')gunshot_sound = pygame.mixer.Sound("music/laser.wav")
explosion_sound = pygame.mixer.Sound("music/explosion.mp3")font = pygame.font.Font('freesansbold.ttf', 32)
# text = font.render('', True, green, blue)### Objects & Events **********************************************************
ADDAST1 = pygame.USEREVENT + 1
ADDAST2 = pygame.USEREVENT + 2
ADDAST3 = pygame.USEREVENT + 3
ADDAST4 = pygame.USEREVENT + 4
ADDAST5 = pygame.USEREVENT + 5
pygame.time.set_timer(ADDAST1, 2000)
pygame.time.set_timer(ADDAST2, 6000)
pygame.time.set_timer(ADDAST3, 10000)
pygame.time.set_timer(ADDAST4, 15000)
pygame.time.set_timer(ADDAST5, 20000)rocket = Rocket(SIZE)asteroids = pygame.sprite.Group()
bullets = pygame.sprite.Group()
explosions = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(rocket)backgrounds = [f'assets/background/bg{i}s.png' for i in range(1,5)]
bg = pygame.image.load(random.choice(backgrounds))startbg = pygame.image.load('assets/start.jpg')### Game **********************************************************************
if __name__ == '__main__':score = 0running = TruegameStarted = FalsemusicStarted = Falsewhile running:if not gameStarted:if not musicStarted:pygame.mixer.music.load('music/Apoxode_-_Electric_1.mp3')pygame.mixer.music.play(loops=-1)musicStarted = Truefor event in pygame.event.get():if event.type == QUIT:running = Falseif event.type == KEYDOWN:if event.key == K_SPACE:gameStarted = TruemusicStarted = Falsewin.blit(startbg, (0,0))        else:if not musicStarted:pygame.mixer.music.load('music/rpg_ambience_-_exploration.ogg')pygame.mixer.music.play(loops=-1)musicStarted = Truefor event in pygame.event.get():if event.type == QUIT:running = Falseif event.type == KEYDOWN:if event.key == K_ESCAPE:running = Falseif event.key == K_SPACE:pos = rocket.rect[:2]bullet = Bullet(pos, rocket.dir, SIZE)bullets.add(bullet)all_sprites.add(bullet)gunshot_sound.play()if event.key == K_q:rocket.rotate_left()if event.key == K_e:rocket.rotate_right()elif event.type == ADDAST1:ast = Asteroid(1, SIZE)asteroids.add(ast)all_sprites.add(ast)elif event.type == ADDAST2:ast = Asteroid(2, SIZE)asteroids.add(ast)all_sprites.add(ast)elif event.type == ADDAST3:ast = Asteroid(3, SIZE)asteroids.add(ast)all_sprites.add(ast)elif event.type == ADDAST4:ast = Asteroid(4, SIZE)asteroids.add(ast)all_sprites.add(ast)elif event.type == ADDAST5:ast = Asteroid(5, SIZE)asteroids.add(ast)all_sprites.add(ast)pressed_keys = pygame.key.get_pressed()rocket.update(pressed_keys)asteroids.update()bullets.update()explosions.update()win.blit(bg, (0,0))explosions.draw(win)for sprite in all_sprites:win.blit(sprite.surf, sprite.rect)win.blit(rocket.surf, rocket.rect)if pygame.sprite.spritecollideany(rocket, asteroids):rocket.kill()score = 0for sprite in all_sprites:sprite.kill()all_sprites.empty()rocket = Rocket(SIZE)all_sprites.add(rocket)gameStarted = FalsemusicStarted = Falsefor bullet in bullets:collision = pygame.sprite.spritecollide(bullet, asteroids, True)if collision:pos = bullet.rect[:2]explosion = Explosion(pos)explosions.add(explosion)score += 1explosion_sound.play()bullet.kill()bullets.remove(bullet)text = font.render('Score : ' + str(score), 1, (200,255,0))win.blit(text, (340, 10))pygame.display.flip()clock.tick(30)pygame.quit()

5、弹跳的球

源码分享:

import os
import pygamefrom player import Ball
from world import World, load_level
from texts import Text, Message
from button import Button, LevelButtonpygame.init()
WIDTH, HEIGHT = 192, 212
win = pygame.display.set_mode((WIDTH, HEIGHT), pygame.NOFRAME)
pygame.display.set_caption('Bounce')clock = pygame.time.Clock()
FPS = 30# GAME VARIABLES **************************************************************
ROWS = 12
MAX_COLS = 150
TILE_SIZE = 16
MAX_LEVEL = 8# COLORS **********************************************************************BLUE = (175, 207, 240)
BLUE2 = (0, 0, 255)
WHITE = (255, 255, 255)# FONTS ***********************************************************************health_font = "Fonts/ARCADECLASSIC.TTF"level_text = Text(health_font, 24)
health_text = Message(40, WIDTH + 10, 19, "x3", health_font, WHITE, win)
select_level_text = Message(WIDTH//2, 20, 24, "Select  Level", health_font, BLUE2, win)
current_level_text = Message(WIDTH - 40, WIDTH + 10, 20, "Level 1", health_font, WHITE, win)
you_win = Message(WIDTH //2, HEIGHT//2, 40, "You  Win", health_font, BLUE2, win)# SOUNDS **********************************************************************click_fx = pygame.mixer.Sound('Sounds/click.mp3')
life_fx = pygame.mixer.Sound('Sounds/gate.mp3')
checkpoint_fx = pygame.mixer.Sound('Sounds/checkpoint.mp3')pygame.mixer.music.load('Sounds/track1.wav')
pygame.mixer.music.play(loops=-1)
pygame.mixer.music.set_volume(0.4)# LOADING IMAGES **************************************************************ball_image = pygame.image.load('Assets/ball.png')
splash_img = pygame.transform.scale(pygame.image.load('Assets/splash_logo.png'),(2*WIDTH, HEIGHT))
bounce_img = pygame.image.load('Assets/menu_logo.png')
game_lost_img = pygame.image.load('Assets/lose.png')
game_lost_img = pygame.transform.scale(game_lost_img, (WIDTH//2, 80))
level_locked_img = pygame.image.load('Assets/level_locked.png')
level_locked_img = pygame.transform.scale(level_locked_img, (40, 40))
level_unlocked_img = pygame.image.load('Assets/level_unlocked.png')
level_unlocked_img = pygame.transform.scale(level_unlocked_img, (40, 40))play_img = pygame.image.load('Assets/play.png')
restart_img = pygame.image.load('Assets/restart.png')
menu_img = pygame.image.load('Assets/menu.png')
sound_on_img = pygame.image.load('Assets/SoundOnBtn.png')
sound_off_img = pygame.image.load('Assets/SoundOffBtn.png')
game_won_img = pygame.image.load('Assets/game won.png')# BUTTONS *********************************************************************play_btn = Button(play_img, False, 45, 130)
sound_btn = Button(sound_on_img, False, 45, 170)
restart_btn = Button(restart_img, False, 45, 130)
menu_btn = Button(menu_img, False, 45, 170)# LEVEL TEXT & BUTTONS ********************************************************level_btns = []
for level in range(MAX_LEVEL):text = level_text.render(f'{level+1}', (255, 255, 255))r = level // 3c = level % 3btn = LevelButton(level_locked_img, (40, 40), 20 + c * 55, 50 + r * 55, text)level_btns.append(btn)# GROUPS **********************************************************************spikes_group = pygame.sprite.Group()
inflator_group = pygame.sprite.Group()
deflator_group = pygame.sprite.Group()
enemy_group = pygame.sprite.Group()
exit_group = pygame.sprite.Group()
checkpoint_group = pygame.sprite.Group()
health_group = pygame.sprite.Group()objects_groups = [spikes_group, inflator_group, deflator_group, enemy_group, exit_group, checkpoint_group, health_group]
collision_groups = [inflator_group, deflator_group]# RESET ***********************************************************************def reset_level_data(level):for group in objects_groups:group.empty()# LOAD LEVEL WORLDworld_data, level_length = load_level(level)w = World(objects_groups)w.generate_world(world_data, win)return world_data, level_length, wdef reset_player_data(level):if level == 1:x, y = 64, 50if level == 2:x, y = 65, 50if level == 3:x, y = 64, 50if level == 4:x, y = 63, 50if level == 5:x, y = 64, 50if level == 6:x, y = 48, 50if level == 7:x, y = 78, 80if level == 8:x, y = 112,100p = Ball(x, y)moving_left = Falsemoving_right = Falsereturn p, moving_left, moving_right# VARIABLES *******************************************************************moving_left = False
moving_right = FalseSCROLL_THRES = 80
screen_scroll = 0
level_scroll = 0
level = 1
next_level = False
reset_level = Falsecheckpoint = None
health = 3
splash_count = 0
sound_on = Truelogo_page = True
home_page = False
level_page = False
game_page = False
restart_page = False
win_page = Falserunning = True
while running:win.fill(BLUE)pygame.draw.rect(win, (255, 255,255), (0, 0, WIDTH, HEIGHT), 1, border_radius=5)for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE or \event.key == pygame.K_q:running = Falseif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT:moving_left = Trueif event.key == pygame.K_RIGHT:moving_right = Trueif event.key == pygame.K_UP:if not p.jump:p.jump = Trueif event.type == pygame.KEYUP:if event.key == pygame.K_LEFT:moving_left = Falseif event.key == pygame.K_RIGHT:moving_right = Falseif logo_page:win.blit(splash_img, (-100,0))splash_count += 1if splash_count % 50 == 0:logo_page = Falsehome_page = Trueif home_page:win.blit(bounce_img, (10,10))if play_btn.draw(win):click_fx.play()home_page = Falselevel_page = Trueif sound_btn.draw(win):click_fx.play()sound_on = not sound_onif sound_on:sound_btn.update_image(sound_on_img)pygame.mixer.music.play(loops=-1)else:sound_btn.update_image(sound_off_img)pygame.mixer.music.stop()if level_page:select_level_text.update(shadow=False)for index, btn in enumerate(level_btns):if index < level:if not btn.unlocked:btn.unlocked = Truebtn.update_image(level_unlocked_img)if btn.draw(win):if index < level:click_fx.play()level_page = Falsegame_page = Truelevel = index + 1screen_scroll = 0level_scroll = 0health = 3world_data, level_length, w = reset_level_data(level)p, moving_left, moving_right = reset_player_data(level)if restart_page:win.blit(game_lost_img, (45,20))if restart_btn.draw(win):click_fx.play()world_data, level_length, w = reset_level_data(level)p, moving_left, moving_right = reset_player_data(level)level_scroll = 0screen_scroll = 0health = 3checkpoint = Nonerestart_page = Falsegame_page = Trueif menu_btn.draw(win):click_fx.play()home_page = Truerestart_page = Falseif win_page:win.blit(game_won_img, (45, 20))you_win.update()if menu_btn.draw(win):click_fx.play()home_page = Truewin_page = Falserestart_page = Falseif game_page:w.update(screen_scroll)w.draw(win)spikes_group.update(screen_scroll)spikes_group.draw(win)health_group.update(screen_scroll)health_group.draw(win)inflator_group.update(screen_scroll)inflator_group.draw(win)deflator_group.update(screen_scroll)deflator_group.draw(win)exit_group.update(screen_scroll)exit_group.draw(win)checkpoint_group.update(screen_scroll)checkpoint_group.draw(win)enemy_group.update(screen_scroll)enemy_group.draw(win)screen_scroll = 0p.update(moving_left, moving_right, w, collision_groups)p.draw(win)if ((p.rect.right >= WIDTH - SCROLL_THRES) and level_scroll < (level_length * 16) - WIDTH) \or ((p.rect.left <= SCROLL_THRES) and level_scroll > 0):dx = p.dxp.rect.x -= dxscreen_scroll = -dxlevel_scroll += dxif len(exit_group) > 0:exit = exit_group.sprites()[0]if not exit.open:if abs(p.rect.x - exit.rect.x) <= 80 and len(health_group) == 0:exit.open = Trueif p.rect.colliderect(exit.rect) and exit.index == 11:checkpoint = Nonecheckpoint_fx.play()level += 1if level < MAX_LEVEL:checkpoint = Falsereset_level = Truenext_level = Trueelse:checkpoint = Nonewin_page = Truecp = pygame.sprite.spritecollide(p, checkpoint_group, False)if cp:checkpoint = cp[0]if not checkpoint.catched:checkpoint_fx.play()checkpoint.catched = Truecheckpoint_pos = p.rect.centercheckpoint_screen_scroll = screen_scrollcheckpoint_level_scroll = level_scrollif pygame.sprite.spritecollide(p, spikes_group, False):reset_level = Trueif pygame.sprite.spritecollide(p, health_group, True):health += 1life_fx.play()if pygame.sprite.spritecollide(p, enemy_group, False):reset_level = Trueif reset_level:if health > 0:if next_level:world_data, level_length, w = reset_level_data(level)p, moving_left, moving_right = reset_player_data(level)level_scroll = 0health = 3checkpoint = Nonenext_level = Falseelif checkpoint:checkpoint_dx = level_scroll - checkpoint_level_scrollw.update(checkpoint_dx)for group in objects_groups:group.update(checkpoint_dx)p.rect.center = checkpoint_poslevel_scroll = checkpoint_level_scrollelse:w.update(level_scroll)for group in objects_groups:group.update(level_scroll)p, moving_left, moving_right = reset_player_data(level)level_scroll = 0screen_scroll = 0reset_level = Falsehealth -= 1else:restart_page = Truegame_page = Falsereset_level = False# Drawing info barpygame.draw.rect(win, (25, 25, 25), (0, HEIGHT-20, WIDTH, 20))pygame.draw.rect(win, (255, 255,255), (0, 0, WIDTH, WIDTH), 2, border_radius=5)win.blit(ball_image, (5, WIDTH + 2))health_text.update(f'x{health}', shadow=False)current_level_text.update(f'Level {level}', shadow=False)clock.tick(FPS)pygame.display.update()pygame.quit()

6、汽车避障

源码分享:

import pygame
import random
from objects import Road, Player, Nitro, Tree, Button, \Obstacle, Coins, Fuelpygame.init()
SCREEN = WIDTH, HEIGHT = 288, 512info = pygame.display.Info()
width = info.current_w
height = info.current_hif width >= height:win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
else:win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)clock = pygame.time.Clock()
FPS = 30lane_pos = [50, 95, 142, 190]# COLORS **********************************************************************WHITE = (255, 255, 255)
BLUE = (30, 144,255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 20)# FONTS ***********************************************************************font = pygame.font.SysFont('cursive', 32)select_car = font.render('Select Car', True, WHITE)# IMAGES **********************************************************************bg = pygame.image.load('Assets/bg.png')home_img = pygame.image.load('Assets/home.png')
play_img = pygame.image.load('Assets/buttons/play.png')
end_img = pygame.image.load('Assets/end.jpg')
end_img = pygame.transform.scale(end_img, (WIDTH, HEIGHT))
game_over_img = pygame.image.load('Assets/game_over.png')
game_over_img = pygame.transform.scale(game_over_img, (220, 220))
coin_img = pygame.image.load('Assets/coins/1.png')
dodge_img = pygame.image.load('Assets/car_dodge.png')left_arrow = pygame.image.load('Assets/buttons/arrow.png')
right_arrow = pygame.transform.flip(left_arrow, True, False)home_btn_img = pygame.image.load('Assets/buttons/home.png')
replay_img = pygame.image.load('Assets/buttons/replay.png')
sound_off_img = pygame.image.load("Assets/buttons/soundOff.png")
sound_on_img = pygame.image.load("Assets/buttons/soundOn.png")cars = []
car_type = 0
for i in range(1, 9):img = pygame.image.load(f'Assets/cars/{i}.png')img = pygame.transform.scale(img, (59, 101))cars.append(img)nitro_frames = []
nitro_counter = 0
for i in range(6):img = pygame.image.load(f'Assets/nitro/{i}.gif')img = pygame.transform.flip(img, False, True)img = pygame.transform.scale(img, (18, 36))nitro_frames.append(img)# FUNCTIONS *******************************************************************
def center(image):return (WIDTH // 2) - image.get_width() // 2# BUTTONS *********************************************************************
play_btn = Button(play_img, (100, 34), center(play_img)+10, HEIGHT-80)
la_btn = Button(left_arrow, (32, 42), 40, 180)
ra_btn = Button(right_arrow, (32, 42), WIDTH-60, 180)home_btn = Button(home_btn_img, (24, 24), WIDTH // 4 - 18, HEIGHT - 80)
replay_btn = Button(replay_img, (36,36), WIDTH // 2  - 18, HEIGHT - 86)
sound_btn = Button(sound_on_img, (24, 24), WIDTH - WIDTH // 4 - 18, HEIGHT - 80)# SOUNDS **********************************************************************click_fx = pygame.mixer.Sound('Sounds/click.mp3')
fuel_fx = pygame.mixer.Sound('Sounds/fuel.wav')
start_fx = pygame.mixer.Sound('Sounds/start.mp3')
restart_fx = pygame.mixer.Sound('Sounds/restart.mp3')
coin_fx = pygame.mixer.Sound('Sounds/coin.mp3')pygame.mixer.music.load('Sounds/mixkit-tech-house-vibes-130.mp3')
pygame.mixer.music.play(loops=-1)
pygame.mixer.music.set_volume(0.6)# OBJECTS *********************************************************************
road = Road()
nitro = Nitro(WIDTH-80, HEIGHT-80)
p = Player(100, HEIGHT-120, car_type)tree_group = pygame.sprite.Group()
coin_group = pygame.sprite.Group()
fuel_group = pygame.sprite.Group()
obstacle_group = pygame.sprite.Group()# VARIABLES *******************************************************************
home_page = True
car_page = False
game_page = False
over_page = Falsemove_left = False
move_right = False
nitro_on = False
sound_on = Truecounter = 0
counter_inc = 1
# speed = 3
speed = 10
dodged = 0
coins = 0
cfuel = 100endx, enddx = 0, 0.5
gameovery = -50running = True
while running:win.fill(BLACK)for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE or event.key == pygame.K_q:running = Falseif event.key == pygame.K_LEFT:move_left = Trueif event.key == pygame.K_RIGHT:move_right = Trueif event.key == pygame.K_n:nitro_on = Trueif event.type == pygame.KEYUP:if event.key == pygame.K_LEFT:move_left = Falseif event.key == pygame.K_RIGHT:move_right = Falseif event.key == pygame.K_n:nitro_on = Falsespeed = 3counter_inc = 1if event.type == pygame.MOUSEBUTTONDOWN:x, y = event.posif nitro.rect.collidepoint((x, y)):nitro_on = Trueelse:if x <= WIDTH // 2:move_left = Trueelse:move_right = Trueif event.type == pygame.MOUSEBUTTONUP:move_left = Falsemove_right = Falsenitro_on = Falsespeed = 3counter_inc = 1if home_page:win.blit(home_img, (0,0))counter += 1if counter % 60 == 0:home_page = Falsecar_page = Trueif car_page:win.blit(select_car, (center(select_car), 80))win.blit(cars[car_type], (WIDTH//2-30, 150))if la_btn.draw(win):car_type -= 1click_fx.play()if car_type < 0:car_type = len(cars) - 1if ra_btn.draw(win):car_type += 1click_fx.play()if car_type >= len(cars):car_type = 0if play_btn.draw(win):car_page = Falsegame_page = Truestart_fx.play()p = Player(100, HEIGHT-120, car_type)counter = 0if over_page:win.blit(end_img, (endx, 0))endx += enddxif endx >= 10 or endx<=-10:enddx *= -1win.blit(game_over_img, (center(game_over_img), gameovery))if gameovery < 16:gameovery += 1num_coin_img = font.render(f'{coins}', True, WHITE)num_dodge_img = font.render(f'{dodged}', True, WHITE)distance_img = font.render(f'Distance : {counter/1000:.2f} km', True, WHITE)win.blit(coin_img, (80, 240))win.blit(dodge_img, (50, 280))win.blit(num_coin_img, (180, 250))win.blit(num_dodge_img, (180, 300))win.blit(distance_img, (center(distance_img), (350)))if home_btn.draw(win):over_page = Falsehome_page = Truecoins = 0dodged = 0counter = 0nitro.gas = 0cfuel = 100endx, enddx = 0, 0.5gameovery = -50if replay_btn.draw(win):over_page = Falsegame_page = Truecoins = 0dodged = 0counter = 0nitro.gas = 0cfuel = 100endx, enddx = 0, 0.5gameovery = -50restart_fx.play()if sound_btn.draw(win):sound_on = not sound_onif sound_on:sound_btn.update_image(sound_on_img)pygame.mixer.music.play(loops=-1)else:sound_btn.update_image(sound_off_img)pygame.mixer.music.stop()if game_page:win.blit(bg, (0,0))road.update(speed)road.draw(win)counter += counter_incif counter % 60 == 0:tree = Tree(random.choice([-5, WIDTH-35]), -20)tree_group.add(tree)if counter % 270 == 0:type = random.choices([1, 2], weights=[6, 4], k=1)[0]x = random.choice(lane_pos)+10if type == 1:count = random.randint(1, 3)for i in range(count):coin = Coins(x,-100 - (25 * i))coin_group.add(coin)elif type == 2:fuel = Fuel(x, -100)fuel_group.add(fuel)elif counter % 90 == 0:obs = random.choices([1, 2, 3], weights=[6,2,2], k=1)[0]obstacle = Obstacle(obs)obstacle_group.add(obstacle)if nitro_on and nitro.gas > 0:x, y = p.rect.centerx - 8, p.rect.bottom - 10win.blit(nitro_frames[nitro_counter], (x, y))nitro_counter = (nitro_counter + 1) % len(nitro_frames)speed = 10if counter_inc == 1:counter = 0counter_inc = 5if nitro.gas <= 0:speed = 3counter_inc = 1nitro.update(nitro_on)nitro.draw(win)obstacle_group.update(speed)obstacle_group.draw(win)tree_group.update(speed)tree_group.draw(win)coin_group.update(speed)coin_group.draw(win)fuel_group.update(speed)fuel_group.draw(win)p.update(move_left, move_right)p.draw(win)if cfuel > 0:pygame.draw.rect(win, GREEN, (20, 20, cfuel, 15), border_radius=5)pygame.draw.rect(win, WHITE, (20, 20, 100, 15), 2, border_radius=5)cfuel -= 0.05# COLLISION DETECTION & KILLSfor obstacle in obstacle_group:if obstacle.rect.y >= HEIGHT:if obstacle.type == 1:dodged += 1obstacle.kill() if pygame.sprite.collide_mask(p, obstacle):pygame.draw.rect(win, RED, p.rect, 1)speed = 0game_page = Falseover_page = Truetree_group.empty()coin_group.empty()fuel_group.empty()obstacle_group.empty()if pygame.sprite.spritecollide(p, coin_group, True):coins += 1coin_fx.play()if pygame.sprite.spritecollide(p, fuel_group, True):cfuel += 25fuel_fx.play()if cfuel >= 100:cfuel = 100pygame.draw.rect(win, BLUE, (0, 0, WIDTH, HEIGHT), 3)clock.tick(FPS)pygame.display.update()pygame.quit()# 今天给大家介绍一款Python制作的汽车避障小游戏
# 主要通过Pygame实现的,运行代码,选择车型,进入游戏
# 可以控制左右移动,点击右下角可以进行加速
# 金币可以增加积分,撞到障碍物则游戏介绍

7、洞窟物语

源码分享:

import pygame
import picklefrom objects import World, load_level, Button, Player, Portal, game_datapygame.init()
SCREEN = WIDTH, HEIGHT = 288, 512
win = pygame.display.set_mode(SCREEN, pygame.SCALED | pygame.FULLSCREEN)
clock = pygame.time.Clock()
FPS = 45tile_size = 16# Imagesbg = pygame.image.load("Assets/bg.png")cave_story = pygame.transform.rotate(pygame.image.load("Assets/cave_story.png"), 90)
cave_story = pygame.transform.scale(cave_story, (150,300))game_won_img = pygame.transform.rotate(pygame.image.load("Assets/win.png"), -90)
game_won_img = pygame.transform.scale(game_won_img, (150,300))# Sounds
pygame.mixer.music.load('Sounds/goodbyte_sad-rpg-town.mp3')
pygame.mixer.music.play(loops=-1)replay_fx =  pygame.mixer.Sound('Sounds/replay.wav')# Buttonsmove_btn = pygame.image.load("Assets/movement.jpeg")
move_btn = pygame.transform.rotate(move_btn, -90)
move_btn = pygame.transform.scale(move_btn, (42, HEIGHT))play_img = pygame.transform.rotate(pygame.image.load("Assets/play.png"), -90)
play_btn = Button(play_img, (60, 150), 30, 170)quit_img = pygame.transform.rotate(pygame.image.load("Assets/quit.png"), -90)
quit_btn = Button(quit_img, (60, 150), 140, 170)replay_img = pygame.transform.rotate(pygame.image.load("Assets/replay.png"), -90)
replay_btn = Button(replay_img, (40, 40), 100, 190)sound_off_img = pygame.transform.rotate(pygame.image.load("Assets/sound_off.png"), -90)
sound_on_img = pygame.transform.rotate(pygame.image.load("Assets/sound_on.png"), -90)
sound_btn = Button(sound_on_img, (40, 40), 100, 260)# Variablescurrent_level = 1
MAX_LEVEL = 3
show_keys = True
pressed_keys = [False, False, False, False]dir_dict = {'Up' : pygame.Rect(5, 27, 35, 50),'Down' : pygame.Rect(5, 160, 35, 50),'Left' :  pygame.Rect(5, 320, 35, 50),'Right' : pygame.Rect(5, 450, 35, 50)
}# groups
diamond_group = pygame.sprite.Group()
spike_group = pygame.sprite.Group()
plant_group = pygame.sprite.Group()
board_group = pygame.sprite.Group()
chain_group = pygame.sprite.Group()
groups = [diamond_group, spike_group, plant_group, board_group, chain_group]data = load_level(current_level)
(player_x, player_y), (portal_x, portal_y) = game_data(current_level)world = World(win, data, groups)
player = Player(win, (player_x,player_y), world, groups)
portal = Portal(portal_x, portal_y, win)game_started = False
game_over = False
game_won = False
replay_menu = False
sound_on = Truebgx = 0
bgcounter = 0
bgdx = 1running = True
while running:win.blit(bg, (bgx, 0))for group in groups:group.draw(win)world.draw()if not game_started:win.blit(cave_story, (100,100))if play_btn.draw(win):game_started = Trueelif game_won:win.blit(game_won_img, (100,100))else:if show_keys:win.blit(move_btn, (0,0))bgcounter += 1if bgcounter >= 15:bgcounter = 0bgx += bgdxif bgx < 0 or bgx >5 :bgdx *= -1#   for rect in dir_dict:#       pygame.draw.rect(win, (255, 0, 0), dir_dict[rect])for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.MOUSEBUTTONDOWN:pos = event.posif show_keys:if dir_dict["Up"].collidepoint(pos):pressed_keys[0] = Trueif dir_dict["Down"].collidepoint(pos):pressed_keys[1] = Trueif dir_dict["Left"].collidepoint(pos):pressed_keys[2] = Trueif dir_dict["Right"].collidepoint(pos):pressed_keys[3] = Trueif event.type == pygame.MOUSEBUTTONUP:pressed_keys = [False, False, False, False]portal.update()if not game_over:game_over = player.update(pressed_keys, game_over)if game_over:show_keys = Falsereplay_menu = Trueif player.rect.colliderect(portal) and player.rect.top > portal.rect.top and player.rect.bottom < portal.rect.bottom:if current_level < MAX_LEVEL:current_level += 1for group in groups:group.empty()data = load_level(current_level)(player_x, player_y), (portal_x, portal_y) = game_data(current_level)world = World(win, data, groups)player = Player(win, (player_x,player_y), world, groups)portal = Portal(portal_x, portal_y, win)else:show_keys = Falsegame_won = Trueif replay_menu:if quit_btn.draw(win):running = Falseif sound_btn.draw(win):sound_on = not sound_onif sound_on:sound_btn.update_image(sound_on_img)pygame.mixer.music.play(loops=-1)else:sound_btn.update_image(sound_off_img)pygame.mixer.music.stop()if replay_btn.draw(win):show_keys = Truereplay_menu = Falsegame_over = Falsereplay_fx.play()for group in groups:group.empty()data = load_level(current_level)(player_x, player_y), (portal_x, portal_y) = game_data(current_level)world = World(win, data, groups)player = Player(win, (player_x,player_y), world, groups)portal = Portal(portal_x, portal_y, win)clock.tick(FPS)pygame.display.update()pygame.quit()

8、联系

源码分享:

# Connected# Author : Prajjwal Pathak (pyguru)
# Date : Thursday, 8 August, 2021import random
import pygamefrom objects import Balls, Coins, Tiles, Particle, Message, Buttonpygame.init()
SCREEN = WIDTH, HEIGHT = 288, 512
CENTER = WIDTH //2, HEIGHT // 2info = pygame.display.Info()
width = info.current_w
height = info.current_hif width >= height:win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
else:win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)
pygame.display.set_caption('Connected')clock = pygame.time.Clock()
FPS = 90# COLORS **********************************************************************RED = (255,0,0)
GREEN = (0,177,64)
BLUE = (30, 144,255)
ORANGE = (252,76,2)
YELLOW = (254,221,0)
PURPLE = (155,38,182)
AQUA = (0,103,127)
WHITE = (255,255,255)
BLACK = (0,0,0)
GRAY = (25, 25, 25)color_list = [PURPLE, GREEN, BLUE, ORANGE, YELLOW, RED]
color_index = 0
color = color_list[color_index]# SOUNDS **********************************************************************flip_fx = pygame.mixer.Sound('Sounds/flip.mp3')
score_fx = pygame.mixer.Sound('Sounds/point.mp3')
dead_fx = pygame.mixer.Sound('Sounds/dead.mp3')
score_page_fx = pygame.mixer.Sound('Sounds/score_page.mp3')pygame.mixer.music.load('Sounds/bgm.mp3')
pygame.mixer.music.play(loops=-1)
pygame.mixer.music.set_volume(0.5)# FONTS ***********************************************************************title_font = "Fonts/Aladin-Regular.ttf"
score_font = "Fonts/DroneflyRegular-K78LA.ttf"
game_over_font = "Fonts/ghostclan.ttf"
final_score_font = "Fonts/DalelandsUncialBold-82zA.ttf"
new_high_font = "Fonts/BubblegumSans-Regular.ttf"connected = Message(WIDTH//2, 120, 55, "ConnecteD", title_font, WHITE, win)
score_msg = Message(WIDTH//2, 100, 60, "0", score_font, (150, 150, 150), win)
game_msg = Message(80, 150, 40, "GAME", game_over_font, BLACK, win)
over_msg = Message(210, 150, 40, "OVER!", game_over_font, WHITE, win)
final_score = Message(WIDTH//2, HEIGHT//2, 90, "0", final_score_font, RED, win)
new_high_msg = Message(WIDTH//2, HEIGHT//2+60, 20, "New High", None, GREEN, win)# Button imageshome_img = pygame.image.load('Assets/homeBtn.png')
replay_img = pygame.image.load('Assets/replay.png')
sound_off_img = pygame.image.load("Assets/soundOffBtn.png")
sound_on_img = pygame.image.load("Assets/soundOnBtn.png")
easy_img = pygame.image.load("Assets/easy.jpg")
hard_img = pygame.image.load("Assets/hard.jpg")# Buttonseasy_btn = Button(easy_img, (70, 24), WIDTH//4-10, HEIGHT-100)
hard_btn = Button(hard_img, (70, 24), WIDTH//2 + 10, HEIGHT-100)
home_btn = Button(home_img, (24, 24), WIDTH // 4 - 18, HEIGHT//2 + 120)
replay_btn = Button(replay_img, (36,36), WIDTH // 2  - 18, HEIGHT//2 + 115)
sound_btn = Button(sound_on_img, (24, 24), WIDTH - WIDTH // 4 - 18, HEIGHT//2 + 120)# Groups **********************************************************************RADIUS = 70
ball_group = pygame.sprite.Group()
coin_group = pygame.sprite.Group()
tile_group = pygame.sprite.Group()
particle_group = pygame.sprite.Group()ball = Balls((CENTER[0], CENTER[1]+RADIUS), RADIUS, 90, win)
ball_group.add(ball)
ball = Balls((CENTER[0], CENTER[1]-RADIUS), RADIUS, 270, win)
ball_group.add(ball)# TIME ************************************************************************start_time = pygame.time.get_ticks()
current_time = 0
coin_delta = 850
tile_delta = 2000# VARIABLES *******************************************************************clicked = False
new_coin = True
num_clicks = 0
score = 0player_alive = True
score = 0
highscore = 0
sound_on = True
easy_level = Truehome_page = True
game_page = False
score_page = Falserunning = True
while running:win.fill(GRAY)for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:if event.key == pygame.K_ESCAPE or \event.key == pygame.K_q:running = Falseif event.type == pygame.MOUSEBUTTONDOWN and game_page:if not clicked:clicked = Truefor ball in ball_group:ball.dtheta *= -1flip_fx.play()num_clicks += 1if num_clicks % 5 == 0:color_index += 1if color_index > len(color_list) - 1:color_index = 0color = color_list[color_index]if event.type == pygame.MOUSEBUTTONDOWN and game_page:clicked = Falseif home_page:connected.update()pygame.draw.circle(win, BLACK, CENTER, 80, 20)ball_group.update(color)if easy_btn.draw(win):ball_group.empty()ball = Balls((CENTER[0], CENTER[1]+RADIUS), RADIUS, 90, win)ball_group.add(ball)home_page = Falsegame_page = Trueeasy_level = Trueif hard_btn.draw(win):ball_group.empty()ball = Balls((CENTER[0], CENTER[1]+RADIUS), RADIUS, 90, win)ball_group.add(ball)ball = Balls((CENTER[0], CENTER[1]-RADIUS), RADIUS, 270, win)ball_group.add(ball)home_page = Falsegame_page = Trueeasy_level = Falseif score_page:game_msg.update()over_msg.update()if score:final_score.update(score, color)else:final_score.update("0", color)if score and (score >= highscore):new_high_msg.update(shadow=False)if home_btn.draw(win):home_page = Truescore_page = Falsegame_page = Falseplayer_alive = Truescore = 0score_msg = Message(WIDTH//2, 100, 60, "0", score_font, (150, 150, 150), win)if replay_btn.draw(win):home_page = Falsescore_page = Falsegame_page = Truescore = 0score_msg = Message(WIDTH//2, 100, 60, "0", score_font, (150, 150, 150), win)if easy_level:ball = Balls((CENTER[0], CENTER[1]+RADIUS), RADIUS, 90, win)ball_group.add(ball)else:ball = Balls((CENTER[0], CENTER[1]+RADIUS), RADIUS, 90, win)ball_group.add(ball)ball = Balls((CENTER[0], CENTER[1]-RADIUS), RADIUS, 270, win)ball_group.add(ball)player_alive = Trueif sound_btn.draw(win):sound_on = not sound_onif sound_on:sound_btn.update_image(sound_on_img)pygame.mixer.music.play(loops=-1)else:sound_btn.update_image(sound_off_img)pygame.mixer.music.stop()if game_page:pygame.draw.circle(win, BLACK, CENTER, 80, 20)ball_group.update(color)coin_group.update(color)tile_group.update()score_msg.update(score)particle_group.update()if player_alive:for ball in ball_group:if pygame.sprite.spritecollide(ball, coin_group, True):score_fx.play()score += 1if highscore <= score:highscore = scorex, y = ball.rect.centerfor i in range(10):particle = Particle(x, y, color, win)particle_group.add(particle)if pygame.sprite.spritecollide(ball, tile_group, True):x, y = ball.rect.centerfor i in range(30):particle = Particle(x, y, color, win)particle_group.add(particle)player_alive = Falsedead_fx.play()current_time = pygame.time.get_ticks()delta = current_time- start_timeif  coin_delta < delta < coin_delta + 100 and new_coin:y = random.randint(CENTER[1]-RADIUS, CENTER[1]+RADIUS)coin = Coins(y, win)coin_group.add(coin)new_coin = Falseif current_time- start_time >= tile_delta:y = random.choice([CENTER[1]-80, CENTER[1], CENTER[1]+80])type_ = random.randint(1,3)t = Tiles(y, type_, win)tile_group.add(t)start_time = current_timenew_coin = Trueif not player_alive and len(particle_group) == 0:score_page = Truegame_page = Falsescore_page_fx.play()ball_group.empty()tile_group.empty()coin_group.empty()pygame.draw.rect(win, BLUE, (0, 0, WIDTH, HEIGHT), 5, border_radius=10)clock.tick(FPS)pygame.display.update()pygame.quit()

9、恐龙避障

代码太多了,接下来就简单介绍下,有兴趣的在公众号内回复【游戏】即可获取所有的游戏源码。

10、狡猾的墙

11、点和盒子

12、捉蛋游戏

13、愤怒的小鸟

14、捉鬼特工队

15、绞刑吏

16、六角冲

17、匹配选择

18、丛林探险

19、关卡设计

20、记忆拼图

21、扫雷

22、钢琴砖

23、图片滑动拼图

24、乒乓球

25、冲刺

26、石头剪刀布

27、旋转冲击

28、贪吃蛇

29、剪刀

30、俄罗斯方块

相关文件及代码都已上传,公众号回复【游戏】即可获取。

万水千山总是情,点个 

边玩边学,30个Python小游戏(含源码)相关推荐

  1. 整理了30款Python小游戏附源码,五一有的玩了

    快到五一了,整理了 30 款 Python 小游戏源码分享给大家,具体内容可以点击下方视频号查看: 点击上方视频后,源码获取方式:①关注上方视频号.②点赞当前视频.③在当前视频评论区扣1 友情提示:获 ...

  2. 边玩边学,13个Python小游戏(含源码)

    经常听到有朋友说,学习编程是一件非常枯燥无味的事情.其实,大家有没有认真想过,可能是我们的学习方法不对? 比方说,你有没有想过,可以通过打游戏来学编程? 今天我想跟大家分享几个Python小游戏,教你 ...

  3. 60行python代码实现弹球小游戏(含源码)

    弹球效果预览 解析代码 弹球 Ball 类 draw负责移动Ball 碰撞检测,反弹,Ball检测Paddle 2.Paddle类 draw负责移动Paddle 碰撞检测,确定能不能继续 监听键盘事件 ...

  4. WinFom中经典小游戏(含源码)

    最近整理了若干经典的小游戏,无聊时可以打发时间.程序本身不大,练手非常不错,主要是GDI编程,主界面地址如下图所示 源码下载方式 1,关注微信公众号:小特工作室(也可直接扫描签名处二维码) 2,发送: ...

  5. python 小游戏——外星人入侵源码倾情奉献

    距离上次贴出<python编程:从入门到实践>13.1-14.1章的代码已经过了又有半个月,这里给大家亲测可用的13.1-14.3的全部代码.具体的代码可能会和书上的稍有不同,主要是计分环 ...

  6. 30个Python小游戏,小白练手,我都能玩一天

    大家好,我是雨雨~ 今天给大家带来30个py小游戏,一定要收藏! 全部源码都整理好了 小游戏源码腾讯文档-在线文档https://docs.qq.com/doc/DRnZDTnFGVUFMc3FL 有 ...

  7. python小游戏-16行代码实现3D撞球小游戏!-源码下载

    python小游戏-16行代码实现3D撞球小游戏!-源码下载 所属网站分类: 资源下载 > python小游戏 作者:搞笑 链接: http://www.pythonheidong.com/bl ...

  8. 2023 俄罗斯方块网页小游戏HTML源码

    手机和电脑自适应都可以玩,试玩了一下源码没问题,也没有加密,想试玩的可以下载源码搭建耍耍 2023 俄罗斯方块网页小游戏HTML源码

  9. html实现扫雷小游戏(附源码)

    文章目录 实现功能 1.扫雷设计 1.1 主界面 1.2 扫雷难度 1.3 附带功能 2.效果和源码 2.1 动态效果 2.2 源代码 源码下载 作者:xcLeigh 文章地址:https://blo ...

最新文章

  1. 【对讲机的那点事】关于对讲机锂电池你了解多少?
  2. Android Studio项目转Eclipse项目
  3. 什么是现代通用计算机的轴型,大学计算机基础(版)各章节习题+答案.doc
  4. 中文论文黑白矢量图绘制
  5. uva 10759 Dice Throwing
  6. 打开另外一个页面_如何在PDF页面中插入图片?
  7. (转载)Android两种Tab分页的方式:TabActivity和ActivityGroup以及Android项目几种常见的应用架构...
  8. 【HEVC】目前H265分析软件推荐
  9. Redis 巧用数据类型实现亿级数据统计
  10. AI实战分享 | 基于CANN的辅助驾驶应用案例
  11. java 分布式任务队列_cerely-分布式异步任务队列
  12. ActiveMQ的用途
  13. LNAMP服务器环境(源码安装)
  14. redo log 和undo log_MySQL 持久化保障机制-redo 日志
  15. Study「Word2016」:论文公式编辑时,编号右对齐
  16. mysql.sock 路径_关于mysql.sock路径的问题
  17. java深拷贝的三种方式
  18. 汇编语言:以“年/月/日 时:分:秒”的格式,显示当前的日期、时间。
  19. 转发和重定向的区别(forward和redirect的区别)
  20. 消息队列中间件 Message Queue 简称:MQ

热门文章

  1. feapder入门教程之AirSpider
  2. PS 如何把大嘴变小嘴
  3. java使用siger 轻松获取本机硬件信息(CPU 内存 网络 io等)
  4. ORA 00257 问题
  5. ElementUI switch 修改值的坑
  6. 如何设置谷歌浏览器(Google Chrome)的开发者工具的语言
  7. 老板邮局android版本,老板邮局首推Bossmail手机邮 已上架APPStore
  8. qq音乐windows版本也用了部分chromium库啊
  9. 全国计算机二级课件,全国计算机等级考试二级课件.ppt
  10. 数据库视频第17~21章的学习框架和笔记