python 取屏幕尺寸大小代码
时间: 2023-04-08 08:05:19 浏览: 105
以下是获取屏幕尺寸大小的 Python 代码:
```python
import tkinter as tk
root = tk.Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
print("屏幕宽度:", width)
print("屏幕高度:", height)
```
这段代码使用 tkinter 库创建了一个窗口,然后调用 `winfo_screenwidth()` 和 `winfo_screenheight()` 方法获取屏幕的宽度和高度。最后将结果打印出来。
相关问题
python pygame 蛇吃豆代码
Python Pygame是一个用于开发2D游戏的库,而蛇吃豆代码是一个经典的游戏示例。下面是一个简单的Python Pygame蛇吃豆代码示例:
```python
import pygame
import random
# 初始化游戏
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 设置屏幕尺寸
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Snake Game")
# 定义蛇的初始位置和大小
snake_block_size = 20
snake_speed = 15
x1 = screen_width / 2
y1 = screen_height / 2
x1_change = 0
y1_change = 0
# 定义豆子的初始位置和大小
food_block_size = 20
food_x = round(random.randrange(0, screen_width - food_block_size) / 20) * 20
food_y = round(random.randrange(0, screen_height - food_block_size) / 20) * 20
clock = pygame.time.Clock()
# 定义蛇的移动函数
def snake(snake_block_size, snake_list):
for x in snake_list:
pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block_size, snake_block_size])
# 游戏主循环
def game_loop():
game_over = False
game_close = False
# 蛇的初始长度
snake_list = []
length_of_snake = 1
while not game_over:
while game_close == True:
screen.fill(BLACK)
font_style = pygame.font.SysFont(None, 50)
message = font_style.render("Game Over! Press Q-Quit or C-Play Again", True, RED)
screen.blit(message, [screen_width / 6, screen_height / 3])
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
game_loop()
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 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
game_close = True
# 更新蛇的位置
x1 += x1_change
y1 += y1_change
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, [food_x, food_y, 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_close = True
snake(snake_block_size, snake_list)
pygame.display.update()
# 判断蛇是否吃到豆子,如果吃到,则长度加1,并重新生成豆子
if x1 == food_x and y1 == food_y:
food_x = round(random.randrange(0, screen_width - food_block_size) / 20) * 20
food_y = round(random.randrange(0, screen_height - food_block_size) / 20) * 20
length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
# 启动游戏循环
game_loop()
```
python贪吃蛇代码
以下是一个简单的 Python 贪吃蛇代码示例:
```python
import pygame
import random
# 初始化 Pygame 库
pygame.init()
# 定义屏幕尺寸和背景颜色
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BG_COLOR = (0, 0, 0)
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 设置窗口标题
pygame.display.set_caption('贪吃蛇')
# 定义贪吃蛇的初始位置和大小
snake_pos = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
# 定义食物的初始位置和大小
food_pos = [random.randrange(1, SCREEN_WIDTH // 10) * 10,
random.randrange(1, SCREEN_HEIGHT // 10) * 10]
food_size = [10, 10]
# 定义贪吃蛇的移动速度和方向
snake_speed = 10
snake_dir = "RIGHT"
# 创建时钟对象,用于控制游戏帧率
clock = pygame.time.Clock()
# 定义游戏循环标志
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_UP and snake_dir != "DOWN":
snake_dir = "UP"
elif event.key == pygame.K_DOWN and snake_dir != "UP":
snake_dir = "DOWN"
elif event.key == pygame.K_LEFT and snake_dir != "RIGHT":
snake_dir = "LEFT"
elif event.key == pygame.K_RIGHT and snake_dir != "LEFT":
snake_dir = "RIGHT"
# 移动贪吃蛇的身体
if snake_dir == "UP":
snake_pos[1] -= snake_speed
elif snake_dir == "DOWN":
snake_pos[1] += snake_speed
elif snake_dir == "LEFT":
snake_pos[0] -= snake_speed
elif snake_dir == "RIGHT":
snake_pos[0] += snake_speed
# 判断贪吃蛇是否吃到食物
if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:
food_pos = [random.randrange(1, SCREEN_WIDTH // 10) * 10,
random.randrange(1, SCREEN_HEIGHT // 10) * 10]
snake_body.append([snake_pos[0], snake_pos[1]])
# 绘制游戏背景
screen.fill(BG_COLOR)
# 绘制食物
pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(
food_pos[0], food_pos[1], food_size[0], food_size[1]))
# 绘制贪吃蛇的身体
for pos in snake_body:
pygame.draw.rect(screen, (0, 255, 0), pygame.Rect(
pos[0], pos[1], 10, 10))
# 更新贪吃蛇身体位置
snake_body.insert(0, list(snake_pos))
snake_body.pop()
# 判断贪吃蛇是否碰到边界或自身
if snake_pos[0] < 0 or snake_pos[0] > SCREEN_WIDTH-10:
game_over = True
elif snake_pos[1] < 0 or snake_pos[1] > SCREEN_HEIGHT-10:
game_over = True
for block in snake_body[1:]:
if snake_pos[0] == block[0] and snake_pos[1] == block[1]:
game_over = True
# 刷新屏幕
pygame.display.update()
# 控制游戏帧率
clock.tick(20)
# 退出 Pygame 库
pygame.quit()
```