会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132834个问题

# coding=utf-8
"""
新增功能:
    左上角文字绘制:
    左上角输出敌方坦克的数量6
"""
#导入pygame模块
import pygame
SCREEN_WIDTH=700      #窗口的大小是常量
SCREEN_HEIGHT=500
BG_COLOR=pygame.Color(0,0,0)    #窗口填充色:黑色
TEXT_COLOR=pygame.Color(255,0,0)        #字体颜色;红色
class MainGame():
    window=None     #定义窗口对象,初始化为None,属于类对象
    def __init__(self):
        pass
    #开始游戏
    def startGame(self):
        #加载主窗口
        pygame.display.init()   #初始化窗口(显示窗口)
        #设置窗口的大小及显示
        MainGame.window=pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])
        #窗口大小作为列表传进去,返回一个对象surface,传给窗口对象
        #MainGame.window 调用用类名.类属性
        # 设置窗口标题
        pygame.display.set_caption("坦克大战1.03")
        while True:
            # 给窗口设置填充色,用surface里面的fill方法
            MainGame.window.fill(BG_COLOR)
            #获取事件
            self.getEvent()
            #绘制文字
            MainGame.window.blit(self.getTextSurface("敌方坦克剩余数量%d"%6),(10,10))
            #(10,10)表示小的surface在窗口window中的位置
            pygame.display.update()  # 窗口一直显示
            #点击x窗口关闭不了

    #结束游戏
    def endGame(self):
        print("谢谢使用,欢迎再次使用")
        exit()
    #左上角文字的绘制
    def getTextSurface(self,text):   #text指敌方坦克数量,变化量
        #初始化字体模块
        pygame.font.init()
        #查看所有的字体名称
        #print(pygame.font.get_fonts())
        #获取字体Font对象
        font=pygame.font.SysFont("kaiti",18)
        #绘制文本信息
        textSurface=font.render(text,True,TEXT_COLOR)
        return textSurface

    #获取事件
    def getEvent(self):
        #获取所有事件
        eventList = pygame.event.get()
        #遍历事件
        for event in eventList:
            #判断按下的键是关闭还是键盘按下
            #如果按的是退出,关闭窗口
            if event.type == pygame.QUIT:
                self.endGame()
            #如果是键盘的按下
            if event.type == pygame.KEYDOWN:
                #判断按下的是上、下、左、右
                if event.key == pygame.K_LEFT:
                    print("按下左键,坦克向左移动")
                elif event.key == pygame.K_RIGHT:
                    print("按下右键,坦克向右移动")
                elif event.key == pygame.K_UP:
                    print("按下上键,坦克向上移动")
                elif event.key == pygame.K_DOWN:
                    print("按下下键,坦克向下移动")




class Tank():
    def __init__(self):
        pass
    #移动
    def move(self):
        pass
    #射击
    def shot(self):
        pass
    #展示坦克的方法
    def dispaliTank(self):
        pass
#我方坦克
class MyTank(Tank):
    def __init__(self):
        pass
#敌方坦克
class EnemyTank(Tank):
    def __init__(self):
        pass

class Bullet():
    def __init__(self):
        pass
    #移动类
    def move(self):
        pass
    #展示子弹的方法
    def displayBullet(self):
        pass

class Wall():
    def __init__(self):
        pass
    #展示墙壁的方法
    def displayWall(self):
        pass

class Explode():
    def __init__(self):
        pass
    #展示爆炸的方法
    def displayExplode(self):
        pass

class Music():
    def __init__(self):
        pass
    #播放音乐的方法
    def play(self):
        pass

if __name__=="__main__":
    #MainGame().startGame()       #调用startGame()方法显示窗口对象
    #窗口对象只显示一次就关闭
    MainGame().getTextSurface()


老师,我上面的代码为什么出现以下报错

Traceback (most recent call last):
  File "F:/py/tank/tank05.py", line 134, in <module>
Hello from the pygame community. https://www.pygame.org/contribute.html
    MainGame().getTextSurface()
TypeError: getTextSurface() missing 1 required positional argument: 'text'


Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 6782楼

import pickle

import numpy as np
from sklearn.linear_model import LogisticRegression
X = []
Y = []
genre_list = ['blues', 'classical', 'country', "disco", 'hiphop', 'jazz', 'metal']
for g in genre_list:
    for n in range(100):
        rad = "d:/BaiduNetdiskDownload/trainset/" +g+'.'+str(n).zfill(5)+'.fft'+'.npy'
        fft_features = np.load(rad)
        X.append(fft_features)
        Y.append(genre_list.index(g))
