试图5天学会python——python小游戏

一、跳动的小球

from cProfile import run
from tkinter.tix import WINDOW
from turtle import Screen
import pygame
import os
import sys

# 0. 超参数
# 窗口尺寸
WINDOW_W, WINDOW_H = 640, 400

# 帧率
FPS = 50

# 重力加速度
g = 9.8 * 100

# 1. 初始化pygame
pygame.init()

# 2. 设置窗口
# 设置窗口出现的位置
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d"%(200, 100)

# 创建窗口
screen = pygame.display.set_mode([WINDOW_W, WINDOW_H])

# 设置窗口标题
pygame.display.set_caption("Jumping Ball")

# 3.创建时钟
clock = pygame.time.Clock()

# 4. 游戏本体
if __name__ == "__main__":
    # 定义小球坐标屏幕中间
    x, y = WINDOW_W / 2, 10

    #设置初始速度
    vx, vy = 0, 0

    # 游戏主循环
    while True:
        # 遍历事件
        for event in pygame.event.get():
            # 接收到推出事件后退出游戏
            if event.type == pygame.QUIT:
                pygame.exit()
                sys.exit()
            
        # 计算小球下一个时刻的速度和位置
        vy = vy + g * 1 / FPS
        y = y + vy * 1 / FPS

        # 如果小球到达地面,则速度反向
        if y >= WINDOW_H - 10:
            vy = -vy

        screen.fill((0, 0, 0))
        
        # 根据求得坐标画小球
        pygame.draw.circle(screen, (255, 0, 0), (int(x), int(y)), 10)

        # 刷新界面
        pygame.display.update()

        # 重新设置FPS
        time_passed = clock.tick(FPS)

二、贪吃蛇

from asyncio.windows_events import NULL
import pygame
import sys
import random
from snakeGame import SCREEN_X

SCREEN_X = 600
SCREEN_Y = 600

class Snake(object):
    def __init__(self) -> None:
        self.direction = pygame.K_RIGHT
        self.body = []
        for i in range(5):
            self.addnode()
    
    def addnode(self):
        left, top = (25, 25)
        if self.body:
            left, top = (self.body[0].left, self.body[0].top)

        node = pygame.Rect(left, top, 25, 25)

        if self.direction == pygame.K_LEFT:
            node.left -= 25
        elif self.direction == pygame.K_RIGHT:
            node.left += 25
        elif self.direction == pygame.K_UP:
            node.top -= 25
        elif self.direction == pygame.K_DOWN:
            node.top += 25

        self.body.insert(0, node)

    def isdead(self):
        if self.body[0].x not in range(SCREEN_X):
            return True
        if self.body[0].y not in range(SCREEN_Y):
            return True
        
        if self.body[0] in self.body[1:]:
            return True

    def delnode(self):
        self.body.pop()

    def move(self):
        self.addnode()
        self.delnode()

    def changedirection(self, curkey):
        LR = [pygame.K_LEFT, pygame.K_RIGHT]
        UD = [pygame.K_UP, pygame.K_DOWN]
        
        if curkey in (LR + UD):
            if (curkey in LR) and (self.direction in LR):
                return NULL
            if (curkey in UD) and (self.direction in UD):
                return NULL

            self.direction = curkey

    def print_text(self, screen, text, clock, pos, size1):
        font1 = pygame.font.Font(None, size1)
        surface1 = font1.render(text, True, clock)
        screen.blit(surface1, pos)

class Food(object):
    def __init__(self) -> None:
        self.rect = pygame.Rect(-25, 25, 25,25)

    def remove(self):
        self.rect.x = -25

    def set(self):
        if self.rect.x == -25:
            allpos = [i for i in range(25, SCREEN_X - 25, 25)]

            self.rect.left = random.choice(allpos)
            self.rect.top = random.choice(allpos)

