Py吃金币游戏

尧前(要钱的谐音):掉下来金币吧!

边城(编程的谐音):吃金币游戏吧。

尧前:好好好!

代码:

import pygame
import random

# 初始化pygame
pygame.init()

# 设置游戏窗口的宽度和高度
width = 800
height = 600

# 创建游戏窗口
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("吃金币游戏")

# 定义颜色
black = (0, 0, 0)
white = (255, 255, 255)
yellow = (255, 255, 0)

# 定义角色的初始位置和大小
player_pos = [width / 2, height - 50]
player_size = 50

# 定义金币的初始位置和大小
coin_pos = [random.randint(0, width - 50), random.randint(0, height - 50)]
coin_size = 50

# 定义分数
score = 0

# 定义游戏结束
game_over = False

# 游戏主循环
while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.MOUSEMOTION:
            player_pos = pygame.mouse.get_pos()

    # 更新角色和金币的位置
    player_rect = pygame.Rect(player_pos[0], player_pos[1], player_size, player_size)
    coin_rect = pygame.Rect(coin_pos[0], coin_pos[1], coin_size, coin_size)

    # 检查角色和金币是否相撞
    if player_rect.colliderect(coin_rect):
        score += 1
        coin_pos = [random.randint(0, width - 50), random.randint(0, height - 50)]

    # 绘制游戏窗口
    window.fill(black)
    pygame.draw.rect(window, yellow, (coin_pos[0], coin_pos[1], coin_size, coin_size))
    pygame.draw.rect(window, white, (player_pos[0], player_pos[1], player_size, player_size))
    pygame.display.update()

# 退出游戏
pygame.quit()

尧前:搞出来是方块!

 Py吃金币游戏_第1张图片

Py吃金币游戏_第2张图片

边城:呃……改一下!

import pygame
import random

# 初始化pygame
pygame.init()

# 设置游戏窗口的宽度和高度
width = 800
height = 600

# 创建游戏窗口
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("吃金币游戏")

# 加载角色和金币的图像
species_img = pygame.image.load("species.png")
people_img = pygame.image.load("people.png")

# 定义角色的初始位置和大小
player_pos = [width/2, height-50]
player_size = 50

# 定义金币的初始位置和大小
coin_pos = [random.randint(0, width-50), random.randint(0, height-50)]
coin_size = 50

# 定义分数
score = 0

# 定义游戏结束
game_over = False

# 游戏主循环
while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.MOUSEMOTION:
            player_pos = pygame.mouse.get_pos()

    # 更新角色和金币的位置
    player_rect = pygame.Rect(player_pos[0], player_pos[1], player_size, player_size)
    coin_rect = pygame.Rect(coin_pos[0], coin_pos[1], coin_size, coin_size)

    # 检查角色和金币是否相撞
    if player_rect.colliderect(coin_rect):
        score += 1
        coin_pos = [random.randint(0, width-50), random.randint(0, height-50)]

    # 绘制游戏窗口
    window.fill((0, 0, 0))
    window.blit(species_img, (coin_pos[0], coin_pos[1]))
    window.blit(people_img, (player_pos[0], player_pos[1]))

    # 显示分数
    font = pygame.font.Font(None, 36)
    text = font.render("Score: " + str(score), True, (255, 255, 255))
    window.blit(text, (10, 10))

    pygame.display.update()

# 退出游戏
pygame.quit()

Py吃金币游戏_第3张图片        people.png

species.png

 Py吃金币游戏_第4张图片

尧前:能让金币从上往下落,人只能左右移动吗? 

边城:可以呀!

import pygame
import random

# 初始化pygame
pygame.init()

# 设置游戏窗口的宽度和高度
width = 800
height = 600

# 创建游戏窗口
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("吃金币游戏")

# 加载角色和金币的图像
species_img = pygame.image.load("species.png")
people_img = pygame.image.load("people.png")

# 定义角色的初始位置和大小
player_pos = [width/2, height-50]
player_size = 50

# 定义金币的初始位置和大小
coin_pos = [random.randint(0, width-50), -50]
coin_size = 50

# 定义分数
score = 0

# 定义角色的移动速度
player_speed = 5

# 定义游戏结束
game_over = False

# 游戏主循环
while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player_pos[0] -= player_speed
            elif event.key == pygame.K_RIGHT:
                player_pos[0] += player_speed

    # 更新金币的位置
    coin_pos[1] += 5

    # 检查角色和金币是否相撞
    player_rect = pygame.Rect(player_pos[0], player_pos[1], player_size, player_size)
    coin_rect = pygame.Rect(coin_pos[0], coin_pos[1], coin_size, coin_size)
    if player_rect.colliderect(coin_rect):
        score += 1
        coin_pos[1] = -50
        coin_pos[0] = random.randint(0, width-50)

    # 绘制游戏窗口
    window.fill((0, 0, 0))
    window.blit(species_img, (coin_pos[0], coin_pos[1]))
    window.blit(people_img, (player_pos[0], player_pos[1]))

    # 显示分数
    font = pygame.font.Font(None, 36)
    text = font.render("Score: " + str(score), True, (255, 255, 255))
    window.blit(text, (10, 10))

    pygame.display.update()