X = np.array(X)
Y = np.array(Y)
model = LogisticRegression()
model.fit(X,Y)

output = open('model.pkl', 'wb')
pickle.dump(model, output)
output.close()




D:\anaconda\envs\TF2.1\python.exe C:/Users/LENOVO/PycharmProjects/ai_tensorflow2.6/music_classify/music_classifier.py
C:\Users\LENOVO\PycharmProjects\ai_tensorflow2.6\music_classify\music_classifier.py:14: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray
  X = np.array(X)
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "C:\Users\LENOVO\PycharmProjects\ai_tensorflow2.6\music_classify\music_classifier.py", line 17, in <module>
    model.fit(X,Y)
  File "D:\anaconda\envs\TF2.1\lib\site-packages\sklearn\linear_model\_logistic.py", line 1508, in fit
    X, y = self._validate_data(
  File "D:\anaconda\envs\TF2.1\lib\site-packages\sklearn\base.py", line 581, in _validate_data
    X, y = check_X_y(X, y, **check_params)
  File "D:\anaconda\envs\TF2.1\lib\site-packages\sklearn\utils\validation.py", line 964, in check_X_y
    X = check_array(
  File "D:\anaconda\envs\TF2.1\lib\site-packages\sklearn\utils\validation.py", line 746, in check_array
    array = np.asarray(array, order=order, dtype=dtype)
  File "D:\anaconda\envs\TF2.1\lib\site-packages\numpy\core\_asarray.py", line 83, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.

请老师看看为啥创建模型这里报错了

Python 全系列/第二十三阶段:人工智能基础_机器学习理论和实战(旧)/Softmax回归 6783楼


QQ截图20200510185301.png

"""测试一个经典的GUI程序的写法,使用面向对象我的方式"""
from tkinter import *
from tkinter import messagebox
class Application(Frame):
    """一个经典的GUI程序类的写法"""
    def __init__(self,master=None):
        super().__init__(master)
        self.master = master
        self.pack()

    def createWidget(self):
        """创建组件"""
        self.btn01 =Button(self)
        self.btn01["text"]="点击送花"
        self.btn01.pack()
        self.btn01["command"]=self.songhua

        #创建一个退出按钮
        self.btnQuit=Button(self,text="退出",command=root.destroy)
        self.btnQuit.pack()
    def songhua(self):
        messagebox.showinfo("送花","送你999花啊")


if __name__=='__maiin__':

root = Tk()
root.geometry("400x100+200+300")
root.title("一个经典的GUI类的测试")
app=Application(master=root)
root.mainloop()


Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 6784楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 6786楼

Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.7.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create table departments(department_id int,department_name varchar(30),location_id int);
ERROR 1046 (3D000): No database selected
mysql> create database emp default character set utf8;
Query OK, 1 row affected (0.00 sec)

mysql> use database;
ERROR 1049 (42000): Unknown database 'database'
mysql> use emp;
Database changed
mysql> create table bjsxt (employee_id int,employee_name varchar(10),employee_salary float98,2));
    ->
    -> create table bjsxt (employee_id int,employee_name varchar(10),employee_salary float(8,2));
    -> use emp;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'float98,2));

create table bjsxt (employee_id int,employee_name varchar(10' at line 1
mysql> create table bjsxt (employee_id int,employee_name varchar(10),employee_salary float(8,2));
Query OK, 0 rows affected (0.03 sec)

mysql> create   table    departments(department_id int,department_name varchar(30),location_id int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(30),location_id int)' at line 1
mysql> use emp;
Database changed
mysql> create   table    departments(department_id int,department_name varchar(30),location_id int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(30),location_id int)' at line 1
mysql> show tables;
+---------------+
| Tables_in_emp |
+---------------+
| bjsxt         |
+---------------+
1 row in set (0.00 sec)

mysql> use emp;
Database changed
mysql> create table departments(department_id int,department_name varchar(30),location_id int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(30),location_id int)' at line 1
mysql> show tables;
+---------------+
| Tables_in_emp |
+---------------+
| bjsxt         |
+---------------+
1 row in set (0.00 sec)

mysql>  create table departments(department_id int,department_name varchar(30),location_id int);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar(30),location_id int)' at line 1
mysql> show tables;
+---------------+
| Tables_in_emp |
+---------------+
| bjsxt         |
+---------------+
1 row in set (0.00 sec)

mysql>

我这里创建第二个表时,系统提示错误,show tables不出意外,果然只有第一个表,找不出错误点在哪?63、.png



Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 6789楼
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 6790楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637