def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y))
    pygame.display.set_caption('SNAKE GAME')
    clock = pygame.time.Clock()
    isdead = False
    score = 0

    snake = Snake()
    food = Food()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.exit()
                sys.exit()

            if event.type == pygame.KEYDOWN:
                snake.changedirection(event.key)
                if event.key == pygame.K_SPACE and isdead:
                    return main()
        
        screen.fill((255, 255, 255))

        if not isdead:
            snake.move()
            score += 5
            snake.print_text(screen, "score: " + str(score), 
            (138, 213, 253), (25, 25), 60)
        else:
            snake.print_text(screen, "Game over!", (255, 0 ,0), 
            (10, 230), 140)

        for rect in snake.body:
            pygame.draw.rect(screen, (0, 255, 0), rect, 0)

        isdead = snake.isdead()

        if food.rect == snake.body[0]:
            food.remove()
            snake.addnode()
            score += 20

        food.set()
        pygame.draw.rect(screen, (255, 0, 255), food.rect, 0)

        pygame.display.update()
        clock.tick(10)

if __name__ == '__main__':
    main()

三、BIU

import turtle
import time
from turtle import mainloop, hideturtle

def clear_all():
    turtle.penup()
    turtle.goto(0, 0)
    turtle.color('white')
    turtle.pensize(800)
    turtle.pendown()
    turtle.setheading(0)
    turtle.fd(300)
    turtle.bk(600)

# 重定位海龟的位置
def go_to(x, y, state):
    turtle.pendown() if state else turtle.penup()
    turtle.goto(x, y)

def draw_heart(size):
    turtle.color('red', 'pink')
    turtle.pensize(2)
    turtle.pendown()
    turtle.setheading(150)
    turtle.begin_fill()
    turtle.fd(size)
    turtle.circle(size * -3.745, 45)
    turtle.circle(size * -1.431, 165)
    turtle.left(120)
    turtle.circle(size * -1.431, 165)
    turtle.circle(size * -3.745, 45)
    turtle.fd(size)
    turtle.end_fill()

# 画出发射爱心的小人
def draw_people(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.pensize(2)
    turtle.color('black')
    turtle.setheading(0)
    turtle.circle(60, 360)
    turtle.penup()
    turtle.setheading(90)
    turtle.fd(75)
    turtle.setheading(180)
    turtle.fd(20)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(2, 360)
    turtle.setheading(0)
    turtle.penup()
    turtle.fd(40)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(-2, 360)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(20)
    turtle.setheading(0)
    turtle.fd(35)
    turtle.setheading(60)
    turtle.fd(10)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(40)
    turtle.setheading(0)
    turtle.fd(35)
    turtle.setheading(-60)
    turtle.fd(10)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(60)
    turtle.setheading(-135)
    turtle.fd(60)
    turtle.bk(60)
    turtle.setheading(-45)
    turtle.fd(30)
    turtle.setheading(-135)
    turtle.fd(35)
    turtle.penup()

# 绘制文字
def draw_text(text, t_color, font_size, show_time):
    turtle.penup()
    turtle.goto(-350, 0)
    turtle.color(t_color)
    turtle.write(text, font=('宋体', font_size, 'normal'))
    time.sleep(show_time)
    clear_all()

# 爱心发射
def draw_():
    turtle.speed(0)
    draw_people(-250, 20)
    turtle.penup()
    turtle.goto(-150, -30)
    draw_heart(14)
    turtle.penup()
    turtle.goto(-200, -200)
    turtle.color('pink')
    turtle.write('Biu~', font=('宋体', 60, 'normal'))
    turtle.penup()
    turtle.goto(-20, -60)
    draw_heart(25)
    turtle.penup()
    turtle.goto(-70, -200)
    turtle.color('pink')
    turtle.write('Biu~', font=('宋体', 60, 'normal'))
    turtle.penup()
    turtle.goto(200, -100)
    draw_heart(45)
    turtle.penup()
    turtle.goto(150, -200)
    turtle.color('pink')
    turtle.write('Biu~', font=('宋体', 60, 'normal'))
    turtle.hideturtle()
    time.sleep(3)

def main():
    # 隐藏海龟
    hideturtle()
    turtle.setup(900, 500)

    draw_text("Are You Readly?", "black", 60, 0)
    draw_text("接下来", "skyblue", 60, 0)
    draw_text("感谢你的出现,让我的日子这么甜!", "pink", 35, 3)
    draw_()
    # 使用mainloop防止窗口卡死
    mainloop()

    turtle.done()

if __name__ == '__main__':
    main()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值