# 退出游戏
pygame.quit()

尧前:金币怎么可以只掉一枚!还那么快!

边城:呃……改改改!

import pygame
import random

# 初始化pygame
pygame.init()

# 设置游戏窗口尺寸
window_width = 800
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("吃金币游戏")

# 加载金币和人物图片
coin_image = pygame.image.load("species.png").convert_alpha()
player_image = pygame.image.load("people.png").convert_alpha()

# 设置金币和人物的初始位置
player_x = window_width // 2
player_y = window_height - player_image.get_height() - 30
coin_x = random.randint(0, window_width - coin_image.get_width())
coin_y = -100
coin_speed = 5

# 初始化分数
score = 0

# 创建字体对象
font = pygame.font.SysFont(None, 36)

# 游戏主循环
running = True
clock = pygame.time.Clock()
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 监听键盘事件
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player_x -= 5
    if keys[pygame.K_RIGHT]:
        player_x += 5

    # 更新金币位置
    coin_y += coin_speed
    if coin_y > window_height:
        coin_x = random.randint(0, window_width - coin_image.get_width())
        coin_y = -100
        score += 1

    # 检测碰撞
    player_rect = pygame.Rect(player_x, player_y, player_image.get_width(), player_image.get_height())
    coin_rect = pygame.Rect(coin_x, coin_y, coin_image.get_width(), coin_image.get_height())
    if player_rect.colliderect(coin_rect):
        coin_x = random.randint(0, window_width - coin_image.get_width())
        coin_y = -100
        score += 1

    # 绘制游戏场景
    window.fill((255, 255, 255))
    window.blit(player_image, (player_x, player_y))
    window.blit(coin_image, (coin_x, coin_y))

    # 显示分数
    score_text = font.render("Score: " + str(score), True, (0, 0, 0))
    window.blit(score_text, (10, 10))

    # 更新屏幕显示
    pygame.display.update()
    clock.tick(60)

# 退出游戏
pygame.quit()

Py吃金币游戏_第5张图片

 尧前:没背景,一点都不正常。

边城:好好好,改改改。

import pygame
import random

# 初始化pygame
pygame.init()

# 设置游戏窗口尺寸
window_width = 800
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("吃金币游戏")

# 加载背景、金币和人物图片
background_image = pygame.image.load("background.png").convert()
coin_image = pygame.image.load("species.png").convert_alpha()
player_image = pygame.image.load("people.png").convert_alpha()

# 设置背景图片尺寸与游戏窗口一致
background = pygame.transform.scale(background_image, (window_width, window_height))

# 设置金币和人物的初始位置
player_x = window_width // 2
player_y = window_height - player_image.get_height() - 30
coin_x = random.randint(0, window_width - coin_image.get_width())
coin_y = -100
coin_speed = 5

# 初始化分数
score = 0

# 创建字体对象
font = pygame.font.SysFont(None, 36)

# 游戏主循环
running = True
clock = pygame.time.Clock()
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            
    # 监听键盘事件
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player_x -= 5
    if keys[pygame.K_RIGHT]:
        player_x += 5
    
    # 更新金币位置
    coin_y += coin_speed
    if coin_y > window_height:
        coin_x = random.randint(0, window_width - coin_image.get_width())
        coin_y = -100
        score += 1
        
    # 检测碰撞
    player_rect = pygame.Rect(player_x, player_y, player_image.get_width(), player_image.get_height())
    coin_rect = pygame.Rect(coin_x, coin_y, coin_image.get_width(), coin_image.get_height())
    if player_rect.colliderect(coin_rect):
        coin_x = random.randint(0, window_width - coin_image.get_width())
        coin_y = -100
        score += 1

    # 绘制游戏场景
    window.blit(background, (0, 0))  # 绘制背景
    window.blit(player_image, (player_x, player_y))
    window.blit(coin_image, (coin_x, coin_y))

    # 显示分数
    score_text = font.render("Score: " + str(score), True, (0, 0, 0))
    window.blit(score_text, (10, 10))

    # 更新屏幕显示
    pygame.display.update()
    clock.tick(60)

# 退出游戏
pygame.quit()

尧前:我看看

Py吃金币游戏_第6张图片

background.png

 Py吃金币游戏_第7张图片

 尧前:哇哦!

Py吃金币游戏_第8张图片

你可能感兴趣的:(游戏,pygame,python)