pygame 自制游戏合集
制作者:小蔡编程 KevinLikesCoding
pygame 打字小游戏:
链接:https://www.cnblogs.com/xiaocaibiancheng/p/15158997.html
import pygame.freetype
import sys
import random
pygame.init()
screen = pygame.display.set_mode((500, 400))
pygame.display.set_caption('game')
f = pygame.freetype.Font(None, 60)
f2 = pygame.freetype.Font(None, 30)
p = 0
w = [50, 140, 230, 320, 410]
l = []
t = 0
s = 0
while True:
pygame.time.delay(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if p == 1:
for i in l:
if ord(i[0]) == event.key - 32:
l.remove(i)
s += 1
elif event.type == pygame.MOUSEBUTTONDOWN:
if p == 0:
p = 1
for i in range(0, 5):
l.append([chr(random.randint(65, 80)), w[i], 50])
screen.fill((0, 0, 0))
if p == 0:
f.render_to(screen, [180, 250], "start", fgcolor=(255, 255, 255))
if p == 1:
t += 1
if t == 100:
t = 0
for i in range(0, 5):
l.append([chr(random.randint(65, 80)), w[i], 50])
for i in l:
f.render_to(screen, [i[1], i[2]], i[0], fgcolor=(255, 255, 255))
for i in range(0, len(l)):
l[i][2] += 1
if l[i][2] >= 400:
p = 2
f2.render_to(screen, [5, 5], "score: %d" % s, fgcolor=(255, 255, 255))
if p == 2:
f.render_to(screen, [100, 100], "game over", fgcolor=(255, 255, 255))
f.render_to(screen, [100, 250], "score: %d" % s, fgcolor=(255, 255, 255))
pygame.display.update()
pygame 金币小游戏
链接:https://www.cnblogs.com/xiaocaibiancheng/p/15169772.html
资源:img.png
https://img2020.cnblogs.com/blog/2160229/202108/2160229-20210821155728188-1677917829.png
import pygame.freetype
import sys
import random
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('game')
p = 0
i1 = 0
s = 0
t = 0
f1 = pygame.freetype.Font('C:/windows/fonts/msyh.ttc', 100)
f2 = pygame.freetype.Font(None, 30)
img = pygame.image.load('img.png')
img = pygame.transform.scale(img, (30, 30))
l = []
while True:
pygame.time.delay(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if p == 0:
t = 0
p = 1
screen.fill((255, 255, 255))
if p == 0:
f1.render_to(screen, [100, 130], '开始游戏')
if p == 1:
i1 += 1
if i1 >= 50//(int(t/30)+1):
i1 = 0
if t < 200:
t += 1
l.append([random.randint(50, 520), 50])
if t == 200 and not l:
p = 2
for i in l:
screen.blit(img, [i[0], i[1]])
i[1] += int(t/30)+1
x = pygame.mouse.get_pos()[0]
if x < 40:
x = 40
if x > 560:
x = 560
pygame.draw.rect(screen, (60, 150, 250), (x-40, 320, 80, 20))
for i in l:
if i[1] >= 300:
if x-70 <= i[0] <= x+40:
s += 1
l.remove(i)
f2.render_to(screen, [30, 30], 'score:%d' % s)
if p == 2:
f1.render_to(screen, [80, 130], 'score:%d' % s)
pygame.display.update()
pygame 小鸡飞行小游戏
链接:https://www.cnblogs.com/xiaocaibiancheng/p/15254972.html
资源:chick.png:
https://img2020.cnblogs.com/blog/2160229/202109/2160229-20210911143132335-2092010670.png
import pygame.freetype
import sys
import random
pygame.init()
screen = pygame.display.set_mode((500, 400))
pygame.display.set_caption('chick game')
png1 = pygame.image.load('chick.png')
png1 = pygame.transform.scale(png1, (96, 88))
font1 = pygame.freetype.Font(None, 24)
font2 = pygame.freetype.Font(None, 80)
c1, c2 = 50, 50
w1, w2 = 300, 600
h1, h2 = 100, 150
sp = 1
sc = 0
p = 0
i = 0
while True:
pygame.time.delay(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if p == 0 and event.key == 32:
p = 1
elif p == 1 and event.key == 32:
sp = -3
elif p == 2 and event.key == 32 and i == 30:
p = 0
c1, c2 = 50, 50
w1, w2 = 300, 600
h1, h2 = 100, 150
sp = 1
sc = 0
i = 0
if p == 0:
screen.fill((255, 255, 255))
screen.blit(png1, (c1, c2))
pygame.draw.rect(screen, (50, 200, 100), (w1, 0, 70, h1))
pygame.draw.rect(screen, (50, 200, 100), (w2, 0, 70, h2))
pygame.draw.rect(screen, (50, 200, 100), (w1, h1 + 150, 70, 400 - h1))
pygame.draw.rect(screen, (50, 200, 100), (w2, h2 + 150, 70, 400 - h2))
font1.render_to(screen, [30, 250], "click space to start")
if p == 1:
w1 -= 2
w2 -= 2
c2 += int(sp)
sp += 0.2
if c2 > 340:
p = 2
if 0 <= w1 <= 150 and not h1 <= c2 <= h1+50:
p = 2
if 0 <= w2 <= 150 and not h2 <= c2 <= h2+50:
p = 2
if w1 <= -70:
w1 += 600
h1 = random.randint(50, 200)
sc += 5
if w2 <= -70:
w2 += 600
h2 = random.randint(50, 200)
sc += 5
screen.fill((255, 255, 255))
screen.blit(png1, (c1, c2))
pygame.draw.rect(screen, (50, 200, 100), (w1, 0, 70, h1))
pygame.draw.rect(screen, (50, 200, 100), (w2, 0, 70, h2))
pygame.draw.rect(screen, (50, 200, 100), (w1, h1 + 150, 70, 400 - h1))
pygame.draw.rect(screen, (50, 200, 100), (w2, h2 + 150, 70, 400 - h2))
if p == 2:
screen.fill((255, 255, 255))
font2.render_to(screen, [80, 50], "score:%d" % sc)
font1.render_to(screen, [100, 250], "click space to play again")
if i < 30:
i += 1
pygame.display.update()
pygame 贪吃蛇
链接:https://www.cnblogs.com/xiaocaibiancheng/p/15775609.html
import pygame
import sys
import random
pygame.init()
screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption('snake')
direct = [0, 1]
body = []
for i in range(3, 8):
body.append([4, i])
head = [4, 7]
clock = 0
blank = []
for i in range(20):
for j in range(20):
if not [i, j] in body:
blank.append([i, j])
food = random.choice(blank)
while True:
pygame.time.delay(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == ord('a'):
new = [0, -1]
elif event.key == ord('d'):
new = [0, 1]
elif event.key == ord('w'):
new = [-1, 0]
elif event.key == ord('s'):
new = [1, 0]
else:
continue
if direct[0] == new[0] or direct[1] == new[1]:
continue
direct = new[:]
clock += 1
if clock >= 15:
clock = 0
old = [body[len(body)-1][0], body[len(body)-1][1]]
head = [old[0] + direct[0], old[1] + direct[1]]
if food == head:
blank = []
for i in range(20):
for j in range(20):
if not [i, j] in body:
blank.append([i, j])
food = random.choice(blank)
else:
del body[0]
if head in body:
pygame.quit()
sys.exit()
if not (0 <= head[0] < 20) or not (0 <= head[1] < 20):
pygame.quit()
sys.exit()
body.append(head)
screen.fill((0, 0, 0))
for i in body:
pygame.draw.rect(screen, (200, 200, 200), (20*i[1], 20*i[0], 20, 20))
pygame.draw.rect(screen, (100, 170, 255), (20*food[1], 20*food[0], 20, 20))
pygame.draw.rect(screen, (255, 255, 255), (20 * head[1], 20 * head[0], 20, 20))
pygame.display.update()