python课设小游戏
时间: 2023-12-02 18:43:25 浏览: 105
以下是一些Python课设小游戏的示例,它们可以帮助您学习Python编程并提高您的编程技能:
1. 猜数字游戏
这是一个简单的猜数字游戏,它要求用户猜一个随机生成的数字。游戏会提示用户猜的数字是太大还是太小,直到用户猜中为止。
```python
import random
number = random.randint(1, 100)
guess = int(input("Guess a number between 1 and 100: "))
while guess != number:
if guess < number:
print("Too low")
else:
print("Too high")
guess = int(input("Guess again: "))
print("You guessed it!")
```
2. 石头,剪子,布游戏
这是一个经典的石头,剪子,布游戏,它要求用户选择石头,剪子或布,并与计算机进行比较。
```python
import random
options = ["rock", "paper", "scissors"]
computer_choice = random.choice(options)
user_choice = input("Choose rock, paper, or scissors: ")
print("Computer chose", computer_choice)
if user_choice == computer_choice:
print("Tie!")
elif user_choice == "rock" and computer_choice == "scissors":
print("You win!")
elif user_choice == "paper" and computer_choice == "rock":
print("You win!")
elif user_choice == "scissors" and computer_choice == "paper":
print("You win!")
else:
print("Computer wins!")
```
3. 贪吃蛇游戏
这是一个经典的贪吃蛇游戏,它要求用户控制蛇吃食物并避免碰到墙壁或自己的身体。
```python
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("Snake Game")
# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
# 定义蛇的初始位置和大小
snake_block_size = 10
snake_speed = 15
x1 = window_width / 2
y1 = window_height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
# 定义食物的初始位置和大小
food_block_size = 10
foodx = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0
# 定义字体
font_style = pygame.font.SysFont(None, 50)
# 定义得分函数
def Your_score(score):
value = font_style.render("Your Score: " + str(score), True, white)
window.blit(value, [0, 0])
# 定义蛇的函数
def our_snake(snake_block_size, snake_List):
for x in snake_List:
pygame.draw.rect(window, green, [x[0], x[1], snake_block_size, snake_block_size])
# 开始游戏循环
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block_size
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block_size
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block_size
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block_size
x1_change = 0
# 判断是否碰到墙壁
if x1 >= window_width or x1 < 0 or y1 >= window_height or y1 < 0:
game_over = True
# 更新蛇的位置
x1 += x1_change
y1 += y1_change
# 绘制食物
window.fill(black)
pygame.draw.rect(window, red, [foodx, foody, food_block_size, food_block_size])
# 更新蛇的长度
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
# 判断是否吃到食物
for x in snake_List[:-1]:
if x == snake_Head:
game_over = True
our_snake(snake_block_size, snake_List)
Your_score(Length_of_snake - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, window_width - food_block_size) / 10.0) * 10.0
foody = round(random.randrange(0, window_height - food_block_size) / 10.0) * 10.0
Length_of_snake += 1
# 控制蛇的速度
clock = pygame.time.Clock()
clock.tick(snake_speed)
# 退出Pygame
pygame.quit()
--相关问题--:
阅读全文