这是我现在如何实现我的简单pygames:
import pygame, sys
from pygame.locals import *
def run_game():
pygame.init()
SIZE = (640, 400)
BG_COLOUR = (0, 0, 0)
LINE_COLOUR = (255, 255, 255)
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
while True:
time_passed = clock.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
exit_game()
screen.fill(BG_COLOUR)
pygame.draw.aaline(screen, LINE_COLOUR, (1, 1), (639, 399))
pygame.display.flip()
def exit_game():
sys.exit()
if __name__ == "__main__"
run_game()
Run Code Online (Sandbox Code Playgroud)
我还看到一个keeprunning标志用于退出主事件循环,而不是使用pygame.event.poll()循环pygame.event.get().任何建议,如变量的大小写/命名,任何使其更有效或可读的东西?
我正在使用C/UNIX并且有一个包含多个记录的输入文件.我已经将每个记录映射到一个结构,并通过在数据库的记录中添加缺少的信息将结构写入输出文件.
我的问题是将结构(由字符数组组成)写回文件.我在用
fwrite(&record, sizeof(record), 1, out);
fwrite("\n", 1, 1, outfd);
Run Code Online (Sandbox Code Playgroud)
这将在输出文件中写入数据,并在每个成员之后使用终止NULL'\ 0'.请告诉我如何将此结构写入文件,而不会在每个成员之后终止'\ 0'.
我在XAMPP上注意到严格的错误报告已经开启,我现在得到了未定义的索引错误.我只有两个小问题(我还在这里学习):
我知道你不具备在PHP声明变量,但它有什么优势无论如何声明呢?如果没有,当我没有定义严格错误报告时,为什么会出现错误?
例如,当我使用get变量时,我会在运行类似函数之前检查它们的值
if($_GET['todo'] == 'adduser')
runFunctionAddUser();
Run Code Online (Sandbox Code Playgroud)
这会产生错误,因为我从不检查get变量是否首先存在.我应该这样做
if(isset($_GET['todo']))
if($_GET['todo'] == 'adduser')
runFunctionAddUser();
Run Code Online (Sandbox Code Playgroud)
代替?这会有优势还是不必要而且缓慢?
我需要一个.net结构(它模仿连接的设备内部映射),我想使用try catch块,因为我使用Marshall.PtrToStructure()和相关的GChandle东西.但是,当我在try catch块中放置结构字段赋值时,我得到此错误"必须在控制权返回给发件人之前完全分配field1".没有try catch块,基本代码工作正常.使用try catch块时是否有任何解决此错误的方法?我应该使用try catch吗?
[StructLayout( LayoutKind.Sequential )]
public struct Effects
{
public UInt16 field_1;
public UInt16 field_2;
...
public Effects(byte[] effectsData)
{
GCHandle gch;
try
{
gch = GCHandle.Alloc( effectsData, GCHandleType.Pinned );
IntPtr pEffects = gch.AddrOfPinnedObject( );
this = (Effects)Marshal.PtrToStructure( pEffects, typeof(Effects ) );
}
catch (Exception ex)
{
}
finally
{
if (gch.IsAllocated)
gch.Free( );
}
}
}
Run Code Online (Sandbox Code Playgroud) 我是C的新手.我在googled很多关于下面代码的输出.但没有太大的帮助.
这是代码:
struct str
{
int i: 1;
int j: 2;
int k: 3;
int l: 4;
};
struct str s;
s.i = 1;
s.j = 2;
s.k = 5;
s.l = 10;
printf(" i: %d \n j: %d \n k: %d \n l: %d \n", s.i, s.j, s.k, s.l);
Output:
i: -1
j: -2
k: -3
l: -6
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么输出如此?谢谢.
如果这是一个愚蠢的问题,请不要投票给我,我只是想了解.
我有一个结构,有一些指针作为成员,我正在尝试做memcpy,我有人建议我不应该在这种情况下使用memcpy作为memcpy做一个浅拷贝(意思是它复制指针)相当深的拷贝(意味着复制什么指针)指向).
但我不确定为什么它在以下程序中没有任何区别:请查看代码和输出,并解释为什么在这种情况下它不是浅层副本?
#include <stdio.h>
#include <malloc.h>
#include <string.h>
struct student {
char *username;
char *id;
int roll;
};
void print_struct(struct student *);
void print_struct_addr(struct student *);
void changeme(struct student *);
int main (void) {
struct student *student1;
char *name = "ram";
char *id = "200ABCD";
int roll = 34;
student1 = (struct student *)malloc(sizeof(struct student));
student1->username = name;
student1->id = id;
student1->roll = roll;
print_struct_addr(student1);
print_struct(student1);
changeme(student1);
print_struct(student1);
print_struct_addr(student1);
return 0;
}
void print_struct(struct student *s) {
printf("Name: %s\n", …Run Code Online (Sandbox Code Playgroud) 我正在使用以龙卷风为核心的mongodb数据库后端.我目前只有我的主文件中有一堆处理程序.它是一个多用户webapp,在用户之间有链接,也就是"朋友"系统.
My current structure is:
templates/
static/
main.py (contains all handlers)
user_actions.py
auth_actions.py
.
.
.
bar_actions.py
Run Code Online (Sandbox Code Playgroud)
大多数处理程序对应于动作文件.例如,友元请求处理程序对应于user_actions.py中的函数,该函数接受数据库和userids作为参数.我觉得这不是这么大项目的最佳布局.我是否应该有某种类型的模型文件包含当前用户的模型,或者这只是多余的.我目前正在将当前用户存储为cookie中的字典.
我有一个结构数组定义如下:
struct {
int x;
char y;
} arr[10];
我机器上int的大小是4个字节,char是1个字节.我知道结构将在内部填充,即数组的每个元素将具有8个字节的大小.但我想知道是否
1)这是因为下一个数组元素中的int类型成员的对齐要求
2)是因为每个结构本身应该在8字节边界上对齐,因为结构类型变量需要自然对齐.
为了使我的观点更清楚,应该是第一个数组成员的起始地址?如果它是一个8字节对齐的地址,如第二种情况所指出的那样,这可能是一个问题,同时定义大的2-D数组,如:
int arr [1000] [1000];
这里,2-D阵列的每个元素(即每个1-D阵列)应该在4000字节边界上对齐.机器可能没有内存孔来满足此内存要求.
我试图使用大括号初始化一个结构,但我真的试图初始化从malloc调用返回的指针指向的结构.
typedef struct foo{
int x;
int y;
} foo;
foo bar = {5,6};
Run Code Online (Sandbox Code Playgroud)
我知道如何做到这一点,但我需要在这种情况下这样做.
foo * bar = malloc(sizeof(foo));
*bar = {3,4};
Run Code Online (Sandbox Code Playgroud) 例如考虑:
struct strct
{
data member_1;
data member_2;
......
};
Run Code Online (Sandbox Code Playgroud)
编译器何时识别
struct strct
Run Code Online (Sandbox Code Playgroud)
作为数据类型?是在执行该行之后
struct strct
Run Code Online (Sandbox Code Playgroud)
?或者在遇到结构定义的右括号之后?