达永编程网

程序员技术分享与交流平台

自己写的游戏代码,拿去玩吧!

import pygame

import random

import math

# 初始化

pygame.init()

screen = pygame.display.set_mode((800, 600))

clock = pygame.time.Clock()


pygame.display.set_caption("疯狂打螺丝 - 工厂突击!")

# 游戏状态

class GameState:

def __init__(self):

self.reset()


def reset(self):

self.score = 0

self.lives = 3

self.time = 60

self.screws = []

self.level = 1

self.game_over = False

# 螺丝类

class Screw:

def __init__(self):

self.x = random.randint(100, 700)

self.y = random.randint(100, 500)

self.angle = 0

self.progress = 0 # 0-100

self.speed = random.choice([2, 3, 4])

self.type = random.choices(

['normal', 'golden', 'broken'],

weights=[80, 15, 5],

k=1

)[0]

# 资源加载

font = pygame.font.SysFont('arial', 30)

screw_img = pygame.image.load('screw.png') # 需要准备图片

hammer_img = pygame.image.load('hammer.png') # 锤子光标

def draw_progress_bar(surface, rect, progress):

pygame.draw.rect(surface, (50,50,50), rect)

inner = rect.inflate(-4, -4)

width = inner.width * progress / 100

fill_rect = pygame.Rect(inner.left, inner.top, width, inner.height)

color = (0,200,0) if progress < 80 else (200,0,0)

pygame.draw.rect(surface, color, fill_rect)

def game_loop():

state = GameState()

last_spawn = pygame.time.get_ticks()


while not state.game_over:

# 事件处理

for event in pygame.event.get():

if event.type == pygame.QUIT:

return

if event.type == pygame.MOUSEBUTTONDOWN:

mx, my = pygame.mouse.get_pos()

for screw in state.screws[:]:

dx = screw.x - mx

dy = screw.y - my

if math.sqrt(dx*dx + dy*dy) < 30:

if screw.type == 'normal':

screw.progress += 15

state.score += 10

elif screw.type == 'golden':

screw.progress += 30

state.score += 50

elif screw.type == 'broken':

state.lives -= 1

# 生成逻辑

now = pygame.time.get_ticks()

if now - last_spawn > 2000 / state.level:

state.screws.append(Screw())

last_spawn = now

if random.random() < 0.1:

state.level += 0.2

# 更新状态

state.time -= 1/60

for screw in state.screws[:]:

screw.angle += screw.speed

if screw.progress >= 100:

state.screws.remove(screw)

state.score += 100

elif screw.angle > 360:

screw.angle = 0

screw.progress -= 5

if screw.progress < 0:

state.screws.remove(screw)

state.lives -= 1

# 输赢判断

if state.time <= 0 or state.lives <= 0:

state.game_over = True

# 绘制界面

screen.fill((30, 30, 40))


# 绘制HUD

text = font.render(f"分数: {state.score}", True, (255,255,255))

screen.blit(text, (20, 20))

text = font.render(f"剩余时间: {int(state.time)}", True, (255,255,255))

screen.blit(text, (20, 60))

text = font.render(f"生命: {'' * state.lives}", True, (200,50,50))

screen.blit(text, (20, 100))


# 绘制螺丝

for screw in state.screws:

rotated = pygame.transform.rotate(screw_img, screw.angle)

rect = rotated.get_rect(center=(screw.x, screw.y))

screen.blit(rotated, rect)


# 进度条

draw_progress_bar(

screen,

pygame.Rect(screw.x-40, screw.y+50, 80, 12),

screw.progress

)


# 特殊类型标识

if screw.type == 'golden':

pygame.draw.circle(screen, (255,215,0), (screw.x, screw.y-40), 8)

elif screw.type == 'broken':

pygame.draw.circle(screen, (100,100,100), (screw.x, screw.y-40), 8)

# 绘制锤子光标

mx, my = pygame.mouse.get_pos()

screen.blit(hammer_img, (mx-15, my-15))


pygame.display.flip()

clock.tick(60)

# 游戏结束界面

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

return

#小游戏# 自己开发的游戏代码,拿去玩

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言