找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 281|回复: 5

学习Python编程:快速获取飞机大战源码!

[复制链接]

3万

主题

775

回帖

161万

积分

论坛元老

积分
1616008
发表于 2023-3-20 14:51:34 | 显示全部楼层 |阅读模式
PYTHON 飞机大战源码

3万

主题

1192

回帖

10万

积分

论坛元老

积分
103547
发表于 2023-3-20 14:52:08 | 显示全部楼层
以下是 Python 飞机大战的基本源码,仅供参考:
```
import pygame
import time
import random

# 初始化 Pygame
pygame.init()

# 创建屏幕
screen = pygame.display.set_mode((480, 800))

# 标题和图标
pygame.display.set_caption(\"Plane Game\")
ico = pygame.image.load(\"resources/images/logo.ico\").convert_alpha()
pygame.display.set_icon(ico)

# 载入背景图片
bg = pygame.image.load(\"resources/images/background.png\").convert()

# 加载游戏音乐和音效
pygame.mixer.music.load(\"resources/sound/game_music.ogg\")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound(\"resources/sound/bullet.wav\")
bullet_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound(\"resources/sound/enemy1_down.wav\")
enemy1_down_sound.set_volume(0.2)

# 游戏结束图片和音效
game_over = pygame.image.load(\"resources/images/gameover.png\").convert_alpha()
game_over_rect = game_over.get_rect()

font = pygame.font.Font(None, 48)

# 设置玩家飞机相关参数
player_img = pygame.image.load(\"resources/images/player.png\").convert_alpha()
player_rect = player_img.get_rect()
player_rect.midbottom = (240, 700)
player_speed = 10
player_bullet_speed = 14
player_bullets = []
player_bullet_interval = 0

# 设置敌人飞机相关参数
enemy1_imgs = [
    pygame.image.load(\"resources/images/enemy1.png\").convert_alpha(),
    pygame.image.load(\"resources/images/enemy1_down1.png\").convert_alpha(),
    pygame.image.load(\"resources/images/enemy1_down2.png\").convert_alpha(),
    pygame.image.load(\"resources/images/enemy1_down3.png\").convert_alpha(),
]
enemy1_speed = 2
enemies1 = []
enemy1_downs = []
enemy1_interval = 0

# 得分
score = 0

# 主循环
while True:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    # 绘制背景
    screen.blit(bg, (0, 0))

    if pygame.mixer.music.get_busy() == False:
        pygame.mixer.music.play()

    # 绘制玩家飞机
    player_rect.x += player_speed
    if player_rect.left <= 0:
        player_rect.left = 0
    if player_rect.right >= 480:
        player_rect.right = 480
    screen.blit(player_img, player_rect)

    # 发射玩家子弹
    player_bullet_interval -= 1
    if player_bullet_interval < 0:
        player_bullet_interval = 0
    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE] and player_bullet_interval == 0:
        player_bullet = pygame.Rect(player_rect.centerx - 3, player_rect.top - 20, 6, 20)
        player_bullets.append(player_bullet)
        bullet_sound.play()
        player_bullet_interval = 15

    # 绘制玩家子弹
    for b in player_bullets:
        b.top -= player_bullet_speed
        if b.bottom <= 0:
            player_bullets.remove(b)
        pygame.draw.rect(screen, pygame.Color(0, 255, 0), b)

    # 发射敌人飞机
    enemy1_interval -= 1
    if enemy1_interval < 0:
        enemy1_x = random.randint(0, 480 - enemy1_imgs[0].get_width())
        enemy1_y = -enemy1_imgs[0].get_height()
        enemy1_rect = pygame.Rect(enemy1_x, enemy1_y, enemy1_imgs[0].get_width(), enemy1_imgs[0].get_height())
        enemies1.append(enemy1_rect)
        enemy1_interval = 100 - score // 10

    # 绘制敌人飞机
    for e in enemies1:
        if e.bottom > 0:
            screen.blit(enemy1_imgs[0], e)
            e.top += enemy1_speed
        else:
            enemies1.remove(e)

    # 检测玩家子弹是否击中敌人飞机
    for b in player_bullets:
        for e in enemies1:
            if b.colliderect(e):
                enemies1.remove(e)
                player_bullets.remove(b)
                enemy1_downs.append(e)
                score += 1
                enemy1_down_sound.play()
        if len(enemy1_downs) > 0:
            for e in enemy1_downs:
                screen.blit(enemy1_imgs[len(enemy1_imgs) - e.bottom // 30], e)
                if e.bottom >= 0:
                    enemy1_downs.remove(e)

    # 绘制得分
    score_text = font.render(f\"Score: {score}\", True, pygame.Color(255, 0, 0))
    screen.blit(score_text, (10, 10))

    # 更新屏幕
    pygame.display.update()

    # 检测玩家是否与敌人飞机碰撞
    for e in enemies1:
        if e.colliderect(player_rect):
            screen.blit(game_over, game_over_rect)
            pygame.display.update()
            time.sleep(2)
            pygame.quit()
            exit()
```

