第五周学习周记

在上周主要学习了python Django框架跟着前人的博客,尝试做了以下,以下是代码
urls.py
from mydb import views

from django.urls import re_path

urlpatterns = [
re_path(r’^index/$’, views.index),
]
settings.py
INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘mydb’,
]
新添了mydb文件
views.py
def index(request):
return render(request, ‘index.html’)
用作请求
在mydb文件中
models.py
from django.db import models

“# Create your models here.”

class UserInfo(models.Model):
user = models.CharField(max_length=32)
pwd = models.CharField(max_length=32)
views.py
from django.shortcuts import render
from mydb import models
“#from django.shortcuts import HttpResponse”

def index(request):
if request.method == “POST”:
username = request.POST.get(“username”, None)
password = request.POST.get(“password”, None)
models.UserInfo.objects.create(user=username, pwd=password)
user_list = models.UserInfo.objects.all()

return render(request, "index.html", {"data": user_list})

“# Create your views here.”
以上是Django 开发中我们需要改的文件
在上周的学习中我们还学习了飞机大战游戏的开发
pygame_main_.py
import pygame
from plane_sprites import *

class PlaneGame(object):
def init(self):
print(“游戏初始化”)
#创建游戏窗口
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
#创建游戏时钟
self.clock = pygame.time.Clock()
# 调用私有方法创建精灵和精灵组
self.__create_sprites()
#设置定时器事件
pygame.time.set_timer(CREATE_ENEMY, 1000)
pygame.time.set_timer(HERO_FIRE,500)

def __create_sprites(self):
    bg1 = BackGround()
    bg2 = BackGround(True)
    bg2.rect.y = - bg2.rect.height
    self.back_group = pygame.sprite.Group(bg1, bg2)
    self.enemy_group = pygame.sprite.Group()
    self.hero = Hero()
    self.hero_group = pygame.sprite.Group(self.hero)
def start_game(self):
    print("游戏开始")
    while True:
        #设置刷新帧率
        self.clock.tick(60)
        #事件监听
        self.__event_hander()
        #碰撞检测
        self.__check_collied()
        #更新绘制精灵组
        self.__update_sprites()
        #更新显示
        pygame.display.update()
        pass

def __event_hander(self):
    for event in pygame.event.get():
        #判断用户是否退出游戏
        if event.type == pygame.QUIT:
            PlaneGame.__game_over()
        elif event.type == CREATE_ENEMY:
           # print("敌机出场")
            enemy = Enemy()
            self.enemy_group.add(enemy)
        elif event.type == HERO_FIRE:
            self.hero.fire()
    keys_pressed = pygame.key.get_pressed()
    if keys_pressed[pygame.K_RIGHT]:
        self.hero.speed = 2
    elif keys_pressed[pygame.K_LEFT]:
        self.hero.speed = -2
    else:
        self.hero.speed=0

def __check_collied(self):
    pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)
    enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
    if len(enemies)>0:
        '''英雄牺牲'''
        self.hero.kill()
        PlaneGame.__game_over()


def __update_sprites(self):
    self.back_group.update()
    self.back_group.draw(self.screen)
    self.enemy_group.update()
    self.enemy_group.draw(self.screen)
    self.hero_group.update()
    self.hero_group.draw(self.screen)
    self.hero.bullets.update()
    self.hero.bullets.draw(self.screen)
@staticmethod
def __game_over():
    print("游戏结束")
    pygame.quit()
    exit()

if name == ‘main’:
game = PlaneGame()
game.start_game()
sprites.py
import random

import pygame
SCREEN_RECT = pygame.Rect(0, 0, 480, 700)
CREATE_ENEMY = pygame.USEREVENT
HERO_FIRE = pygame.USEREVENT+1

class GameSprites(pygame.sprite.Sprite):
‘’‘飞机大战游戏精灵’’’

def __init__(self, image_name, speed=1):
    super().__init__()
    self.image = pygame.image.load(image_name)
    self.rect = self.image.get_rect()
    self.speed = speed

def update(self, *args):
    self.rect.y += self.speed

class BackGround(GameSprites):
‘’’ 游戏背景’’’
def init(self, is_out=False):
super().init("./images/background.png" )
if is_out:
self.rect.y = - self.rect.y

def update(self, *args):
    super().update()
    if self.rect.y >= SCREEN_RECT.height:
        self.rect.y = - self.rect.height

class Enemy(GameSprites):
def init(self):
super().init("./images/enemy.png")
self.speed = random.randint(1, 3)
self.rect.bottom = 0
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0, max_x)

def update(self, *args):
    super().update()
    if self.rect.y >= SCREEN_RECT.height:
        #print("飞出")
        self.kill()

def __del__(self):
    pass

class Hero(GameSprites):
def init(self):
super().init("./images/hero.png", 0)
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.bottom - 120
self.bullets = pygame.sprite.Group()

def update(self, *args):
    self.rect.x += self.speed
    if self.rect.x <= 0:
        self.rect.x = 0
    elif self .rect.right > SCREEN_RECT.right:
        self.rect.right = SCREEN_RECT.right

def fire(self):
    bullet = Bullet()
    bullet.rect.bottom = self.rect.y - 20
    bullet.rect.centerx = self.rect.centerx
    self.bullets.add(bullet)

class Bullet (GameSprites):
def init(self):
super().init("./images/bullct1.png", -2)

def update(self, *args):
    super().update()
    if self.rect.bottom < 0:
        self.kill()
def __del__(self):
    pass
   moodle作业
import os

import os.path
import time
while True:
name= input(“请选择操作系统 1 :windows 2 :linux”)
if name == “1”:
a=input(“请输入:”)
if a == “dir”:
for file in os.listdir(‘C:\’):
b= str(file)
c=os.path.getctime(“C:\{}”.format(b))
print(time.ctime©,“DIR”,file)
if name ==“2”:
a = input(“请输入指令”)
if a ==“ls”:
for file1 in os.listdir(‘C:\’)[0:3]:
print(file1,end=’ ‘)
for file2 in os.listdir(‘C:\’)[3:4]:
print(file2)
for file3 in os.listdir(‘C:\’)[4:9]:
print(file3,end =’ ‘)
for file4 in os.listdir(‘C:\’)[9:10]:
print(file4 )
for file5 in os.listdir(‘C:\’)[10:15]:
print(file5,end=’ ’ )
for file6 in os.listdir(‘C:\’)[15:16]:
print(file6 )
for file7 in os.listdir(‘C:\’)[16:21]:
print(file7,end=’ ’ )
for file8 in os.listdir(‘C:\’)[21:22]:
print(file8)
for file9 in os.listdir(‘C:\’)[22:27]:
print(file9, end=’ ‘)
for file10 in os.listdir(‘C:\’)[22:23]:
print(file10)
for file11 in os.listdir(‘C:\’)[23:28]:
print(file11, end=’ ‘)
for file12 in os.listdir(‘C:\’)[28:29]:
print(file12)
for file13 in os.listdir(‘C:\’)[29:34]:
print(file13, end=’ ‘)
for file14 in os.listdir(‘C:\’)[34:35]:
print(file14)
for file15 in os.listdir(‘C:\’)[35:40]:
print(file15, end=’ ‘)
for file16 in os.listdir(‘C:\’)[40:41]:
print(file16)
for file17 in os.listdir(‘C:\’)[41:70]:
print(file17, end=’ ')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值