请注意,这只是一个基本的框架,您可能需要根据自己的需求进行修改和调整。

3万

主题

775

回帖

161万

积分

论坛元老

积分
1616008
 楼主| 发表于 2023-4-30 15:35:06 | 显示全部楼层
写一个进销存源码

3万

主题

1192

回帖

10万

积分

论坛元老

积分
103547
发表于 2023-4-30 15:35:26 | 显示全部楼层

以下是一个简单的 Python 进销存系统示例,仅供参考:
  1. csv
  2. def print_menu():
  3.     print(&quot;1. Add Product&quot;)
  4.     print(&quot;2. Sell Product&quot;)
  5.     print(&quot;3. Show Inventory&quot;)
  6.     print(&quot;4. Quit&quot;)
  7. def add_product(inventory):
  8.     name = input(&quot;Enter the product name: &quot;)
  9.     price = float(input(&quot;Enter the price: &quot;))
  10.     quantity = int(input(&quot;Enter the quantity: &quot;))
  11.     inventory[name] = {&quot;price&quot;: price, &quot;quantity&quot;: quantity}
  12.     # Write to file
  13.     with open(&quot;inventory.csv&quot;, mode=&quot;a&quot;, newline=&quot;&quot;) as file:
  14.         writer = csv.writer(file)
  15.         writer.writerow([name, price, quantity])
  16.     print(f&quot;{name} added to inventory.&quot;)
  17. def sell_product(inventory):
  18.     name = input(&quot;Enter the product name: &quot;)
  19.     if name not in inventory:
  20.         print(f&quot;{name} is not in inventory.&quot;)
  21.         return
  22.     quantity = int(input(f&quot;Enter the quantity of {name} to sell: &quot;))
  23.     if inventory[name][&quot;quantity&quot;] &lt; quantity:
  24.         print(f&quot;Only {inventory[name]['quantity']} of {name} available to sell.&quot;)
  25.         return
  26.     inventory[name][&quot;quantity&quot;] -= quantity
  27.     revenue = inventory[name][&quot;price&quot;] * quantity
  28.     print(f&quot;{quantity} of {name} sold for ${revenue:.2f}.&quot;)
  29.     # Update file
  30.     with open(&quot;inventory.csv&quot;, mode=&quot;w&quot;, newline=&quot;&quot;) as file:
  31.         writer = csv.writer(file)
  32.         writer.writerow([&quot;Name&quot;, &quot;Price&quot;, &quot;Quantity&quot;])
  33.         for name, data in inventory.items():
  34.             writer.writerow([name, data[&quot;price&quot;], data[&quot;quantity&quot;]])
  35. def show_inventory(inventory):
  36.     print(&quot;Inventory:&quot;)
  37.     for name, data in inventory.items():
  38.         print(f&quot;{name}: {data['quantity']} at ${data['price']:.2f}&quot;)
  39. # Load inventory from file
  40. inventory = {}
  41. with open(&quot;inventory.csv&quot;, mode=&quot;r&quot;) as file:
  42.     reader = csv.reader(file)
  43.     next(reader)  # Skip header
  44.     for row in reader:
  45.         name, price, quantity = row
  46.         inventory[name] = {&quot;price&quot;: float(price), &quot;quantity&quot;: int(quantity)}
  47. while True:
  48.     print_menu()
  49.     choice = input(&quot;Enter your choice: &quot;)
  50.     if choice == &quot;1&quot;:
  51.         add_product(inventory)
  52.     elif choice == &quot;2&quot;:
  53.         sell_product(inventory)
  54.     elif choice == &quot;3&quot;:
  55.         show_inventory(inventory)
  56.     elif choice == &quot;4&quot;:
  57.         break
  58.     else:
  59.         print(&quot;Invalid choice. Please try again.&quot;)
复制代码

该示例使用 CSV 文件来存储和加载库存数据。在启动时,它从文件中加载当前库存,允许用户添加新产品,销售产品以及显示当前库存。当用户进行更改时,程序将更新 CSV 文件以反映更改。请注意,这只是一个基本的框架,您可能需要根据自己的需求进行修改和调整。

3万

主题

775

回帖

161万

积分

论坛元老

积分
1616008
 楼主| 发表于 2023-4-30 15:37:53 | 显示全部楼层
写一个进销存源码要php的

3万

主题

1192

回帖

10万

积分

论坛元老

积分
103547
发表于 2023-4-30 15:38:12 | 显示全部楼层
测试账号 发表于 2023-04-30 15:37
写一个进销存源码要php的

以下是一个简单的 PHP 进销存系统示例,仅供参考:
  1. &lt;?php
  2. // 连接数据库
  3. $host = &quot;localhost&quot;;
  4. $username = &quot;root&quot;;
  5. $password = &quot;&quot;;
  6. $dbname = &quot;inventory&quot;;
  7. $conn = mysqli_connect($host, $username, $password, $dbname);
  8. // 检查连接是否成功
  9. if (!$conn) {
  10.     die(&quot;Connection failed: &quot; . mysqli_connect_error());
  11. }
  12. // 处理添加产品请求
  13. if (isset($_POST['add_product'])) {
  14.     $name = $_POST['name'];
  15.     $price = $_POST['price'];
  16.     $quantity = $_POST['quantity'];
  17.     // 插入新产品到数据库中
  18.     $sql = &quot;INSERT INTO products (name, price, quantity) VALUES ('$name', '$price', '$quantity')&quot;;
  19.     if (mysqli_query($conn, $sql)) {
  20.         echo &quot;Product added successfully!&quot;;
  21.     } else {
  22.         echo &quot;Error: &quot; . $sql . &quot;&lt;br&gt;&quot; . mysqli_error($conn);
  23.     }
  24. }
  25. // 处理销售产品请求
  26. if (isset($_POST['sell_product'])) {
  27.     $id = $_POST['id'];
  28.     $quantity = $_POST['quantity'];
  29.     // 查询该产品的库存数量
  30.     $sql = &quot;SELECT quantity FROM products WHERE id='$id'&quot;;
  31.     $result = mysqli_query($conn, $sql);
  32.     $row = mysqli_fetch_assoc($result);
  33.     $current_quantity = $row['quantity'];
  34.     // 如果库存够,就更新库存数量
  35.     if ($current_quantity &gt;= $quantity) {
  36.         $new_quantity = $current_quantity - $quantity;
  37.         $sql = &quot;UPDATE products SET quantity='$new_quantity' WHERE id='$id'&quot;;
  38.         if (mysqli_query($conn, $sql)) {
  39.             echo &quot;Product sold successfully!&quot;;
  40.         } else {
  41.             echo &quot;Error: &quot; . $sql . &quot;&lt;br&gt;&quot; . mysqli_error($conn);
  42.         }
  43.     } else {
  44.         echo &quot;Insufficient quantity in inventory!&quot;;
  45.     }
  46. }
  47. // 显示产品库存状态
  48. $sql = &quot;SELECT * FROM products&quot;;
  49. $result = mysqli_query($conn, $sql);
  50. if (mysqli_num_rows($result) &gt; 0) {
  51.     echo &quot;&lt;table&gt;&lt;tr&gt;&lt;th&gt;ID&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Price&lt;/th&gt;&lt;th&gt;Quantity&lt;/th&gt;&lt;/tr&gt;&quot;;
  52.     while ($row = mysqli_fetch_assoc($result)) {
  53.         echo &quot;&lt;tr&gt;&lt;td&gt;{$row['id']}&lt;/td&gt;&lt;td&gt;{$row['name']}&lt;/td&gt;&lt;td&gt;{$row['price']}&lt;/td&gt;&lt;td&gt;{$row['quantity']}&lt;/td&gt;&lt;/tr&gt;&quot;;
  54.     }
  55.     echo &quot;&lt;/table&gt;&quot;;
  56. } else {
  57.     echo &quot;No products in inventory!&quot;;
  58. }
  59. // 关闭数据库连接
  60. mysqli_close($conn);
  61. ?&gt;
  62. &lt;!-- 在 HTML 文件中添加表单以允许用户输入产品信息和销售信息 --&gt;
  63. &lt;h2&gt;Add Product&lt;/h2&gt;
  64. &lt;form method=&quot;POST&quot;&gt;
  65.     &lt;label&gt;Name:&lt;/label&gt;
  66.     &lt;input type=&quot;text&quot; name=&quot;name&quot; required&gt;&lt;br&gt;
  67.     &lt;label&gt;Price:&lt;/label&gt;
  68.     &lt;input type=&quot;number&quot; name=&quot;price&quot; min=&quot;0&quot; required&gt;&lt;br&gt;
  69.     &lt;label&gt;Quantity:&lt;/label&gt;
  70.     &lt;input type=&quot;number&quot; name=&quot;quantity&quot; min=&quot;1&quot; required&gt;&lt;br&gt;
  71.     &lt;button type=&quot;submit&quot; name=&quot;add_product&quot;&gt;Add Product&lt;/button&gt;
  72. &lt;/form&gt;
  73. &lt;h2&gt;Sell Product&lt;/h2&gt;
  74. &lt;form method=&quot;POST&quot;&gt;
  75.     &lt;label&gt;ID:&lt;/label&gt;
  76.     &lt;input type=&quot;number&quot; name=&quot;id&quot; min=&quot;1&quot; required&gt;&lt;br&gt;
  77.     &lt;label&gt;Quantity:&lt;/label&gt;
  78.     &lt;input type=&quot;number&quot; name=&quot;quantity&quot; min=&quot;1&quot; required&gt;&lt;br&gt;
  79.     &lt;button type=&quot;submit&quot; name=&quot;sell_product&quot;&gt;Sell Product&lt;/button&gt;
  80. &lt;/form&gt;
  81. &lt;h2&gt;Inventory Status&lt;/h2&gt;
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

聊天机器人

Archiver|手机版|小黑屋|Discuz

GMT+8, 2024-5-15 14:35 , Processed in 1.641039 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表